USB Via .NET 2 Notifications And Marshal?Jan 14, 2009
I'm a VB6 programmer who was forced to upgrade to Visual Studio 2005 Version 8.0.50727.42 .NET Framework Version 2.0.50727. I am trying to write a call-back which will intercept a Windows message for when our USB HID device is plugged or unplugged. We have a custom .dll which handles the majority of the HID interface. I just need to call a few API's to connect to the device. I can connect and communicate.
I am looking at USB Complete Third Edition. The code examples do not compile. I'm unsure if this is because of the .NET version 2 or the 2005 dialog of VB. I cannot resolve "StructLayout..." statements, but the program appears to be working without them. I cannot get any "Marshal." statements to compile, which is where I am finding it difficult to get around.
Although OnDeviceChange() is being called six times, both IF statements fail. m.msg is 0x219, which is Device Change. m.WParm is always 7, m.lParm is always 0. And, this is where I'm stuck. m.lParm should be a pointer which gets me to the device path? Don't know what 7 is.
So, maybe I'm not registered to receive the proper messages from the device (or I'm looking in the wrong place). The missing step might be to call the RegisterDeviceNotification() API. This book is telling me to use frmMy.Handle declared globally as Friend frmMy As frmUSB - that gives me a null reference runtime error, so I swapped in Me.Handle for the first parameter to RegisterDeviceNotification(). It likes that. But, then it has heartburn with the buffer pointer - not so easy to resolve in Visual Basic. The call wants a LONG. I have DevBroadcastDeviceInterfaceBuffer, which the book says to use Marshal.AllocHGlobal(Size) and Size is Marshal.Sizeof(DevBroadcastDeviceInterface). Neither Marshal statement compiles and I'm not sure if it was removed in this version of .NET? Anyone? I really don't need a dynamically allocated buffer. This is quickie VB app for a single USB device.
Can anyone tell me how far down the rabbit hole I am and what I'm missing. I can tell when something changes to some USB device. I have a handle to that device supplied to me by a custom API library - probably is the HID handle.
How can I setup to receive a message when my HID USB device is unplugged or plugged in?