简体   繁体   中英

P/Invoke WlanHostedNetworkQueryStatus from wlanapi.dll

I have mapped C++ function (from WLanapi.dll):

    DWORD WINAPI WlanHostedNetworkQueryStatus(
      _In_        HANDLE hClientHandle,
      _Out_       PWLAN_HOSTED_NETWORK_STATUS *ppWlanHostedNetworkStatus,
      _Reserved_  PVOID pvReserved
    );

To the following C# code:

    [DllImport("Wlanapi.dll", SetLastError = true)]
    static extern UInt32 WlanHostedNetworkQueryStatus(
        [In] IntPtr hClientHandle,
        [Out] out _WLAN_HOSTED_NETWORK_STATUS ppWlanHostedNetworkStatus,
        [In, Out] IntPtr pvReserved
        );

I have also mapped all the structs and enums required and other stuff (for example to get the clientHandle pointer and to start the hosted network).
The _WLAN_HOSTED_NETWORK_STATUS is mapped like so:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct _WLAN_HOSTED_NETWORK_STATUS
    {
        public _WLAN_HOSTED_NETWORK_STATE HostedNetworkState;
        public Guid IPDeviceID;
        public _DOT11_MAC_ADDRESS wlanHostedNetworkBSSID;
        public _DOT11_PHY_TYPE dot11PhyType;
        public UInt32 ulChannelFrequency;
        public UInt32 dwNumberOfPeers;
        public _WLAN_HOSTED_NETWORK_PEER_STATE[] PeerList;
    }

Now when executing that function, I am not sure how to use ppWlanHostedNetworkStatus correctly and such. The function returns ERROR_SUCCESS (0) which means I have called it and passed parameters correctly as far as I am concerned:

    _WLAN_HOSTED_NETWORK_STATUS netStatus = new _WLAN_HOSTED_NETWORK_STATUS();

    WlanHostedNetworkQueryStatus(clientHandle, out netStatus, IntPtr.Zero);

But while querying ppWlanHostedNetworkStatus for values (like state of the network, or number of connected peers) I am getting just some strange long integers (I would say memory addresses, but I am not sure), for example call:

netStatus.HostedNetworkState.ToString();

Returns

11465720

HostedNetworkState is an enumeration defined like:

    public enum _WLAN_HOSTED_NETWORK_STATE
    {
        wlan_hosted_network_unavailable,
        wlan_hosted_network_idle,
        wlan_hosted_network_active
    }

So .toString() should have returned one of these strings from the enumeration, right?
I am pretty sure it is something to do with pointers etc, since in the documentation of the _WLAN_HOSTED_NETWORK_STATUS ( MS documentation ) it says that before the call to that function, the ppWlanHostedNetworkStatus should be a NULL, and that it is itself a pointer to the structure...

How can I debug it? I am coding in C#, VS 2012...

Thanks for your help.

-----EDIT-----
I further tried to map the function with IntPtr as an argument, pass IntPtr.Zero and Marshal.PtrToStruct, but I am getting AccessViolationException when trying to do that...

    [DllImport("Wlanapi.dll", SetLastError = true)]
    static extern UInt32 WlanHostedNetworkQueryStatus(
        [In] IntPtr hClientHandle,
        [Out] out IntPtr ppWlanHostedNetworkStatus,
        [In, Out] IntPtr pvReserved
        );

And then:

    IntPtr ppStatus = IntPtr.Zero;

    WlanHostedNetworkQueryStatus(clientHandle, out ppStatus, IntPtr.Zero);

    _WLAN_HOSTED_NETWORK_STATUS netStatus = (_WLAN_HOSTED_NETWORK_STATUS)Marshal.PtrToStructure(ppStatus, typeof(_WLAN_HOSTED_NETWORK_STATUS));

------EDIT 2-------

Following advice from Fermat2357, I have uncommented part of the struct to map, and change the following to count for a pointer to pointer:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct _WLAN_HOSTED_NETWORK_STATUS
    {
        public _WLAN_HOSTED_NETWORK_STATE HostedNetworkState;
        public Guid IPDeviceID;
        public _DOT11_MAC_ADDRESS wlanHostedNetworkBSSID;
        public _DOT11_PHY_TYPE dot11PhyType;
        public UInt32 ulChannelFrequency;
        public UInt32 dwNumberOfPeers;
        //public _WLAN_HOSTED_NETWORK_PEER_STATE[] PeerList;
    }

