Problem translating 4 lines C# code to C++

S

stax76

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

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

the full code is taken from the following article:

http://msdn.microsoft.com/en-us/library/ms996387.aspx
 
E

Erik Wikström

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

byte[] bRawData = new byte[raw.hid.dwSizHid];

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.dwSizHid];
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(new IntPtr(pRawData), bRawData, 0, raw.hid.dwSizHid - 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:

http://msdn.microsoft.com/en-us/library/ms632585(VS.85).aspx

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

stax76

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.bRawData[1];
int a2 = 0;

if (raw->data.hid.dwSizeHid == 3)
a2 = raw->data.hid.bRawData[2];

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

Jim Langston

stax76 said:
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.bRawData[1];
int a2 = 0;

if (raw->data.hid.dwSizeHid == 3)
a2 = raw->data.hid.bRawData[2];

Why aren't you just:
a1 = raw->data.hid.bRawData[raw->data.hid.dwSizeHid - 1];

although I'm not sure how you're using a2 given the original algorithm.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top