目标:获得Windows 7的名称(比如“我的Windows”)。
铅:http://blogs.msdn.com/b/andypennell/archive/2013/11/09/getting-the-name-of-your-windows-phone-8-device.aspx
上面的代码是以C++的形式提供的,我想将它更改为C#。原因之一是我只有VisualStudio2012Express,它不允许我在Windows项目的同时使用C++,而且我已经购买了VS2013,不能只为此购买VS2012。另一个原因是C++中有超过150个依赖文件,这是太多的代码了!
所以,我的第一次尝试:(受21014265.html启发)
[StructLayout(LayoutKind.Sequential)]
internal struct WSAData
{
public short wVersion;
public short wHighVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 257)]
public string szDescription;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 129)]
public string szSystemStatus;
public short iMaxSockets;
public short iMaxUdpDg;
public int lpVendorInfo;
}
[DllImport("wsock32.dll")]
internal static extern int WSAStartup(
[In] short wVersionRequested,
[Out] out WSAData lpWSAData
);
[DllImport("wsock32.dll")]
internal static extern int WSACleanup();
public static void Test()
{
WSAData dummy;
WSAStartup(0x0002, out dummy);
// TODO: more stuff
WSACleanup();
}它在WSAStartup(0x0002, out dummy);上失败,只有例外:
System.MethodAccessException类型的第一次例外发生在MyLibrary.dll中 附加信息:尝试访问方法失败: MyLibrary.WSAStartup(System.Int16,.WSAData&)
我的第二次尝试:(受将服务名称转换为端口启发)
[StructLayout(LayoutKind.Sequential)]
public struct WSAData
{
public short version;
public short highVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 257)]
public string description;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 129)]
public string systemStatus;
public short maxSockets;
public short maxUdpDg;
public IntPtr vendorInfo;
}
internal static class NativeMethods
{
[DllImport("Ws2_32.dll")]
public static extern Int32 WSAStartup(short wVersionRequested, ref WSAData wsaData);
[DllImport("Ws2_32.dll")]
public static extern Int32 WSACleanup();
}
public static void Test()
{
WSAData dummy = new WSAData();
NativeMethods.WSAStartup(0x0202, ref dummy);
// TODO: more stuff
NativeMethods.WSACleanup();
}它在NativeMethods.WSAStartup(0x0202, ref dummy);上失败,只有例外:
System.MethodAccessException类型的第一次例外发生在MyLibrary.dll中 附加信息:尝试访问该方法失败: MyLibrary+NativeMethods.WSAStartup(System.Int16,.WSAData&)
有什么建议可以让它在WP7设备上工作吗?
[编辑:这个职位也暗示了0x0101版使用WSAStartup()的可能性]
发布于 2014-09-22 11:25:50
WSAStartup 有一个SecurityCriticalAttribute只用于内部,您不能从应用程序中使用它。
windows phone 7应用程序不允许使用PInvoke。
发布于 2014-09-22 11:36:06
WP7允许C#代码,而不是C++。由于套接字的C#实现,您不需要手动调用WSAStartup(),系统将自动为您调用。
https://stackoverflow.com/questions/25972709
复制相似问题