0

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()]

Community
  • 1
  • 1
Cœur
  • 32,421
  • 21
  • 173
  • 232

2 Answers2

2

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.

thumbmunkeys
  • 20,144
  • 8
  • 56
  • 107
  • Apparently Andy-Pennell was successful at it in C++ (first link of the question). And it's not exactly an application I'm doing, it's more of a portable library. – Cœur Sep 22 '14 at 12:33
  • The article clearly mentions it's for Windows Phone 8. There is no C++ runtime component for windows phone 7, also no PInvoke. So I don't see how it would work. – thumbmunkeys Sep 22 '14 at 13:16
  • OK, your last assertion about `PInvoke` is the truth. While in Windows Phone 8 it only takes two words, `PeerFinder.DisplayName`, Andy Pennell's elaborate solution with native code is not applicable for Windows Phone 7 and so mostly useless. -_-' – Cœur Sep 23 '14 at 08:27
  • @Cœur I know your frustration. Due to the many changes in the APIs between 7, 7.5, 8 and 8.1 it's sometimes quite hard to find the proper API. – thumbmunkeys Sep 23 '14 at 09:46
0

WP7 allows C# code, not C++. And due C# implementation of sockets, you don't need to call WSAStartup() manually, the system will do that for you automatically.

Yury Schkatula
  • 4,650
  • 2
  • 16
  • 35
  • Fine. How to call getaddrinfo in C#, then? I need it to get the phone name (like, "My Windows Phone"). – Cœur Sep 22 '14 at 12:31
  • I afraid, phone name is not a thing you can extract via network API at WP7. Once I did such investigations, I found useful the only things like NetworkInterface, NetworkInterfaceInfo and DeviceNetworkInformation – Yury Schkatula Sep 23 '14 at 16:31