Click here to Skip to main content
16,017,638 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi all,

I have a question about implement the following C code in C#

basically,

I have a struct
C++
//*****************************************************************************
//
// This structure defines the fields in a BOOTP request/reply packet.
//
//*****************************************************************************
typedef struct
{
    //
    // The operation; 1 is a request, 2 is a reply.
    //
    unsigned char ucOp;

    //
    // The hardware type; 1 is Ethernet.
    //
    unsigned char ucHType;

    //
    // The hardware address length; for Ethernet this will be 6, the length of
    // the MAC address.
    //
    unsigned char ucHLen;

    //
    // Hop count, used by gateways for cross-gateway booting.
    //
    unsigned char ucHops;

    //
    // The transaction ID.
    //
    unsigned long ulXID;

    //
    // The number of seconds elapsed since the client started trying to boot.
    //
    unsigned short usSecs;

    //
    // The BOOTP flags.
    //
    unsigned short usFlags;

    //
    // The client's IP address, if it knows it.
    //
    unsigned long ulCIAddr;

    //
    // The client's IP address, as assigned by the BOOTP server.
    //
    unsigned long ulYIAddr;

    //
    // The TFTP server's IP address.
    //
    unsigned long ulSIAddr;

    //
    // The gateway IP address, if booting cross-gateway.
    //
    unsigned long ulGIAddr;

    //
    // The hardware address; for Ethernet this is the MAC address.
    //
    unsigned char pucCHAddr[16];

    //
    // The name, or nickname, of the server that should handle this BOOTP
    // request.
    //
    char pcSName[64];

    //
    // The name of the boot file to be loaded via TFTP.
    //
    char pcFile[128];

    //
    // Optional vendor-specific area; not used for BOOTP.
    //
    unsigned char pucVend[64];
}
tBOOTPPacket;


then, C code created a pointer to an byte array. the pointer type is this tBOOTPPacket struct.

C++
unsigned char pcPacketData[700];
tBOOTPPacket *pPacket;

pPacket = (tBOOTPPacket *)pcPacketData;


how to do this in C# .NET4.5

Thanks in advance
Posted

This is kind of basic C# stuff, especially in regard to referencing your struct and the items in your struct. You should be able to make the conversions rather easily. If this is difficult for you to do, you really need to learn a little more about C#.

However, here is a nice tutorial at MSDN for using and referencing structs and it covers the important differences between classes and structs. Structs Tutorial[^]

Good luck.
 
Share this answer
 
Comments
Jochen Arndt 10-Feb-14 11:12am    
+5.
But I see a problem arising that is not handled by your link:
The structure will contain padding bytes and the shown C structure is for the BOOTP protocol which is packed (this must be also considered in C).

C# structure packing is described here: http://www.developerfusion.com/article/84519/mastering-structs-in-c/
S Houghtelin 10-Feb-14 11:22am    
That is good information. Good catch on the protocol too.
Member 10528344 10-Feb-14 11:45am    
Hi Permalink,
Thank you for your reply.

my question is not how to create a struct in C#.

the c code is using a tBOOTPPacket type pointer point to an array. this basically interpreting the array as a BOOTP request.

I have no idea how to achieve it in C#. I create a struct or a class in c#. how do I interpret an array into tBOOTPPacket struct or class?

Thank you
Jochen Arndt 10-Feb-14 13:01pm    
Have a look at this CP article: http://www.codeproject.com/Articles/25896/Reading-Unmanaged-Data-Into-Structures
Member 10528344 10-Feb-14 14:33pm    
beautiful! exactly what i am looking for

Thanks
I did some research, Apparently, I may need to look at Marshal.PtrToStructure<t> Method (IntPtr, T)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900