I call it like this:

    IntPtr ppStatus = IntPtr.Zero;
    WlanHostedNetworkQueryStatus(clientHandle, out ppStatus, IntPtr.Zero);
    IntPtr ppStatus2 = new IntPtr(ppStatus.ToInt32());
    _WLAN_HOSTED_NETWORK_STATUS stat = (_WLAN_HOSTED_NETWORK_STATUS)Marshal.PtrToStructure(ppStatus2, typeof(_WLAN_HOSTED_NETWORK_STATUS));
    netStatus = stat.HostedNetworkState.ToString();

This finally gives me the correct network status (active after starting it)... Now I have to find a way to marshal that dynamic array...
Thanks so far for help Fermat2357

Your mapping is incorrect. Take a look at the definition of the API function WlanHostedNetworkQueryStatus .

DWORD WINAPI WlanHostedNetworkQueryStatus(
  _In_        HANDLE hClientHandle,
  _Out_       PWLAN_HOSTED_NETWORK_STATUS *ppWlanHostedNetworkStatus,
  _Reserved_  PVOID pvReserved
);

Please take care, the parameter ppWlanHostedNetworkStatus is a pointer to a pointer to a WLAN_HOSTED_NETWORK_STATUS structure. Take a deeper look in the documentation of the function you will find

ppWlanHostedNetworkStatus [out]

On input, this parameter must be NULL.

On output, this parameter receives a pointer to the current status of the wireless Hosted Network, if the call to the WlanHostedNetworkQueryStatus function succeeds. The current status is returned in a WLAN_HOSTED_NETWORK_STATUS structure.

If you give in a NULL pointer (as described in the documentation) the underlying API will allocate a buffer for you holding the structure and initializing the pointer to this buffer. Later dont forget to free it by a call to WlanFreeMemory . Otherwise you will have a resource leak here.

However the documentation seems to be not 100% complete for this function. In my tests (Win7 32Bit) the API will not allocate memory for you if you intitalize the pointer to a enough big memory buffer. In this case a later call to WlanFreeMemory seems not be necessary. But in this case its hard to guess how much memory you need for the following WLAN_HOSTED_NETWORK_PEER_STATE structures. For that reason this seems not to be useable anyway.

Here is the C code I used for testing

#include <Wlanapi.h>

int _tmain(int argc, _TCHAR* argv[])
{
    DWORD dwRes;
    HANDLE hHandle;
    DWORD negotiatedVersion;

    dwRes = WlanOpenHandle(1, NULL, &negotiatedVersion, &hHandle);
    if (ERROR_SUCCESS == dwRes)
    {
        PWLAN_HOSTED_NETWORK_STATUS pStatus = NULL;

        dwRes = WlanHostedNetworkQueryStatus(hHandle, &pStatus, NULL);
        if (ERROR_SUCCESS == dwRes)
        {
            if (wlan_hosted_network_unavailable != pStatus->HostedNetworkState)
            {
                // Do something with the result
            }

            WlanFreeMemory(pStatus);
        }
        else
        {
            // handle Error
        }

        WlanCloseHandle(hHandle, NULL);
    }
    else
    {
        // handle Error
    }

    return 0;
}

If you want to make your sample work you need to modify the way you marshal the structure.

Edit

To marshal correctly you can try the following:

...
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct _WLAN_HOSTED_NETWORK_STATUS
{
    public _WLAN_HOSTED_NETWORK_STATE HostedNetworkState;
    public Guid IPDeviceID;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
    public string wlanHostedNetworkBSSID;
    public _DOT11_PHY_TYPE dot11PhyType;
    public UInt32 ulChannelFrequency;
    public UInt32 dwNumberOfPeers;
    public _WLAN_HOSTED_NETWORK_PEER_STATE PeerList;
}

...

IntPtr ptr = new IntPtr();
uint hostedNetworkQueryStatusSuccess = WlanHostedNetworkQueryStatus(clientHandle, out ptr, IntPtr.Zero);
if (openHandleSuccess == 0)
{
    var netStat = (_WLAN_HOSTED_NETWORK_STATUS)Marshal.PtrToStructure(ptr, typeof(_WLAN_HOSTED_NETWORK_STATUS));
    Console.WriteLine(netStat.HostedNetworkState);

    if (netStat.HostedNetworkState != _WLAN_HOSTED_NETWORK_STATE.wlan_hosted_network_unavailable)
    {
        IntPtr offset = Marshal.OffsetOf(typeof(_WLAN_HOSTED_NETWORK_STATUS), "PeerList");

        for (int i = 0; i < netStat.dwNumberOfPeers; i++)
        {
            var peer = (_WLAN_HOSTED_NETWORK_PEER_STATE)Marshal.PtrToStructure(
            new IntPtr(ptr.ToInt64() + offset.ToInt64()), 
            typeof(_WLAN_HOSTED_NETWORK_PEER_STATE));

            System.Console.WriteLine(peer.PeerMacAddress);

            offset += Marshal.SizeOf(peer);
        }
    }
}

