wlanapi.dll c#

N

Nelson Guerrero

Hi, I'm trying to obtain the profile list using the wlanapi.dll but I'm
receveing errors. The following code is what I've done so far. I took
the structures and functions definition from
http://msdn2.microsoft.com/en-us/library/ms706716.aspx



[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct WLAN_PROFILE_INFO
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public char[] strInterfaceDescription;
public int dwFlags;
}

public class WLAN_PROFILE_INFO_LIST
{
public int dwNumberOfItems;
public int dwIndex;
[MarshalAs(UnmanagedType.ByValArray)]
public WLAN_PROFILE_INFO[] ProfileInfo;
}

[DllImport("wlanapi.dll", SetLastError = true)]
private static extern int WlanGetProfileList(
[In] IntPtr hClientHandle,
[In] Guid pInterfaceGuid,
[In] string pReserved,
[Out]
[System.Runtime.InteropServices.MarshalAsAttribute(UnmanagedType.LPStruct)]
out WLAN_PROFILE_INFO_LIST ppProfileList);

private void myfunction()
{
int ierror;

string pReservedGetAvailableNetworkList = null;
ierror = WlanEnumInterfaces(phClientHandle,
pReservedGetAvailableNetworkList, out this.wIIL);
for (int i = 0; i < wIIL.dwNumberOfItems; i++)
{
WLAN_PROFILE_INFO_LIST wPIL=new WLAN_PROFILE_INFO_LIST();
ierror = WlanGetProfileList(phClientHandle,
wIIL.InterfaceInfo.InterfaceGuid,
pReservedGetAvailableNetworkList , out wPIL);
}

}

When I call "myfunction()" I receive the following message:
PInvokeStackImbalance was detected
Message: A call to PInvoke function 'WlanapiWrapper::WlanGetProfileList'
has unbalanced the stack. This is likely because the managed PInvoke
signature does not match the unmanaged target signature. Check that the
calling convention and parameters of the PInvoke signature match the
target unmanaged signature.
I checked all the types, and I did it based on the functions that
already work like WlanOpenHandle and WlanEnumInterfaces, as you see both
functions have the same parameters definition so what I know is that
the message refers to the type that I defined on the declaration method,
I tried changing it and I received different errors, so, I'll appreciate
any help cause this problem is driving me crazy.

Thanks,
Nelson
 
M

Mattias Sjögren

Nelson,
public class WLAN_PROFILE_INFO_LIST
{
public int dwNumberOfItems;
public int dwIndex;
[MarshalAs(UnmanagedType.ByValArray)]
public WLAN_PROFILE_INFO[] ProfileInfo;
}

You can't use ByValArray without also specifying an array size with
SizeConst. In other words, you can't use it for variable length
structs. Here's some guidance on how you can deal with that

http://dotnetinterop.com/faq/?q=VariableLengthStruct

[DllImport("wlanapi.dll", SetLastError = true)]
private static extern int WlanGetProfileList(
[In] IntPtr hClientHandle,
[In] Guid pInterfaceGuid,
[In] string pReserved,
[Out]
[System.Runtime.InteropServices.MarshalAsAttribute(UnmanagedType.LPStruct)]
out WLAN_PROFILE_INFO_LIST ppProfileList);

Should be

[DllImport("wlanapi.dll", SetLastError = true)]
private static extern int WlanGetProfileList(
[In] IntPtr hClientHandle,
[In] ref Guid pInterfaceGuid,
[In] string pReserved,
out IntPtr ppProfileList);


Mattias
 
N

Nelson Guerrero

Mattias, well, I dont know. Like I said, it works in the same way with
the other structures, so why this one needs to be changed? Anyway, I
change just the guid parameter and declared ref and it works. :) So, Its
a bit confusing.
Thanks

Nelson

Nelson,
public class WLAN_PROFILE_INFO_LIST
{
public int dwNumberOfItems;
public int dwIndex;
[MarshalAs(UnmanagedType.ByValArray)]
public WLAN_PROFILE_INFO[] ProfileInfo;
}

You can't use ByValArray without also specifying an array size with
SizeConst. In other words, you can't use it for variable length
structs. Here's some guidance on how you can deal with that

http://dotnetinterop.com/faq/?q=VariableLengthStruct

[DllImport("wlanapi.dll", SetLastError = true)]
private static extern int WlanGetProfileList(
[In] IntPtr hClientHandle,
[In] Guid pInterfaceGuid,
[In] string pReserved,
[Out]
[System.Runtime.InteropServices.MarshalAsAttribute(UnmanagedType.LPStruct)]
out WLAN_PROFILE_INFO_LIST ppProfileList);

Should be

[DllImport("wlanapi.dll", SetLastError = true)]
private static extern int WlanGetProfileList(
[In] IntPtr hClientHandle,
[In] ref Guid pInterfaceGuid,
[In] string pReserved,
out IntPtr ppProfileList);


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top