Marhsalling a structure

  • Thread starter =?ISO-8859-1?Q?Marc-Aur=E8le_Brothier?=
  • Start date
?

=?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;
}
 
A

Alex Yakhnin [MVP]

Try to change:
private static extern bool DllPacketSetHwFilter(ref IntPtr
pHandle,
ulong filter);

to:

private static extern bool DllPacketSetHwFilter(IntPtr
pHandle, uint filter);

HTH... Alex
 
?

=?ISO-8859-1?Q?Marc-Aur=E8le_Brothier?=

Hi,

Thank you very much, it's working.
Why the ulong doesn't work? (I tried first without this modification)

Many thanks
/Marco
Try to change:
private static extern bool DllPacketSetHwFilter(ref IntPtr
pHandle,
ulong filter);

to:

private static extern bool DllPacketSetHwFilter(IntPtr
pHandle, uint filter);

HTH... Alex

-----Original Message-----
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;
}

.
 
P

Paul G. Tobey [eMVP]

ulong is 64 bits in the .NET environment.

Paul T.

Marc-Aurèle Brothier said:
Hi,

Thank you very much, it's working.
Why the ulong doesn't work? (I tried first without this modification)

Many thanks
/Marco
Try to change:
private static extern bool DllPacketSetHwFilter(ref IntPtr
pHandle,
ulong filter);

to:

private static extern bool DllPacketSetHwFilter(IntPtr
pHandle, uint filter);

HTH... Alex

-----Original Message-----
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;
}

.
 
G

Geoff Schwab [MSFT]

Hi Marco,

The following FAQ link has some good information on this exception and even
refers to your exact problem. This may be useful for future reference for
you.

6.15. Why do I get a MissingMethodException when I call a function from a
native DLL?
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/faq/default.aspx#6.15

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
Marc-Aurèle Brothier said:
Hi,

Thank you very much, it's working.
Why the ulong doesn't work? (I tried first without this modification)

Many thanks
/Marco
Try to change:
private static extern bool DllPacketSetHwFilter(ref IntPtr
pHandle,
ulong filter);

to:

private static extern bool DllPacketSetHwFilter(IntPtr
pHandle, uint filter);

HTH... Alex

-----Original Message-----
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;
}

.
 
Joined
Jul 13, 2008
Messages
1
Reaction score
0
Hi,
I am trying to add wrapper over functions exported by packet.dll. The implementation in this thread give me a start for the .Net C# implementation.
source code:

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct ADAPTER
{
public uint nWrites; //int NumWrites
public IntPtr hFile; //HANDLE hFile
public IntPtr ReadEvent;//HANDLE ReadEvent
}

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct PACKET
{
public IntPtr hEvent; //HANDLE hEvent
public String Buffer;
public uint nLength; //UINT Length
public uint uBytesReceived; //DWORD ulBytesReceived
public bool bIoComplete; //BOOLEAN bIoComplete
}

[ DllImport( "Packet.dll", SetLastError=true,EntryPoint="PacketOpenAdapter" ) ]
public static extern IntPtr DllPacketOpenAdapter(String sb);

[ DllImport( "Packet.dll", SetLastError=true,EntryPoint="PacketCloseAdapter" ) ]
public static extern void DllPacketCloseAdapter(ref IntPtr pHandle);

[ DllImport( "Packet.dll", SetLastError=true,EntryPoint="PacketSetHwFilter" ) ]
public static extern bool DllPacketSetHwFilter(ref IntPtr pHandle,uint filter);

[ DllImport( "Packet.dll", SetLastError=true,EntryPoint="PacketSetBuff" ) ]
public static extern bool DllPacketSetBuff(ref IntPtr pHandle,int buffsize);

[ DllImport( "Packet.dll", SetLastError=true,EntryPoint="PacketSetReadTimeout" ) ]
public static extern bool DllPacketSetReadTimeout(ref IntPtr pHandle,int timeOut);

[ DllImport( "Packet.dll", SetLastError=true,EntryPoint="PacketAllocatePacket" ) ]
public static extern IntPtr DllPacketAllocatePacket();

[ DllImport( "Packet.dll", SetLastError=true,EntryPoint="PacketInitPacket" ) ]
public static extern void DllPacketInitPacket(ref IntPtr pHandle, ref String buffer,uint length);

[ DllImport( "Packet.dll", SetLastError=true,EntryPoint="PacketReceivePacket" ) ]
public static extern bool DllPacketReceivePacket(ref IntPtr pHandle, ref IntPtr pPacket, bool sync);

- following function calls have no issue:
DllPacketOpenAdapter and DllPacketSetHwFilter ( returns true)
-but call to DllPacketSetBuff does not return.

are there any issues with marshalling ?
Thanks in advance.
 

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