473,385 Members | 1,782 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,385 software developers and data experts.

reading from a USB keyboard (HID)

rs
Hi guys,

I am trying to read from a USB keyboard using vb.net and HID classes. the
USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and
is detected in device manager as my keyboard. the USB keyboard is detected
as HID keyboard device. the program finds the keyboard if it is attached.
and I am getting valid handles. however, everytime I use the readfile
function I am getting "object reference not set to an instant of an object"
exception. if i use any other function it fails and I am getting -1 in the
"success" parameter. I am including my code:

HidD_GetHidGuid(HidGuid)

'Friend Function FindDeviceFromGuid _

'(ByVal myGuid As System.Guid, _

'ByRef devicePathName() As String) _

'As Boolean

'Purpose : Uses SetupDi API functions to retrieve the device path name of an

' : attached device that belongs to an interface class.

'Accepts : myGuid - an interface class GUID.

' : devicePathName - a pointer to an array of strings that will contain

' : the device path names of attached devices.

'Returns : True if at least one device is found, False if not.

'Fill an array with the device path names of all attached HIDs.

DeviceFound = MyDeviceManagement.FindDeviceFromGuid _

(HidGuid, _

DevicePathName)

'If there is at least one HID, attempt to read the Vendor ID and Product ID

'of each device until there is a match or all devices have been examined.

If DeviceFound = True Then

MemberIndex = 0

Do

HIDHandle = CreateFile _

(DevicePathName(MemberIndex), _

0, _

FILE_SHARE_READ, _

Security, _

OPEN_EXISTING, _

0, _

0)

If (HIDHandle <> INVALID_HANDLE_VALUE) Then

MyHID.DeviceAttributes.Size = Marshal.SizeOf(MyHID.DeviceAttributes)

Result = HidD_GetAttributes(HIDHandle, MyHID.DeviceAttributes)


If (Result <> 0) Then

'Find out if the device matches the one we're looking for.

If (MyHID.DeviceAttributes.VendorID = MyVendorID) And _

(MyHID.DeviceAttributes.ProductID = MyProductID) Then

MyDeviceDetected = True

'Save the DevicePathName so OnDeviceChange() knows which name is my device.

MyDevicePathName = DevicePathName(MemberIndex)

Else

'It's not a match, so close the handle.

MyDeviceDetected = False

Result = CloseHandle(HIDHandle)

End If

Else

'There was a problem in retrieving the information.

MyDeviceDetected = False

Result = CloseHandle(HIDHandle)

End If

End If

'Keep looking until we find the device or there are no more left to examine.

MemberIndex = MemberIndex + 1

Loop Until ((MyDeviceDetected = True) Or _

(MemberIndex = UBound(DevicePathName) + 1))

If MyDeviceDetected Then

rtbstatus.Text = rtbstatus.Text + vbCrLf + "keyboard found"

'The device was detected.

'Register to receive notifications if the device is removed or attached.

Success = MyDeviceManagement.RegisterForDeviceNotifications _

(MyDevicePathName, _

Me.Handle, _

HidGuid, _

DeviceNotificationHandle)
'Debug.WriteLine("RegisterForDeviceNotifications = " & Success)

'Learn the capabilities of the device.

MyHID.Capabilities = MyHID.GetDeviceCapabilities _

(HIDHandle)

If Success Then

'Get and display the Input report buffer size.

'GetInputReportBufferSize()

rtbstatus.Text = rtbstatus.Text + vbCrLf + "register notification is set"

'*******************************

ReadHandle = CreateFile _

(MyDevicePathName, _

GENERIC_READ, _

0, _

Security, _

OPEN_EXISTING, _

0, _

0) I also tried it with overlaping. nogo

rtbstatus.Text = rtbstatus.Text + vbCrLf + "handle created"

'(optional)

'Flush any waiting reports in the input buffer.

MyHID.FlushQueue(ReadHandle)

MyHID.FlushQueue(HIDHandle)

'*****************************************

'start reading

Dim InputReportBuffer() As Byte

Dim NumberOfBytesRead As Long

ReDim InputReportBuffer(32)

success = HidD_GetFeature _

(hidHandle, _

InputReportBuffer(0), _

CInt(UBound(inFeatureReportBuffer) + 1))

'sucess always = false and input report buffer = 0s. I also tried

HidD_GetInputReport and read file functions. nogo.

any idea what i am doing wrong? any input is appreciated.


Nov 21 '05 #1
0 6572

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

Similar topics

1
by: CMiYC | last post by:
I'm attempting to open an USB HID device using createfile from kernel32. The open attempt always fails. This is the call I'm using: CreateFile(HidName$, &HC0000000, 3, 0, 3, 0 ,0) HidName$...
2
by: Michael Bendzick | last post by:
Is there a simple way in python to read a keyboard scan code? I'm working on a shell script that interfaces with a proprietary keyboard device (extra buttons) and need to be able to distinguish...
2
by: Hubert Hermanutz | last post by:
Hello, Where can i get a sample about HID communication with .NET Framework? I don't know enough about HID communication, only that with VC++ it is possible to using the Windows Device...
7
by: Don Riesbeck Jr. | last post by:
I'm working on an application (OEM) using C# that utilizes input from a keyboard, and USB Barcode Scanner. The scanner is a HID Keyboard device, and input from it is sent to the system as if it...
1
by: Eric Renken | last post by:
We are developing an HID device and I have a couple questions about communicating with it. I need to be able to do a direct Interrupt Endpoint call to the USB device. Is this possible to do from...
2
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as...
0
by: Chris Herring | last post by:
I am writing a WinForms app that will integrate a handheld voting device system into the app. The handhelds communicate to the PC via a USB connection. The USB device has no manufacturer driver,...
3
by: jodansmif | last post by:
I've been teaching myself Python as part of my senior design project at university. The obstacle our group currently faces is communicating with a microcontroller (ACS USB Servo II) that appears...
0
by: LawrenceK | last post by:
Hi All, I don't know if this is the right forum for my question. I want to be able configure a USB HID through a web page (i.e. using IE). I read some where in this forum that it was possible to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.