0

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
iddqd
  • 1,225
  • 2
  • 16
  • 34

1 Answers1

1

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
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks! the code problems are caused by some rushed copy and paste i did from Microsoft example and my own code. But isn't it possible that `WlanFreeMemory(pIfList);` will recursivly free the `WLAN_INTERFACE_INFO` array inside `pIntfList`, which will also free `IfInfo ` ? – iddqd Mar 24 '16 at 12:04
  • It will free that array, but you took a **copy** of the struct – David Heffernan Mar 24 '16 at 12:23