473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.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
Jul 31 '08 #1
3 1926
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.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/libr...85(VS.85).aspx

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

--
Erik Wikström
Jul 31 '08 #2
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)
{
...
}
Aug 1 '08 #3

"stax76" <fr*********@yahoo.dewrote in message
news:c0**********************************@k37g2000 hsf.googlegroups.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.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.
switch(a1 | a2 << 8)
{
...
}

Aug 1 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jax | last post by:
I am using a class that inherits from the DataGridTextBoxColumn. It adds a combo box into the column where it displays a selection of choices. The problem I have is that when this comboBox loses...
10
by: A.M | last post by:
Hi, I am having difficulty with shell scripting in Python. I use the following command to run a DOS command and put the return value in a Python variable:
9
by: lombardm | last post by:
I am trying to decipher/translate some code that I have no experience with. I am trying to find out how the checksum is computed for a Megellan Explorist GPS Waypoint (POI) file. Below is the...
5
by: Andrus | last post by:
I have database containing translations. I'm creating VS 2005 WinForms application which should use this database to translate menu items to user language. I replaced lines in myform.designer.cs...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.