porcus

I've been working on a project in Visual Studio 2005 in which I could interface with HID-compliant devices. Everything was working great when I was developing under Windows XP (32-bit). However, once I ported my test app to my Windows Vista (64-bit) machine, my app appeared to be dead in the water. On that machine, an interop call to SetupDiEnumDeviceInfo (from setupapi.dll) returns false every single time, even though I know I have devices which would have been enumerated just fine under Windows XP. If I can't even detect a device, then how am I going to do anything else with it

Not able to find any information on any of Microsoft's websites (or anyplace else on the web) about the above problem, I sought another way to go about solving my Vista problem using any other approach. That's when I ran across a helpful article on codeproject.com (http://www.codeproject.com/cs/system/rawinput.asp) which addressed how to use the raw input api to handle multiple keyboards in C#. Thinking that this might help, I downloaded, built, and ran this project on my Vista box. Guess what This was yet another failed attempt... or so I thought. (See my questions and comments at the bottom of the article. (porcus == me)) After some debugging and analysis of certain portions of the code, I was able to figure out that after a call to Marshal.PtrToStructure, a resulting sub-structure (RAWKEYBOARD) was "off" by 7 bytes! All I had to do to get this application to work was to change the structure declaration by inserting 7 bytes to the top/beginning, and everything appeared to work just fine after that! Strange, eh

Does anyone have any idea why either of these 2 issues would be occurring under Vista 64-bit I would really like to get the device enumeration routines working if possible, but I may find some other way to work around that issue. As for having to modify the RAWKEYBOARD structure by 7 bytes in order to get that test app working, I'm totally baffled by that one!! Since I can't find any other documented instance of these kinds of issues anywhere, any help would be much appreciated!

Thanks,
Paul



Re: 64-Bit .NET Framework Development. Lacking HID device support under Vista? Bizarre structure alignment after Marshal.PtrToStructure? What is going on here??

MVP User

I've been working on a project in Visual Studio 2005 in which I could interface with HID-compliant devices. Everything was working great when I was developing under Windows XP (32-bit). However, once I ported my test app to my Windows Vista (64-bit) machine, my app appeared to be dead in the water. On that machine, an interop call to SetupDiEnumDeviceInfo (from setupapi.dll) returns false every single time, even though I know I have devices which would have been enumerated just fine under Windows XP. If I can't even detect a device, then how am I going to do anything else with it

Not able to find any information on any of Microsoft's websites (or anyplace else on the web) about the above problem, I sought another way to go about solving my Vista problem using any other approach. That's when I ran across a helpful article on codeproject.com (http://www.codeproject.com/cs/system/rawinput.asp) which addressed how to use the raw input api to handle multiple keyboards in C#. Thinking that this might help, I downloaded, built, and ran this project on my Vista box. Guess what This was yet another failed attempt... or so I thought. (See my questions and comments at the bottom of the article. (porcus == me)) After some debugging and analysis of certain portions of the code, I was able to figure out that after a call to Marshal.PtrToStructure, a resulting sub-structure (RAWKEYBOARD) was "off" by 7 bytes! All I had to do to get this application to work was to change the structure declaration by inserting 7 bytes to the top/beginning, and everything appeared to work just fine after that! Strange, eh

Does anyone have any idea why either of these 2 issues would be occurring under Vista 64-bit I would really like to get the device enumeration routines working if possible, but I may find some other way to work around that issue. As for having to modify the RAWKEYBOARD structure by 7 bytes in order to get that test app working, I'm totally baffled by that one!! Since I can't find any other documented instance of these kinds of issues anywhere, any help would be much appreciated!

Thanks,
Paul


The code posted on codeproject is a "prototype" of a piece of non portable (and buggy) code.
Both  RAWINPUTHEADER  and RAWINPUT definitions are wrong,
RAWINPUTHEADER is wrong because it assumes wParam (a WPARAM) being an UInt32 while it should be a IntPtr.  (this is the bug)
RAWINPUT is wrong (the fieldoffset(16)) because it wrongly assumes RAWINPUTHEADER being 16 bytes long on all platforms, while may be correct on 32 bit, it's 24 bytes on 64 bit. (this makes code non portable).
Didn't look at the accompanying code, but I wouldn't be surprised to see other similar bugs as well.
 
 
Willy.
 
 




Re: 64-Bit .NET Framework Development. Lacking HID device support under Vista? Bizarre structure alignment after Marshal.PtrToStructure? What is going on here??

porcus

I tried your suggestion, and what do you know ... It worked! I feel so dumb for not having thought about the pointer size issue earlier. But how did you know out that the wParam was a pointer After looking at the documentation on that structure (both before and after your helpful suggestion), I wasn't able to figure that out.

Thanks for drawing attention to this pointer issue. I'll pay more attention to this kind of thing in the future! I don't know yet if this is going to help me understand the problem I was having enumerating HID devices using the interop call to SetupDiEnumDeviceInfo, but I'll have a look at that again to see if anything new catches my attention.

Thanks!




Re: 64-Bit .NET Framework Development. Lacking HID device support under Vista? Bizarre structure alignment after Marshal.PtrToStructure? What is going on here??

MVP User

I tried your suggestion, and what do you know ... It worked! I feel so dumb for not having thought about the pointer size issue earlier. But how did you know out that the wParam was a pointer After looking at the documentation on that structure (both before and after your helpful suggestion), I wasn't able to figure that out.

Thanks for drawing attention to this pointer issue. I'll pay more attention to this kind of thing in the future! I don't know yet if this is going to help me understand the problem I was having enumerating HID devices using the interop call to SetupDiEnumDeviceInfo, but I'll have a look at that again to see if anything new catches my attention.

Thanks!

Well, a good start is "Windows Data Types" in MSDN, here you will see that WPARAM is UINT_PTR which at his turn is a "platform dependent integer" type, that is, its 32 bit on 32 bit windows 64 bit on 64 bit windows. The same applies to HANDLE and HANDLE derived types, they are platform dependent integer values, that means, IntPtr types in .NET.
 
Willy.
 
 
 
 




Re: 64-Bit .NET Framework Development. Lacking HID device support under Vista? Bizarre structure alignment after Marshal.PtrToStructure? What is going on here??

porcus

Cool! That makes sense now. Too bad I didn't know my Windows data types types better the first time around. I'm still basically a novice when it comes to the Win API, but after having a look over the data type declarations on that page, that helps me to better understand what assumptions are not safe to make and what I need to keep in mind when doing any .net interop stuff in the future. Thanks for all your help!