Solved

Issue with handle, and function

Posted on 2007-12-05
11
418 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
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

856 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