Re: Little Problem

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Angel J. Hernández M (angeljesus14_at_hotmail.com)
Date: 09/13/04


Date: Mon, 13 Sep 2004 16:57:29 -0400

Hi there... That's not the problem...

I've got a code written in C# and works fine but I call a lot of API
functions so I decided to write the low-level stuff in C++. My problem is
(not about __pin cuz I already try that). I have a struct which is
responsible for storing packet's IP Header. It's defined like this...

            [StructLayout(LayoutKind::Explicit)]
             __value struct IPHeader {
                 [FieldOffset(0)] Byte verlen; // IP Version & IP Header
length
                 [FieldOffset(1)] Byte tos; // Type of service
                 [FieldOffset(2)] UInt16 length; // Packet's total length
                 [FieldOffset(4)] UInt16 id; // Unique identifier
                 [FieldOffset(6)] UInt16 offset; // Flags & Offset
                 [FieldOffset(8)] Byte ttl; // Time To Live
                 [FieldOffset(9)] Byte protocol; // Protocol (TCP, UDP, any
other)
                 [FieldOffset(10)] UInt16 checksum; // IP Header checksum
                 [FieldOffset(12)] Int64 source; // Source address
                 [FieldOffset(16)] Int64 destination; // Destination address
                 };

What I want to do (like I did in C# code) is... Fill all of the structs
fields by using a pointer... In C# I have this code

 protected virtual unsafe object[] ParseIPHeader(byte[] packet, int length)
{
    short src_port = 0, dst_port = 0;
    object[] retval = new object[6];

    fixed(byte* packetdata = packet) {
     CCommon.IPHeader* ipheader = (CCommon.IPHeader*) packetdata;
    // Rest of code goes here...
}

As you can see I fix my pointer and it's initialized with packet's address.
In C# I don't have to use the & operator for an operation like the one shown
above. My problem is that... I want to get the same result in C++.

Object* CRawSocket::ParseIPHeader(Byte packet[], int length)[] {
     short src_port = 0, dst_port = 0;
     System::Text::StringBuilder *src_address, *dst_address;
     Object *retval[] = new Object*[6];
     src_address = new System::Text::StringBuilder();
     dst_address = new System::Text::StringBuilder();

     Byte *packeddata = __try_cast< Byte *>(&packet[0]) ;
     CCommon::IPHeader *ipheader = (CCommon::IPHeader *) &packeddata;
     // Rest of code goes here...
}

I open two instances of VS Debugger (C# & C++ projects). The ipheader points
to packeddata which in turn points to the first element of the array
packet. This works perfectly in C# but it doesn't seem to work in C++. When
I compare both structures (C# & C++) the information contaned in them is
totally different when the data received from the socket is the same.

TIA

"Ben Schwehn" <b.schwehn@gmx.net> escribió en el mensaje
news:%23PzpuCdmEHA.744@TK2MSFTNGP10.phx.gbl...
>
> > fixed(byte* packetdata = &packet) // wrong!
> >
> > but in the C++ world I need to pass the address
> >
> > I've tried this...
>
> I'm not entirly sure what you're trying to do but instead of c#'s fixed
> what you'll probably want to do is use __pin something like
>
> int __pin* p = &packet[0];
>
> to get the address of packet and also pin the entire array
>
> hth
> --
> Ben
> http://bschwehn.de



Relevant Pages

  • Re: how to store list of varying types
    ... represent that with those in the struct definitions? ... pointer null, and the second one the CString object. ... then have to finish constructing the packet by copying the two data objects ... start with an upper-case letter (so, I would use DataElement ...
    (microsoft.public.vc.mfc)
  • Re: how to store list of varying types
    ... When there's a variable-length string, ... typedef struct { ... pointer null, and the second one the CString object. ... then have to finish constructing the packet by copying the two data objects ...
    (microsoft.public.vc.mfc)
  • Re: Passing an array of structuresfrom a pointer?
    ... an array of struct. ... You cannot assign a pointer to struct to a long* ... to only declare structs in headers and then define the ...
    (microsoft.public.vc.language)
  • Re: Looking for a more elegant way to do memory offsets
    ... The mallocfunction returns memory that is "suitably aligned" ... >which in it contains the pointer to the first element in the linked ... and here a "struct dem_mem" does not contain a pointer to another ... This is not a linked-list access, but rather an array access: ...
    (comp.lang.c)
  • Re: Passing an array of structuresfrom a pointer?
    ... to only declare structs in headers and then define the ... the struct should be declared ... what if you have a simple array like this: ... It also returned the value of the pointer. ...
    (microsoft.public.vc.language)