?
=?ISO-8859-1?Q?Marc-Aur=E8le_Brothier?=
Hi,
I have a DLL in C++ that I want to use in my C# program on my PocketPC
2003. The DLL first creates the structure and initializes the structure,
I have not problem to get the structure back in my C# program. But when
I pass this structure as an argument in a function, I got an error:
"NotSupportedException"
I will be glad if someone can help me. I read a lof of different post
about Marshalling but didn't find the answer.
/Marco
------------------------------
Here is the C++ code:
typedef struct _ADAPTER {
UINT nWrites;
HANDLE hFile;
HANDLE ReadEvent;
} ADAPTER, *LPADAPTER;
LPADAPTER PacketOpenAdapter(LPTSTR AdapterName);
BOOLEAN PacketSetHwFilter(LPADAPTER AdapterObject,ULONG Filter);
------------------------------
Here is the C# code:
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct ADAPTER
{
public uint nWrites;
public IntPtr hFile;
public IntPtr ReadEvent;
}
[ DllImport( "Packet32.dll", SetLastError=true,
EntryPoint="PacketOpenAdapter" ) ]
private static extern IntPtr DllPacketOpenAdapter(String sb);
[ DllImport( "Packet32.dll", SetLastError=true,
EntryPoint="PacketSetHwFilter" ) ]
private static extern bool DllPacketSetHwFilter(ref IntPtr pHandle,
ulong filter);
public bool OpenAdapter(string name)
{
IntPtr tmp = DllPacketOpenAdapter(name);
adapterHandle = (Packet32.ADAPTER)Marshal.PtrToStructure(tmp,
typeof(Packet32.ADAPTER));
if (adapterHandle.hFile != IntPtr.Zero)
{
adapterName = name;
return true;
}
return false;
}
public bool SetHwFilter(ulong filter)
{
IntPtr tmp;
int sizeStruct = Marshal.SizeOf(typeof(Packet32.ADAPTER));
tmp = LocalAlloc(MemoryAllocFlags.LPTR,
(uint)Marshal.SizeOf(typeof(Packet32.ADAPTER)));
try
{
Marshal.StructureToPtr(adapterHandle, tmp, false);
bool res = DllPacketSetHwFilter(ref tmp, filter);
Marshal.PtrToStructure(tmp, adapterHandle);
LocalFree(tmp);
return res;
}
catch(Exception e) {
string msg = e.Message;
LocalFree(tmp);
}
return false;
}
I have a DLL in C++ that I want to use in my C# program on my PocketPC
2003. The DLL first creates the structure and initializes the structure,
I have not problem to get the structure back in my C# program. But when
I pass this structure as an argument in a function, I got an error:
"NotSupportedException"
I will be glad if someone can help me. I read a lof of different post
about Marshalling but didn't find the answer.
/Marco
------------------------------
Here is the C++ code:
typedef struct _ADAPTER {
UINT nWrites;
HANDLE hFile;
HANDLE ReadEvent;
} ADAPTER, *LPADAPTER;
LPADAPTER PacketOpenAdapter(LPTSTR AdapterName);
BOOLEAN PacketSetHwFilter(LPADAPTER AdapterObject,ULONG Filter);
------------------------------
Here is the C# code:
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct ADAPTER
{
public uint nWrites;
public IntPtr hFile;
public IntPtr ReadEvent;
}
[ DllImport( "Packet32.dll", SetLastError=true,
EntryPoint="PacketOpenAdapter" ) ]
private static extern IntPtr DllPacketOpenAdapter(String sb);
[ DllImport( "Packet32.dll", SetLastError=true,
EntryPoint="PacketSetHwFilter" ) ]
private static extern bool DllPacketSetHwFilter(ref IntPtr pHandle,
ulong filter);
public bool OpenAdapter(string name)
{
IntPtr tmp = DllPacketOpenAdapter(name);
adapterHandle = (Packet32.ADAPTER)Marshal.PtrToStructure(tmp,
typeof(Packet32.ADAPTER));
if (adapterHandle.hFile != IntPtr.Zero)
{
adapterName = name;
return true;
}
return false;
}
public bool SetHwFilter(ulong filter)
{
IntPtr tmp;
int sizeStruct = Marshal.SizeOf(typeof(Packet32.ADAPTER));
tmp = LocalAlloc(MemoryAllocFlags.LPTR,
(uint)Marshal.SizeOf(typeof(Packet32.ADAPTER)));
try
{
Marshal.StructureToPtr(adapterHandle, tmp, false);
bool res = DllPacketSetHwFilter(ref tmp, filter);
Marshal.PtrToStructure(tmp, adapterHandle);
LocalFree(tmp);
return res;
}
catch(Exception e) {
string msg = e.Message;
LocalFree(tmp);
}
return false;
}