Solved

Issue with handle, and function

Posted on 2007-12-05
11
422 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20417591
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
ID: 20417608
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
ID: 20418725
>>>> 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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20418907
>>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
ID: 20419119
>>>> 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
 
LVL 55

Expert Comment

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

Author Comment

by:mileyja
ID: 20431036
ok im looking into this
0
 

Author Comment

by:mileyja
ID: 20434585
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
ID: 20434599

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
ID: 20605516
Closed, 500 points refunded.
Computer101
EE Admin
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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 switch statements in the C programming language.

729 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