The Idea: Im using a structure here that I can be sure it is marshaled correctly all the time. Later with the knowledge of the dwNumberOfPeers member (if it is valid) I get all the inner structures step by step.

EDIT: One thing to try is a trick I use to help debugging - replace your struct with a byte array, and see what you pull back (ok, in this case a uint array):

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct _WLAN_HOSTED_NETWORK_STATUS
{
    [MarshalAs(UnmanagedType.ByValArray, ArraySubType=UnmanagedType.U4, SizeConst = 48)]
    public uint[] scratch;
}

I get very strange values when I run this locally, however - the first (10x4) = 40 bytes are zeroed out:

0 0 0 0 
0 0 0 0 
0 0 4D454D4C 28 
16DCD870 0 787F6447 80000020 
...omitted...

Try this set of P/Invoke declarations:

(done in LINQPad, so replace "Dump" method accordingly)

void Main()
{
    IntPtr clientHandle;
    int negotiatedVersion;
    if(WlanOpenHandle(2, IntPtr.Zero, out negotiatedVersion, out clientHandle) != 0)
    {
        throw new InvalidOperationException("Could not open handle");
    }
    Console.WriteLine("Negotiated version:{0}", negotiatedVersion);
    IntPtr pNetStatus = IntPtr.Zero;
    if(WlanHostedNetworkQueryStatus(clientHandle, out pNetStatus, IntPtr.Zero) != 0)
    {
        throw new InvalidOperationException("Could not query network status");
    }
    var netStatus = (WLAN_HOSTED_NETWORK_STATUS)Marshal.PtrToStructure(pNetStatus, typeof(WLAN_HOSTED_NETWORK_STATUS));
    Console.WriteLine(netStatus.PeerList[0]);
    WlanFreeMemory(pNetStatus);
    WlanCloseHandle(clientHandle, IntPtr.Zero);
}


[DllImport("Wlanapi.dll", SetLastError = true)]
[return:MarshalAs(UnmanagedType.Bool)]
public static extern bool WlanOpenHandle(
  [In] int dwClientVersion,
  IntPtr pReserved,
  [Out] out int pdwNegotiatedVersion,
  [Out] out IntPtr phClientHandle
);
[DllImport("Wlanapi.dll", SetLastError = true)]
[return:MarshalAs(UnmanagedType.Bool)]
public static extern bool WlanCloseHandle(
  [In] IntPtr hClientHandle,
  IntPtr pReserved
);
[DllImport("Wlanapi.dll", SetLastError = true)]
static extern UInt32 WlanHostedNetworkQueryStatus(
    [In] IntPtr hClientHandle,
    [Out] out IntPtr ppWlanHostedNetworkStatus,
    [In, Out] IntPtr pvReserved
);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct _WLAN_HOSTED_NETWORK_STATUS
{
    public _WLAN_HOSTED_NETWORK_STATE HostedNetworkState;
    public Guid IPDeviceID;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=6)]
    public string wlanHostedNetworkBSSID;
    public _DOT11_PHY_TYPE dot11PhyType;
    public UInt32 ulChannelFrequency;
    public UInt32 dwNumberOfPeers;
    public IntPtr PeerList;
}
public enum _WLAN_HOSTED_NETWORK_STATE 
{ 
  wlan_hosted_network_unavailable,
  wlan_hosted_network_idle,
  wlan_hosted_network_active
}
public enum _DOT11_PHY_TYPE : uint
{ 
  dot11_phy_type_unknown     = 0,
  dot11_phy_type_any         = 0,
  dot11_phy_type_fhss        = 1,
  dot11_phy_type_dsss        = 2,
  dot11_phy_type_irbaseband  = 3,
  dot11_phy_type_ofdm        = 4,
  dot11_phy_type_hrdsss      = 5,
  dot11_phy_type_erp         = 6,
  dot11_phy_type_ht          = 7,
  dot11_phy_type_IHV_start   = 0x80000000,
  dot11_phy_type_IHV_end     = 0xffffffff
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct _WLAN_HOSTED_NETWORK_PEER_STATE 
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=6)]
    public string PeerMacAddress;
    _WLAN_HOSTED_NETWORK_PEER_AUTH_STATE PeerAuthState;
}
public enum _WLAN_HOSTED_NETWORK_PEER_AUTH_STATE 
{ 
  wlan_hosted_network_peer_state_invalid,
  wlan_hosted_network_peer_state_authenticated
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM