C# wrapper for the WinPcap Library
-
Hi, I'm trying (only trying) to write a C# wrapper for the WinPcap library. Of course I started with the simplest function: Get the version of the lib. This works fine :-) Second thing: I tried to wrap the PacketGetAdapterNames() function (also pretty simple). Here's the source code: //---------------------------------------------------------------------------- //WinPcap API Source code: BOOLEAN PacketGetAdapterNames(PTSTR pStr,PULONG BufferSize) { HKEY LinkageKey,AdapKey; UINT RegKeySize=0; LONG Status; ULONG Result; PTSTR BpStr; char *TTpStr,*DpStr,*DescBuf; LPADAPTER adapter; PPACKET_OID_DATA OidData; int i=0,k,rewind,dim; TCHAR AdapName[256]; ODSEx("PacketGetAdapterNames: BufferSize=%d\n",*BufferSize); OidData=GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT,512); if (OidData == NULL) { ... //---------------------------------------------------------------------------- //C#wrapper code I wrote: [DllImport("packet.dll")] extern static bool PacketGetAdapterNames(ref string pStr, ref ulong BufferSize); //---------------------------------------------------------------------------- The function always returns false and the ref param variables are never updated :-((. I have got a sample C app with source code, that uses the same API. This app works fine... //---------------------------------------------------------------------------- WCHAR AdapterName[8192]; // string that contains a list of the network adapters AdapterLength=4096; if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE) { printf("Unable to retrieve the list of the adapters!\n"); return -1; } temp=AdapterName; temp1=AdapterName; while ((*temp!='\0')||(*(temp-1)!='\0')) {... ----------------------------------------------------------------------------------------- Has anybody got an idea?? I'm not very familiar with these kind of API calls. G
-
Hi, I'm trying (only trying) to write a C# wrapper for the WinPcap library. Of course I started with the simplest function: Get the version of the lib. This works fine :-) Second thing: I tried to wrap the PacketGetAdapterNames() function (also pretty simple). Here's the source code: //---------------------------------------------------------------------------- //WinPcap API Source code: BOOLEAN PacketGetAdapterNames(PTSTR pStr,PULONG BufferSize) { HKEY LinkageKey,AdapKey; UINT RegKeySize=0; LONG Status; ULONG Result; PTSTR BpStr; char *TTpStr,*DpStr,*DescBuf; LPADAPTER adapter; PPACKET_OID_DATA OidData; int i=0,k,rewind,dim; TCHAR AdapName[256]; ODSEx("PacketGetAdapterNames: BufferSize=%d\n",*BufferSize); OidData=GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT,512); if (OidData == NULL) { ... //---------------------------------------------------------------------------- //C#wrapper code I wrote: [DllImport("packet.dll")] extern static bool PacketGetAdapterNames(ref string pStr, ref ulong BufferSize); //---------------------------------------------------------------------------- The function always returns false and the ref param variables are never updated :-((. I have got a sample C app with source code, that uses the same API. This app works fine... //---------------------------------------------------------------------------- WCHAR AdapterName[8192]; // string that contains a list of the network adapters AdapterLength=4096; if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE) { printf("Unable to retrieve the list of the adapters!\n"); return -1; } temp=AdapterName; temp1=AdapterName; while ((*temp!='\0')||(*(temp-1)!='\0')) {... ----------------------------------------------------------------------------------------- Has anybody got an idea?? I'm not very familiar with these kind of API calls. G
A long in Win32 API is 32-bits, a long in .NET is 64 bits while an int is 32 bits. If the string being passed in is a buffer (looks like it is) you should also be using StringBuilder instead of string. With those two changes I come up with
[DllImport("packet.dll")] extern static bool PacketGetAdapterNames(StringBuilder pStr, ref uint BufferSize);
void GetAdapterNames() { System.Text.StringBuilder AdapterNames = new System.Text.StringBuilder(bufferSize); uint bufSize = (uint) bufferSize; PacketGetAdapterNames(AdapterNames, ref bufSize); }
I don't think ref is needed on the buffer because it will be passed like a char array to the function by the marshaler, putting ref on the buffer would be equivelant to WCHAR** in C/C++. HTH, James Simplicity Rules! -
A long in Win32 API is 32-bits, a long in .NET is 64 bits while an int is 32 bits. If the string being passed in is a buffer (looks like it is) you should also be using StringBuilder instead of string. With those two changes I come up with
[DllImport("packet.dll")] extern static bool PacketGetAdapterNames(StringBuilder pStr, ref uint BufferSize);
void GetAdapterNames() { System.Text.StringBuilder AdapterNames = new System.Text.StringBuilder(bufferSize); uint bufSize = (uint) bufferSize; PacketGetAdapterNames(AdapterNames, ref bufSize); }
I don't think ref is needed on the buffer because it will be passed like a char array to the function by the marshaler, putting ref on the buffer would be equivelant to WCHAR** in C/C++. HTH, James Simplicity Rules! -
A long in Win32 API is 32-bits, a long in .NET is 64 bits while an int is 32 bits. If the string being passed in is a buffer (looks like it is) you should also be using StringBuilder instead of string. With those two changes I come up with
[DllImport("packet.dll")] extern static bool PacketGetAdapterNames(StringBuilder pStr, ref uint BufferSize);
void GetAdapterNames() { System.Text.StringBuilder AdapterNames = new System.Text.StringBuilder(bufferSize); uint bufSize = (uint) bufferSize; PacketGetAdapterNames(AdapterNames, ref bufSize); }
I don't think ref is needed on the buffer because it will be passed like a char array to the function by the marshaler, putting ref on the buffer would be equivelant to WCHAR** in C/C++. HTH, James Simplicity Rules!James, Thanks again. I've got another question. Any idea how I can make a struct in C# that is the equivalent of: ?? typedef struct ADAPTER { HANDLE hFile; TCHAR SymbolicLink[MAX_LINK_NAME_LENGTH]; int NumWrites; HANDLE ReadEvent; UINT ReadTimeOut; }; typedef struct PACKET { PVOID Buffer; UINT Length; UINT ulBytesReceived; BOOLEAN bIoComplete; }; Thanks, G
-
James, Thanks again. I've got another question. Any idea how I can make a struct in C# that is the equivalent of: ?? typedef struct ADAPTER { HANDLE hFile; TCHAR SymbolicLink[MAX_LINK_NAME_LENGTH]; int NumWrites; HANDLE ReadEvent; UINT ReadTimeOut; }; typedef struct PACKET { PVOID Buffer; UINT Length; UINT ulBytesReceived; BOOLEAN bIoComplete; }; Thanks, G
Untested, but might work :)
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct ADAPTER { public IntPtr hFile; public StringBuilder SymbolicLink; public int NumWrites; public IntPtr ReadEvent; public uint ReadTimeOut; } [StructLayout(LayoutKind.Sequential)] public struct PACKET { public byte [] Buffer; public uint Length; public uint ulBytesReceived; public bool bIoComplete; }
Good Luck :) James Simplicity Rules! -
Untested, but might work :)
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct ADAPTER { public IntPtr hFile; public StringBuilder SymbolicLink; public int NumWrites; public IntPtr ReadEvent; public uint ReadTimeOut; } [StructLayout(LayoutKind.Sequential)] public struct PACKET { public byte [] Buffer; public uint Length; public uint ulBytesReceived; public bool bIoComplete; }
Good Luck :) James Simplicity Rules!James, (Not knowing anything about the WinPcap Library) Explain to me why using a StringBuilder in place of the TCHAR SymbolicLink[MAX_LINK_NAME_LENGTH] array would work here? Wouldn't a variation of something like the following be the right choice? [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=MAX_LINK_NAME_LENGTH )] public String SymbolicLink; Thanks, Neil
-
James, (Not knowing anything about the WinPcap Library) Explain to me why using a StringBuilder in place of the TCHAR SymbolicLink[MAX_LINK_NAME_LENGTH] array would work here? Wouldn't a variation of something like the following be the right choice? [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=MAX_LINK_NAME_LENGTH )] public String SymbolicLink; Thanks, Neil
I don't know :-P I originally had written what you had, then switched it to StringBuilder. James Simplicity Rules!
-
I don't know :-P I originally had written what you had, then switched it to StringBuilder. James Simplicity Rules!
I am confident that the attributed string as I presented, is the right choice. The StringBuilder reference is the size of a pointer, in contrast to the TCHAR array being n TCHAR(s) in length (+ alignment padding if necessary), directly affecting the size of the overall structure and the offsets of its members. Regards