Hi,
I'm trying to read raw input from a HID-device. I have tried both C# and
VB.NET with no success.
Right now I'm trying to list the raw input devices using
GETRAWINPUTDEVICELIST. My problem, I think, is that the function
GETRAWINPUTDEVICELIST needs a pointer to a structure called RAWINPUTDEVICE,
and I have no clue how to provide this. I think this is the problem,
because right now I can get the number of devices connected to the system,
but when I try to get the list of devices, I don't get any errors, just an
empty structure of RAWINPUTDEVICE.
If anyone could shed any light on this subject, I would greatly appreciate
it.
I have included my code below, Thanks!
David
----------------------------------------------------------------------------
---------------------------------
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure RAWINPUTDEVICELIST
' Handle to the raw input device
Public hDevice As Int32
' Type of device
Public dwType As Int32
End Structure
----------------------------------------------------------------------------
---------------------------------
Public Declare Function GetRawInputDeviceList Lib "user32.dll" _
(ByVal pRawInputDeviceList() As RAWINPUTDEVICELIST, _
ByRef puiNumDevices As Integer, _
ByVal cbSize As Integer) _
As Integer
----------------------------------------------------------------------------
---------------------------------
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Try
Getdevices()
Catch ex As Exception
Data.Text = "Exception: " & ex.Message
End Try
End Sub
----------------------------------------------------------------------------
---------------------------------
Public Sub GetDevices()
' used only to get the size of 1 RAWINPUTDEVICELIST
Dim Device As RawInput.RAWINPUTDEVICELIST
Dim DeviceList(0) As RawInput.RAWINPUTDEVICELIST
Dim DeviceCount As Integer
' Get the number of devices connected
Dim Result As Integer = RawInput.GetRawInputDeviceList(Nothing,
NumDevices, Marshal.SizeOf(DeviceList(0)))
If Not Result = 0 Then
Dim ErrorCode As Integer = Marshal.GetLastWin32Error
Data.Text = "Error " & ErrorCode & ". " & GetMessage(ErrorCode)
Exit Sub
End If
' Allocate the memory needed
ReDim DeviceList(NumDevices - 1)
' ----------------------------------------------------------------------
' Attempt 1 to retrieve all devices
' This attempt failed
Result = RawInput.GetRawInputDeviceList(DeviceList,
Marshal.SizeOf(Device) * NumDevices, Marshal.SizeOf(Device))
' ----------------------------------------------------------------------
' Attempt 2 to retrieve all devices
' This attempt also failed
' I removed attempt 2 in pasting this code.
' ----------------------------------------------------------------------
Select Case Result
Case -1
' Error occurred, display what happened
Dim ErrorCode As Integer = Marshal.GetLastWin32Error
Data.Text = "Error " & ErrorCode & ". " & GetMessage(ErrorCode)
Case 0
' DO NOTHING
Case Else
Data.AppendText("Retrieved " & Result & " devices..." & vbCrLf)
For Each dvc As RawInput.RAWINPUTDEVICELIST In DeviceList
Data.AppendText("hDevice: " & dvc.hDevice.ToString & " ...
dwType: " & dvc.dwType & vbCrLf)
Next dvc
End Select
End Sub
----------------------------------------------------------------------------
---------------------------------
Here is a copy of the output I get from Data.Text:
Retrieved 4 devices...
hDevice: 0 ... dwType: 0
hDevice: 0 ... dwType: 0
hDevice: 0 ... dwType: 0
hDevice: 0 ... dwType: 0
As I see it, the application can "see" that there are 4 devices attached to
my system, which is correct, but the
structure DeviceList() is not being popuplated with the data I need. Thanks!
:-)
Barry Lance - 08 Jun 2004 04:56 GMT
Seen this post and made me curious... I've been trying to learn to use my
Delorme USB GPS via .net. I threw something together and believe I have it
working. Although I write in VB.Net, I do not like using Arrays with API
calls so I go what some may say is the long way around to avoid them. Hope
this helps you out.
Barry Lance
my sample code generates this output:
hDevice: 65633 ... dwType: 1
hDevice: 65631 ... dwType: 1
hDevice: 65609 ... dwType: 0
hDevice: 65607 ... dwType: 0
here's the source
Public Structure RAWINPUTDEVICELIST
Public hDevice As IntPtr
Public dwType As Int32
End Structure
<DllImport("user32.dll", CharSet:=CharSet.Auto,
EntryPOint:="GetRawInputDeviceList", SetLastError:=True)> _
Public Function GetRawInputDeviceList(ByVal pRawInputDeviceList As
IntPtr, ByRef puiNumDevices As Int32, ByVal cbSize As Int32) As Int32
End Function
Sub Main()
Dim RawInputDevice As RAWINPUTDEVICELIST
Dim pRawInputDeviceList As IntPtr = IntPtr.Zero
Dim NumDevices As Int32
Dim Rcode As Int32 = GetRawInputDeviceList(IntPtr.Zero, NumDevices,
Marshal.SizeOf(GetType(RAWINPUTDEVICELIST)))
If NumDevices Then
pRawInputDeviceList = Marshal.AllocHGlobal(NumDevices *
Marshal.SizeOf(GetType(RAWINPUTDEVICELIST)))
If GetRawInputDeviceList(pRawInputDeviceList, NumDevices,
Marshal.SizeOf(GetType(RAWINPUTDEVICELIST))) Then
Dim ListIndex As Int32
For ListIndex = 0 To (NumDevices - 1)
RawInputDevice = Marshal.PtrToStructure(New
IntPtr(pRawInputDeviceList.ToInt32 + (ListIndex *
Marshal.SizeOf(GetType(RAWINPUTDEVICELIST)))),
GetType(RAWINPUTDEVICELIST))
Console.WriteLine("hDevice: " & RawInputDevice.hDevice.ToInt32 & " ...
dwType: " & RawInputDevice.dwType)
Next
Marshal.FreeHGlobal(pRawInputDeviceList)
End If
End If
End Sub
Barry Lance - 08 Jun 2004 05:09 GMT
here's some more sample output after I plugged in my USB HID GPS (Delorme
Earthmate). Note the device of dwType 2 is the GPS.
hDevice: 722389 ... dwType: 2
hDevice: 65633 ... dwType: 1
hDevice: 65631 ... dwType: 1
hDevice: 65609 ... dwType: 0
hDevice: 65607 ... dwType: 0
Barry Lance