Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Issue with handle, and function

Posted on 2007-12-05
11
Medium Priority
?
425 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
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!

 
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

610 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