Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. unsigned chars

unsigned chars

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    packetlos
    wrote on last edited by
    #1

    Hi guys, I am trying to copy into a struct the contents of a buffer returned from the winpcap library, the libray function call returns a pointer to the buffer like the following type: const UCHAR *pkt_data I am trying to declare something to hold the data where // Packet data ??? is.

    typedef struct DATAGRAM
    {
    	UINT packetID;			// Sequence Number
    	ip_header ipHeader;		// IP header
    	pcap_pkthdr wpHeader;	        // Winpcap header
    	unsigned char pkt_data[];	// Packet data ???
    } DATAGRAM;
    

    I would prefer to keep it as UCHARS because it makes it easier to parse the data later on into headers such as tcp, so my question is how should declare the type in the struct and how can i copy the data in from the buffer?? Cheers Packetlos

    J 1 Reply Last reply
    0
    • P packetlos

      Hi guys, I am trying to copy into a struct the contents of a buffer returned from the winpcap library, the libray function call returns a pointer to the buffer like the following type: const UCHAR *pkt_data I am trying to declare something to hold the data where // Packet data ??? is.

      typedef struct DATAGRAM
      {
      	UINT packetID;			// Sequence Number
      	ip_header ipHeader;		// IP header
      	pcap_pkthdr wpHeader;	        // Winpcap header
      	unsigned char pkt_data[];	// Packet data ???
      } DATAGRAM;
      

      I would prefer to keep it as UCHARS because it makes it easier to parse the data later on into headers such as tcp, so my question is how should declare the type in the struct and how can i copy the data in from the buffer?? Cheers Packetlos

      J Offline
      J Offline
      John R Shaw
      wrote on last edited by
      #2

      This "unsigned char pkt_data[];" should not pass the compiler. Insted the stucture should look something like this:

      typedef struct DATAGRAM{
      UINT packetID; // Sequence Number
      ip_header ipHeader; // IP header
      pcap_pkthdr wpHeader; // Winpcap header
      unsigned char pkt_data[1]; // Packet data ???
      } DATAGRAM;

      Then if you know if you know the nuber of bytes you will be receiving you can allocate the need memory like so: DATAGRAM* pDatat = (DATAGRAM*)malloc(sizeof(DATAGRAM) + nDataSize) or the C++ equivalent to allocate the need memory and then copy the data into pData->pkt_data as you see fit. If you do not know the data size ahead of time things get a lot more complicated and you may want to use one of the STL templates or if programming using MFC you could replace "unsigned char pkt_data[1];" with "CByteArray pkt_data;". CByteArray stores unsigned chars, because that is what a byte is. Good Luck1 INTP

      P 1 Reply Last reply
      0
      • J John R Shaw

        This "unsigned char pkt_data[];" should not pass the compiler. Insted the stucture should look something like this:

        typedef struct DATAGRAM{
        UINT packetID; // Sequence Number
        ip_header ipHeader; // IP header
        pcap_pkthdr wpHeader; // Winpcap header
        unsigned char pkt_data[1]; // Packet data ???
        } DATAGRAM;

        Then if you know if you know the nuber of bytes you will be receiving you can allocate the need memory like so: DATAGRAM* pDatat = (DATAGRAM*)malloc(sizeof(DATAGRAM) + nDataSize) or the C++ equivalent to allocate the need memory and then copy the data into pData->pkt_data as you see fit. If you do not know the data size ahead of time things get a lot more complicated and you may want to use one of the STL templates or if programming using MFC you could replace "unsigned char pkt_data[1];" with "CByteArray pkt_data;". CByteArray stores unsigned chars, because that is what a byte is. Good Luck1 INTP

        P Offline
        P Offline
        packetlos
        wrote on last edited by
        #3

        Hi John, Thanks for taking time to reply, CByteArray seems to be exactly what I need, I can calculate the size of the data and set the CByteArray like this: tempDatagram.pkt_data.SetSize(dataLen); But then how do I copy the data into the array using the given pointer to the buffer of UCHARS?

        P 1 Reply Last reply
        0
        • P packetlos

          Hi John, Thanks for taking time to reply, CByteArray seems to be exactly what I need, I can calculate the size of the data and set the CByteArray like this: tempDatagram.pkt_data.SetSize(dataLen); But then how do I copy the data into the array using the given pointer to the buffer of UCHARS?

          P Offline
          P Offline
          packetlos
          wrote on last edited by
          #4

          Sorted with: MoveMemory(tempDatagram.pkt_data.GetData(), pkt_data, dataLen);

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups