Simurr Simurr - 1 year ago 201
C++ Question

Get the ip address of Windows wifi hosted network device in c++ (WLANAPI)

I'm adding the ability within an app to create a wifi hosted network. I finally got the example app working from the Windows 7 sdk.

What I am not finding is a built in way to get the IP address of the wifi device used to create the hosted network. I need that address to bind a server to it.

The only way I can think of doing this is maybe using the MAC address of the device and matching that to an entry in a list of all network devices on the machine? Is that the only way? Would that even work since the wifi hosted device is virtual (according to the documentation)?

There looks like a way (WlanHostedNetworkQueryStatus) to get the MAC address and GUID of the wlan device used but the best way to turn that into an actual IP address eludes me...

I'm pretty new to C++ and Windows development so maybe it's something simple I'm missing.


I ended up using the IP Helper API to convert the WLAN GUID to LUID and then LUID to Index. The included header files were a bit annoying to figure out since most documentation seems to be geared for device drivers. The ones I needed were.

#include <windows.h>
#include <wlanapi.h>
#include <iphlpapi.h>
#include <netioapi.h>

Since I'm using Qt I used the index to get a QNetworkInterface and from that I got the IPv4 address. It was simpler than figuring out NotifyAddrChange or NotifyIpInterfaceChange, though one of those would be the way to go if you are just using Windows API.

Answer Source

The WlanHostedNetworkQueryStatus function returns a WLAN_HOSTED_NETWORK_STATUS structure that has a member IPDeviceID. The documentation for that member specifies:

This is member is the GUID of a virtual wireless device which would not be available through calls to the WlanEnumInterfaces function. This GUID can be used for calling other higher layer networking functions that use the device GUID (IP Helper functions, for example).

The IP Helper functions have a function ConvertInterfaceGuidToLuid, which will give you a LUID that you can use with the functions with the other functions in that list.

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