Copying from IntPtr to struct



Hi,

I am using pinvoke in the compact framework to implement some WZCSAPI
functions. It is working and I'm getting the required stuff from the
function, but it is in the form of a IntPtr which I need to copy to a
marshalled struct (WZC_802_11_CONFIG_LIST) which I have shown below.
When I try to copy from the IntPtr to the struct, I am getting an
exception like the below:

System.NotSupportedException was unhandled
Message="NotSupportedException"

<Code snippet>
[StructLayout(LayoutKind.Sequential)]
public struct WZC_WLAN_CONFIG
{
public uint Length;
public uint dwCtlFlags;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
public byte[] MacAddress;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public byte[] Reserved;
[MarshalAs(UnmanagedType.LPStruct)]
public NDIS_802_11_SSID Ssid;
public uint Privacy;
public uint Rssi;
public NDIS_802_11_NETWORK_TYPE NetworkTypeInUse;
[MarshalAs(UnmanagedType.LPStruct)]
public NDIS_802_11_CONFIGURATION Configuration;
public NDIS_802_11_NETWORK_INFRASTRUCTURE
InfrastructureMode;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] SupportedRates;
public uint KeyIndex;
public uint KeyLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst =
WZCCTL_MAX_WEPK_MATERIAL)]
public byte[] KeyMaterial;
public NDIS_802_11_AUTHENTICATION_MODE AuthenticationMode;
public IntPtr rdUserData;
[MarshalAs(UnmanagedType.LPStruct)]
public WZC_EAPOL_PARAMS EapolParams;
public IntPtr rdNetworkData;
public uint WPAMCastCipher;
public uint ulMediaType;
}

struct WZC_802_11_CONFIG_LIST
{
public uint NumberOfItems; // number of elements in the
array below
public uint Index; // [start] index in the array
below
[MarshalAs(UnmanagedType.ByValArray, ArraySubType =
UnmanagedType.LPStruct, SizeConst = 1)]
public WZC_WLAN_CONFIG [] Config; // array of
WZC_WLAN_CONFIGs
}


[DllImport("wzcsapi.dll", SetLastError = true)]
private static extern uint WZCQueryInterfaceEx(
string pSrvAddr,
uint dwInFlags,
ref INTF_ENTRY_EX pIntfEx,
ref uint pdwOutFlags);

void function()
{

uint dwOutFlags = 0;

INTF_ENTRY_EX Intf = new INTF_ENTRY_EX();
//Marshal.(&Intf, 0x00, sizeof(INTF_ENTRY_EX));
Intf.wszGuid = szWiFiCard;

uint dwStatus = WZCQueryInterfaceEx(
null,
INTF_ALL,
ref Intf,
ref dwOutFlags);
if (dwStatus != 0)
{
string Error = "WZCEnumInterfaces() error 0x" +
dwStatus.ToString("X"); //ToString("X") converts to hex
MessageBox.Show(Error, "Error");
WZCDeleteIntfObjEx(ref Intf);
return;
}

byte[] data = new byte[Intf.rdStSSIDList.dwDataLen];

Marshal.Copy(Intf.rdStSSIDList.pData, data, 0,
(int)Intf.rdStSSIDList.dwDataLen);
GCHandle pinnedPacket = GCHandle.Alloc(data,
GCHandleType.Pinned);
WZC_802_11_CONFIG_LIST pConfigList = new
WZC_802_11_CONFIG_LIST();
pConfigList =
(WZC_802_11_CONFIG_LIST)Marshal.PtrToStructure(pinnedPacket.AddrOfPinnedObject(),
typeof(WZC_802_11_CONFIG_LIST));
}
</Code snippet>
.



Relevant Pages

  • Re: Access Denied executing Batch File from CreateProcessAsUser
    ... > public uint dwX; ... > public IntPtr lpReserved2; ... > private extern static bool CreateProcessAsUser(IntPtr hToken, ... > lpApplicationName, String lpCommandLine, ref SECURITY_ATTRIBUTES ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Process List
    ... If you pass a struct, which is a value type, you must add the ... "ref" decoration to the parameter to tell it to pass it by ref. ... snapshot using Process32First and Process32Next. ... public uint cntUsage; ...
    (microsoft.public.dotnet.framework.compactframework)
  • DCOM across domains
    ... public IntPtr pIID; ... public struct COAUTHIDENTITY ... public uint DomainLength; ...
    (microsoft.public.dotnet.framework.interop)
  • Manipulating win32 app menus from c# using pinvoke...
    ... supplying the fMask of the MENUITEMINFO with the MF_OWNERDRAW constant from ... public uint cbSize; ... public IntPtr hbmpChecked; ... private static extern IntPtr GetSubMenu ...
    (microsoft.public.dotnet.languages.csharp)
  • Function WaveInOpen doesnt return meesage MM_WIM_OPEN?
    ... public IntPtr hWaveOut = IntPtr.Zero; ... public uint nAvgBytesPerSec; ... public static extern int waveInOpen(ref IntPtr lphWaveIn, ... IntPtr dwCallback, uint dwInstance, uint dwFlags); ...
    (microsoft.public.dotnet.languages.csharp)