Problem translating 4 lines C# code to C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stax76

    Problem translating 4 lines C# code to C++

    Hello,
    can anybody tell me what it would look like in C++?

    byte[] bRawData = new byte[raw.hid.dwSizHi d];
    int pRawData = buffer.ToInt32( ) + Marshal.SizeOf( typeof(RAWINPUT )) +
    1; // buffer is of type IntPtr
    Marshal.Copy(ne w IntPtr(pRawData ), bRawData, 0, raw.hid.dwSizHi d - 1);
    int rawData = bRawData[0] | bRawData[1] << 8;

    the full code is taken from the following article:


  • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

    #2
    Re: Problem translating 4 lines C# code to C++

    On 2008-07-31 23:45, stax76 wrote:
    Hello,
    can anybody tell me what it would look like in C++?
    >
    byte[] bRawData = new byte[raw.hid.dwSizHi d];
    Use a char array for bytes, since you will do some bit-shifting you
    probably want unsigned char. But to be safe you should check how
    shifting affects C# bytes, but it seems in this case that the result is
    an integer type larger (more bits) than a byte.

    unsigned char* bRawData = new unsigned char[raw.hid.dwSizHi d];
    int pRawData = buffer.ToInt32( ) + Marshal.SizeOf( typeof(RAWINPUT )) +
    1; // buffer is of type IntPtr
    In you case buffer should be of type unsigned char* or perhaps void*,
    what this code does is getting a pointer which points to some offset
    from the beginning of the buffer containing the data you want. RAWINPUT
    should be a struct or a class.

    unsigned char* pRawData = buffer + sizeof(RAWINPUT ) + 1;
    Marshal.Copy(ne w IntPtr(pRawData ), bRawData, 0, raw.hid.dwSizHi d - 1);
    Looks like they copy the interesting parts from the buffer into the
    array, use memcpy().
    int rawData = bRawData[0] | bRawData[1] << 8;
    Here they combine the values of bRawData[0] and bRawData[1] so that the
    value in bRawData[1] is in bis 8-15 and bRawData[0] is in bits 0-8. This
    line should work as it is.
    the full code is taken from the following article:
    >
    http://msdn.microsoft.com/en-us/library/ms996387.aspx
    Since you are not doing .Net development looking at the .Net
    documentation might not be the best idea, it is better looking at the
    Win32 documentation:

    The user input section describes how user input is captured and used.


    Check the Raw Input section for more information relevant to your problem.

    --
    Erik Wikström

    Comment

    • stax76

      #3
      Re: Problem translating 4 lines C# code to C++

      Thanks, that helped a lot. After trying very hard I've got it working
      but it's not understandable since there is no docu for the hid data
      structure, looks as follows:

      int a1 = raw->data.hid.bRawD ata[1];
      int a2 = 0;

      if (raw->data.hid.dwSiz eHid == 3)
      a2 = raw->data.hid.bRawD ata[2];

      switch(a1 | a2 << 8)
      {
      ...
      }

      Comment

      • Jim Langston

        #4
        Re: Problem translating 4 lines C# code to C++


        "stax76" <frank_skare@ya hoo.dewrote in message
        news:c0c7a5fe-c2c1-467d-8a80-0deaef84d052@k3 7g2000hsf.googl egroups.com...
        Thanks, that helped a lot. After trying very hard I've got it working
        but it's not understandable since there is no docu for the hid data
        structure, looks as follows:
        >
        int a1 = raw->data.hid.bRawD ata[1];
        int a2 = 0;
        >
        if (raw->data.hid.dwSiz eHid == 3)
        a2 = raw->data.hid.bRawD ata[2];
        Why aren't you just:
        a1 = raw->data.hid.bRawD ata[raw->data.hid.dwSiz eHid - 1];

        although I'm not sure how you're using a2 given the original algorithm.
        switch(a1 | a2 << 8)
        {
        ...
        }

        Comment

        Working...