[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 426
  • Last Modified:

Issue with handle, and function

this call succeeds:
#region AirpcapGetReadEvent

        [DllImport("airpcap.dll", EntryPoint = "AirpcapGetReadEvent", CharSet = CharSet.Ansi)]
        public extern static int AirpcapGetReadEvent(IntPtr ipAdapterHandle, ref IntPtr readevent);

but that gets me the pointer, for the life of me I cant figure out what the actual object is supposed to be on the c# side of things. (I.E.. what do i marshall this to.  

This is the actual source code for the call:

BOOL AirpcapGetReadEvent(PAirpcapHandle AdapterHandle, HANDLE* PReadEvent)
{
      *PReadEvent = AdapterHandle->ReadEvent;

      if(AdapterHandle->ReadEvent == NULL)
      {
            return FALSE;
      }

      return TRUE;
}
returns true if successsful, it works fine, but that gives me a non-zero pointer.

I want to use the whole thing in this context:





Here is this code for the read function:
BOOL AirpcapRead(PAirpcapHandle AdapterHandle, PBYTE BufferToFill, UINT BufSize, PUINT ReceievedBytes)
{
      if(!ReadFile(AdapterHandle->OsHandle, BufferToFill, BufSize, (LPDWORD)ReceievedBytes, NULL))
      {
            _snprintf(AdapterHandle->Ebuf, AIRPCAP_ERRBUF_SIZE, "Error %d trying to read data\n", GetLastError());
            return FALSE;
      }

      return TRUE;
}

The code below is for the code to the call above

What are your thoughts??
This the context i want at some point to use
	if(!AirpcapGetReadEvent(Ad, &ReadEvent))
	{
		printf("Error getting the read event: %s\n", AirpcapGetLastError(Ad));
		AirpcapClose(Ad);
		return -1;
	}
 
	//
	// Allocate a 256k packet buffer
	//
	PacketBuffer = (BYTE*)malloc(PACKET_BUFFER_SIZE);
	if(!PacketBuffer)
	{
		printf("No memory for the packet buffer\n");
		AirpcapClose(Ad);
		return -1;
	}
 
	//
	// Everything ok! 
	// Loop forever printing the packets
	//
	while(TRUE)
	{
	    // capture the packets
		if(!AirpcapRead(Ad, 
			PacketBuffer, 
			PACKET_BUFFER_SIZE, 
			&BytesReceived))
		{
			printf("Error receiving packets: %s\n", AirpcapGetLastError(Ad));
			free(PacketBuffer);
			AirpcapClose(Ad);
			return -1;
		}
 
		// parse the buffer and print the packets
		PrintPackets(PacketBuffer, BytesReceived);
 
		// wait until some packets are available. This prevents polling and keeps the CPU low. 
		WaitForSingleObject(ReadEvent, WAIT_INTERVAL_MS);
	}
 
	return 0;
}

Open in new window

0
mileyja
Asked:
mileyja
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Jaime OlivaresCommented:
Well, the function AirpcapGetReadEvent does almost nothing, just copy a handle from the PAirpcapHandle structure
to an independent variable, and return true/false according if the handle is null or not. I don't see any benefit to use it, maybe just some legibility.
0
 
Jaime OlivaresCommented:
this code portion:
      if(!AirpcapGetReadEvent(Ad, &ReadEvent))
      {
            printf("Error getting the read event: %s\n", AirpcapGetLastError(Ad));
            AirpcapClose(Ad);
            return -1;
      }
 
can be translated to C# without using AirpcapGetReadEvent():

               IntPtr ReadEvent = Ad.ReadEvent;
      if(ReadEvent == null)
      {
            // error message here
            AirpcapClose(Ad);
            return -1;
      }
0
 
itsmeandnobodyelseCommented:
>>>> IntPtr ReadEvent = Ad.ReadEvent;

As the variable 'Ad' in C# was the argument passed as 'IntPtr ipAdapterHandle' to C++, I would assume that the above statement doesn't compile because IntPtr hardly has a member 'ReadEvent'. Or did I miss some information?

The C++ func may *do almost nothing* as Jaime tells correctly but at least it will return the 'ReadEvent' member of struct AirpcapHandle, what is not impossible to do that in C# but in case 'ReadEvent' is not the very first member of the struct you have to make a lot of assumptions about the size of the previous members and about alignment issues, what IMO easier was made in C++ than in C#.

Regards, Alex
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
Jaime OlivaresCommented:
>>The C++ func may *do almost nothing* as Jaime tells correctly but at least it will return the 'ReadEvent' member
>> of struct AirpcapHandle, what is not impossible to do that in C# but in case 'ReadEvent' is not the very first >>member of the struct you have to make a lot of assumptions about the size of the previous members and about >>alignment issues, what IMO easier was made in C++ than in C#.

There are lots of previous questions of the author about this. He has the structure marshalled in C#
0
 
itsmeandnobodyelseCommented:
>>>> He has the structure marshalled in C#
Ok, but why should  

       Ad.ReadEvent

map to

       AdapterHandle->ReadEvent


if Ad is a C# structure and  AdapterHandle is a (C++) pointer to a (C++) structure.

Moreover, if Ad is a C# structure, how can it be passed as IntPtr to a C++ function?

 
0
 
Jaime OlivaresCommented:
The structure contents, including the handler, comes from previous calls to other Aircap library calls
0
 
mileyjaAuthor Commented:
ok im looking into this
0
 
mileyjaAuthor Commented:
struct _AirpcapHandle
{
      HANDLE OsHandle;                  
      HANDLE ReadEvent;
      UINT Flags;      // Currently unused
      AIRPCAP_ADAPTER_TYPE AdapterType;
      HKEY hKey;
      AirpcapChannelInfo *pChannels;
      ULONG NumChannels;

      CHAR Ebuf[AIRPCAP_ERRBUF_SIZE];
};

this is the airpcaphandle struct does that help
0
 
mileyjaAuthor Commented:

This is my c# translation that seems to come back valid and lets me turn the Led's on the adapter on and off

please tell me if there is anything wrong with this translation and also if we can get that read event code to work and
and how I can get this that function in the snippet at the top of the page to work.  

I wish I had more prior C and C# experience.

 struct AirpcapHandle
        {
            public UInt32 OsHandle;
            public UInt32 ReadEvent;
            public UInt32 Flags;      // Currently unused
            public AIRPCAP_ADAPTER_TYPE AdapterType;
            public UInt32 hKey;
            public IntPtr pChannels;  //  AirpcapChannelInfo *
            public ulong NumChannels;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 512)]
            public string Ebuf;
        }
0
 
Computer101Commented:
Closed, 500 points refunded.
Computer101
EE Admin
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now