Link to home
Start Free TrialLog in
Avatar of camster123
camster123

asked on

How do I cast a pointer to a C++ struct with data members to a pointer to a C++ struct which has no data members

How do I cast a pointer to a C++ struct with data members  to a pointer to a C++ struct which has no data members such as:
struct libusb_device_handle?

   For example , On windows 7 , I would like to  cast a C++ pointer to   HRAWINPUT   to a C++ pointer  to a   C++ struct which has no data member such as libusb_device_handle
 
     The reason I want to do this is shown here:

   
I have already  opened the CH Products VM Desktop joystick device via libusb_open()
Then I would like to do some magic (meaning polling for events) such as invoking GetRawInputData()
to record the x and y coordinates when we move the joystick vigorously.
     Any help is greatly appreciated.
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi camster,

I'm not sure that's exactly what you want, but just recasting on the fly should suffice.  Note that the compiler's warn/alert options may be triggered, but you should be able to override them.

A libusb_device_handle should be an integer, not an address.  The struct pointer HRAWINPUT is an address to a structure.  What you're asking to do is very non-standard and will give you the desired results only if the first item in the structure is the handle.

  libusb_device_handle = (int *)HRAWINPUT;

That will take the first 32 (or 64) bytes in the structure and copy the contests into libusb_device_handle.  If the first item in the structure is the device handle, you can do the same in a much more direct fashion with:

    libusb_device_handle = HRAWINPUT->item1;


Kent
Avatar of camster123
camster123

ASKER

HI Kent,
    I thought your  solution was very elegant and well explained.
   You explain how to cast a HRAWINPUT struct pointer to a  libusb_device_handle pointer.
    How do I  cast  a  libusb_device_handle pointer to an a HRAWINPUT struct pointer?
         Frank
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Kent made an very useful solution to an important question.