Solved

Issue with handle, and function

Posted on 2007-12-05
11
410 Views
Last Modified: 2008-02-01
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
Comment
Question by:mileyja
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> 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
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
>>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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
The structure contents, including the handler, comes from previous calls to other Aircap library calls
0
 

Author Comment

by:mileyja
Comment Utility
ok im looking into this
0
 

Author Comment

by:mileyja
Comment Utility
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
 

Accepted Solution

by:
mileyja earned 0 total points
Comment Utility

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
 
LVL 1

Expert Comment

by:Computer101
Comment Utility
Closed, 500 points refunded.
Computer101
EE Admin
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now