c++windowswinapiwlanapi

wlanapi - will WlanFreeMemory free its WLAN_INTERFACE_INFO array struct?


I am using Microsoft's WLAN API in the following code (I left only the relevant pieces from the example):

WLAN_INTERFACE_INFO_LIST structure

WLAN_INTERFACE_INFO structure

WlanEnumInterfaces() function

WlanFreeMemory() function

PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
WLAN_INTERFACE_INFO pIfInfo = NULL;
WlanEnumInterfaces(hClient, NULL, &pIfList); 
pIfInfo = pIntfList->InterfaceInfo[i];
WlanFreeMemory(pIfList);

I am saving the active interface, which is located in pIntfList->InterfaceInfo[i], to pIfInfo.

Will WlanFreeMemory(pIfList) also free pIfInfo and leave this variable useless? Or is its values copied to a new structure when doing pIfInfo = pIntfList->InterfaceInfo[i]?

Is there any way to avoid keeping the entire WLAN_INTERFACE_INFO struct variable and only keeping a PWLAN_INTERFACE_INFO?


Solution

  • Will WlanFreeMemory(pIfList) also free pIfInfo and leave this variable useless?

    No. Your variable pIfInfo is actually a struct rather than a pointer. So when you write

    WLAN_INTERFACE_INFO pIfInfo = NULL;
    WlanEnumInterfaces(hClient, NULL, &pIfList); 
    pIfInfo = pIntfList->InterfaceInfo[i];
    

    you are taking a copy of the struct. Note that the code does not compile because you cannot assign NULL to a struct. Note also that pIfInfo is a poor choice of name because it implies to the read that the variable is a pointer.

    Now, the call to WlanFreeMemory(pIfList) will free all of the memory allocated, including the array pIntfList->InterfaceInfo[]. But since you take a copy of the struct, a copy of element i of the array, that does not affect you.

    I would probably write your code like this:

    PWLAN_INTERFACE_INFO_LIST pIfList;
    if (WlanEnumInterfaces(hClient, NULL, &pIfList) != ERROR_SUCCESS)
    {
        // handle error
    }
    WLAN_INTERFACE_INFO IfInfo = pIntfList->InterfaceInfo[i];
    WlanFreeMemory(pIfList);
    // can still use IfInfo, but not pIfList