Cœur Cœur -4 years ago 216
C# Question

How to call WSAStartup() in C# (on Windows Phone 7)?

Goal: getting a Windows Phone 7 name (like, "My Windows Phone").

Lead: http://blogs.msdn.com/b/andypennell/archive/2013/11/09/getting-the-name-of-your-windows-phone-8-device.aspx

The above code is provided as C++, and I would like to change it to C#. One reason is because I only have Visual Studio 2012 Express and it doesn't allow me to use C++ together with a Windows Phone project, and I already bought VS2013, can't afford VS2012 just for this. Another reason is it's more than 150 files of dependencies in C++, that's too much code!

So, my first attempt: (inspired by http://www.experts-exchange.com/Programming/Languages/.NET/Q_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();
}


It fails on
WSAStartup(0x0002, out dummy);
with Exception:


A first chance exception of type 'System.MethodAccessException' occurred in MyLibrary.dll

Additional information: Attempt to access the method failed: MyLibrary.WSAStartup(System.Int16, .WSAData&)


And my second attempt: (inspired by Convert service name to port)

[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();
}


It fails on
NativeMethods.WSAStartup(0x0202, ref dummy);
with Exception:


A first chance exception of type 'System.MethodAccessException' occurred in MyLibrary.dll

Additional information: Attempt to access the method failed: MyLibrary+NativeMethods.WSAStartup(System.Int16, .WSAData&)


Any advice to make it work on a WP7 device?

[edit: this post also suggests the possibility of version 0x0101 to use WSAStartup()]

Answer Source

WSAStartup has a SecurityCriticalAttribute that makes for internal only, you can't it use from your application.

PInvoke is not allowed for windows phone 7 apps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download