Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# wrapper for the WinPcap Library

C# wrapper for the WinPcap Library

Scheduled Pinned Locked Moved C#
csharpsysadminjsonquestionannouncement
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HG
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • H HG

      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

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      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!

      H 2 Replies Last reply
      0
      • J James T Johnson

        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!

        H Offline
        H Offline
        HG
        wrote on last edited by
        #3

        You're the man! Thanks, it worked. G:-D

        1 Reply Last reply
        0
        • J James T Johnson

          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!

          H Offline
          H Offline
          HG
          wrote on last edited by
          #4

          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

          J 1 Reply Last reply
          0
          • H HG

            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

            J Offline
            J Offline
            James T Johnson
            wrote on last edited by
            #5

            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!

            N 1 Reply Last reply
            0
            • J James T Johnson

              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!

              N Offline
              N Offline
              Neil Van Note
              wrote on last edited by
              #6

              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

              J 1 Reply Last reply
              0
              • N Neil Van Note

                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

                J Offline
                J Offline
                James T Johnson
                wrote on last edited by
                #7

                I don't know :-P I originally had written what you had, then switched it to StringBuilder. James Simplicity Rules!

                N 1 Reply Last reply
                0
                • J James T Johnson

                  I don't know :-P I originally had written what you had, then switched it to StringBuilder. James Simplicity Rules!

                  N Offline
                  N Offline
                  Neil Van Note
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups