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

Can't Get API to work from VB.net for USB

Help

I have code that Works fine in VB6, that I have attempted to convert to VB.net
I do a Call HidD_GetHidGuid(HidGuid), and it looks like it gets the right info, then I do a call to SetupDiGetClassDevs(HidGuid, 0, 0, &H12) and Get a Handle, different than in VB6 but I thought it was due to diff of varible sizes. But when I loop through trying to find my device using SetupDiEnumDeviceInterfaces(PnPHandle, 0, HidGuid, HIDdevice, SpDeviceInfoData), while I get no errors, but I never find any devices, even though I know I should get a couple of HID devices. I don't think I am passing the right stuff to the API's, but don't know where to turn. I've tried changing what and how I pass to the API's, but other than generating an errors that GetLastError() always returns as a 126 (The specified module could not be found) I can't get it to work. Can anybody Help? I'm way behind in this project.
Jul 21 '05 #1
4 10338
On 2004-04-15, Dale McGorman <k8**@arrl.net> wrote:
Help,

I have code that Works fine in VB6, that I have attempted to convert to VB.net.
I do a Call HidD_GetHidGuid(HidGuid), and it looks like it gets the right info, then I do a call to SetupDiGetClassDevs(HidGuid, 0, 0, &H12) and Get a Handle, different than in VB6 but I thought it was due to diff of varible sizes. But when I loop through trying to find my device using SetupDiEnumDeviceInterfaces(PnPHandle, 0, HidGuid, HIDdevice, SpDeviceInfoData), while I get no errors, but I never find any devices, even though I know I should get a couple of HID devices. I don't think I am passing the right stuff to the API's, but don't know where to turn. I've tried changing what and how I pass to the API's, but other than generating an errors that GetLastError() always returns as a 126 (The specified module could not be found) I can't get it to work. Can anybody Help? I'm way behind in this project.


It would be a lot easier to diagnose your problem if you posted your
code. Specifically your api declarations and a sample of how your using
the functions.

--
Tom Shelton [MVP]
Powered By Gentoo Linux 1.4
In the long run, every program becomes rococco, and then rubble.
-- Alan Perlis
Jul 21 '05 #2
On 2004-04-15, Dale McGorman <k8**@arrl.net> wrote:
Help,

I have code that Works fine in VB6, that I have attempted to convert to VB.net.
I do a Call HidD_GetHidGuid(HidGuid), and it looks like it gets the right info, then I do a call to SetupDiGetClassDevs(HidGuid, 0, 0, &H12) and Get a Handle, different than in VB6 but I thought it was due to diff of varible sizes. But when I loop through trying to find my device using SetupDiEnumDeviceInterfaces(PnPHandle, 0, HidGuid, HIDdevice, SpDeviceInfoData), while I get no errors, but I never find any devices, even though I know I should get a couple of HID devices. I don't think I am passing the right stuff to the API's, but don't know where to turn. I've tried changing what and how I pass to the API's, but other than generating an errors that GetLastError() always returns as a 126 (The specified module could not be found) I can't get it to work. Can anybody Help? I'm way behind in this project.


It would be a lot easier to diagnose your problem if you posted your
code. Specifically your api declarations and a sample of how your using
the functions.

--
Tom Shelton [MVP]
Powered By Gentoo Linux 1.4
In the long run, every program becomes rococco, and then rubble.
-- Alan Perlis
Jul 21 '05 #3
OK
Here it is
Declaration
I've only worked on the first three API Calls so far, so there may be probems in some of the other

Public Structure Device_Interface_Dat
Dim cbsize As Int3
Dim InterfaceClassGuid As Gui
Dim Flags As Int3
Dim ReservedPtr As Lon
End Structur

Public Structure Device_Interface_Detai
Public cbsize As Intege
'<VBFixedArray(256)
Public DataPath() As Byt

'UPGRADE_TODO: "Initialize" must be called to initialize instances of this structure. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1026"
Public Sub Initialize(
ReDim DataPath(256
End Su
End Structur

Public Structure Security_Attribute
Public nLength As Intege
Public lpSecurityDescriptor As Intege
Public bInheritHandle As Intege
End Structur

Public Structure HidD_Attribute
Public Size As Lon
Public VendorID As Shor
Public ProductID As Shor
Public VersionNumber As Shor
End Structur
Public Structure SP_DEVINFO_DAT
Public cbsize As Intege
Public ClassGuid As Gui
Public DevInst As Intege
Public Reserved As Lon
End Structur

Public Class Win3

Declare Sub HidD_GetHidGuid Lib "HID.dll" (ByRef GuidPtr As Guid

Declare Auto Function SetupDiGetClassDevs Lib "setupapi.dll" Alias "SetupDiGetClassDevsA"
(ByRef GuidPtr, ByRef EnumPtr, ByRef HwndParent, ByVal Flags) As Lon

Declare Auto Function SetupDiEnumDeviceInterfaces Lib "setupapi.dll" (ByVal Handle As Long, ByVal InfoPtr As Long, ByRef GuidPtr As Guid, ByVal MemberIndex As Integer, ByRef InterfaceDataPtr As SP_DEVINFO_DATA) As Intege

Declare Auto Function SetupDiGetDeviceInterfaceDetail Lib "setupapi.dll" Alias "SetupDiGetDeviceInterfaceDetailA"
(ByVal Handle, ByRef InterfaceDataPtr, ByRef InterfaceDetailPtr, ByVal DetailLength,
ByRef ReturnedLengthPtr, ByRef DevInfoDataPtr) As Boolea

'UPGRADE_WARNING: Structure Security_Attributes may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer,
ByVal dwShareMode As Integer, ByRef lpSecurityAttributes As Security_Attributes,
ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer,
ByVal hTemplateFile As Integer) As Intege

Declare Function HidD_GetAttributes Lib "HID.dll" (ByVal Handle As Integer, ByRef BufferPtr As Integer) As Intege

Declare Sub CloseHandle Lib "kernel32" (ByVal HandleToClose As Integer

Declare Function SetupDiDestroyDeviceInfoList Lib "setupapi.dll" (ByVal DeviceInfoSet As Integer) As Boolea

Declare Function GetLastError Lib "kernel32" () As Intege

Declare Function FormatMessage Lib "kernel32" (ByVal dwFlags As Int32, ByVal lpSource As Int32, ByVal dwMessageId As Int32,
ByVal lpBuffer As Int32, ByVal nSize As Int32, ByVal arguments As Long) As Intege

End Clas

Function I am using. Have not gotten all the way through it so there are probably several things that need fixed. I never got anything to come back from the Win32.SetupDiEnumDeviceInterfaces(PnPHandle, 0, HidGuid, HIDdevice, SpDeviceInfoData) cal

Function OpenUSB() As Boolea
Dim PnPHandle As Int6
Dim HIDdevice As Lon
Dim BytesReturned As Lon
Dim inx As Int1
Dim HidName As Strin
Dim ThisHIDdevice As HidD_Attribute
Dim TempInt As Intege
Dim result As Boolea
Dim strTemp As Strin

Dim DeviceInterfaceData As Device_Interface_Dat
Dim FunctionClassDeviceData As Device_Interface_Detai
Dim SpDeviceInfoData As SP_DEVINFO_DATA

On Error GoTo OpenUSBErrHndlr
OpenUSB = True
' First, get my class identifier
Call Win32.HidD_GetHidGuid(HidGuid)

'strTemp = HidGuid.ToString
'
' Get a handle for the Plug and Play node and request currently active HID devices
PnPHandle = Win32.SetupDiGetClassDevs(HidGuid, 0, 0, &H12)
'If (PnPHandle& = -1) Then ErrorExit ("Could not attach to PnP node")

FunctionClassDeviceData.Initialize()
DeviceInterfaceData.cbsize = 28 'Length of data structure in bytes
'
' Lets look for a maximum of 20 HID devices
For HIDdevice = 0 To 19

' Is there a HID device at this table entry

If Win32.SetupDiEnumDeviceInterfaces(PnPHandle, 0, HidGuid, HIDdevice, SpDeviceInfoData) Then
' There is a device here, get it's name
FunctionClassDeviceData.cbsize = 5
result = Win32.SetupDiGetDeviceInterfaceDetail(PnPHandle, DeviceInterfaceData, FunctionClassDeviceData.cbsize, _
Len(FunctionClassDeviceData), BytesReturned, 0)
' If (Success = 0) Then ErrorExit ("Could not find the system name for this HID device")
' Convert C string to Visual Basic String
HidName = "" : inx = 0
Do While FunctionClassDeviceData.DataPath(inx) <> 0
HidName = HidName & Chr(FunctionClassDeviceData.DataPath(inx))
inx = inx + 1
Loop
' Can now open this HID device

Dim SA As Security_Attributes

HidHandle = Win32.CreateFile(HidName, &HC0000000, 3, SA, 3, 0, 0)
If (HidHandle = -1) Then
' Note that Win2K opens system input devices (mouse, keyboard) for EXCLUSIVE use
strTemp = "System input HID detected"
Else
' Get VID and PID for display
result = Win32.HidD_GetAttributes(HidHandle, ThisHIDdevice.Size)

If (ThisHIDdevice.VendorID = 66) Then

OurDevHndl = HidHandle
Else
Call Win32.CloseHandle(HidHandle)
End If
End If
End If 'SetupDiEnumDeviceInterfaces
Next HIDdevice

Call Win32.SetupDiDestroyDeviceInfoList(PnPHandle)
Exit Function
OpenUSBErrHndlr:
Dim errNum As Int32
errNum = Win32.GetLastError()
OpenUSB = False
End Function
Jul 21 '05 #4
OK
Here it is
Declaration
I've only worked on the first three API Calls so far, so there may be probems in some of the other

Public Structure Device_Interface_Dat
Dim cbsize As Int3
Dim InterfaceClassGuid As Gui
Dim Flags As Int3
Dim ReservedPtr As Lon
End Structur

Public Structure Device_Interface_Detai
Public cbsize As Intege
'<VBFixedArray(256)
Public DataPath() As Byt

'UPGRADE_TODO: "Initialize" must be called to initialize instances of this structure. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1026"
Public Sub Initialize(
ReDim DataPath(256
End Su
End Structur

Public Structure Security_Attribute
Public nLength As Intege
Public lpSecurityDescriptor As Intege
Public bInheritHandle As Intege
End Structur

Public Structure HidD_Attribute
Public Size As Lon
Public VendorID As Shor
Public ProductID As Shor
Public VersionNumber As Shor
End Structur
Public Structure SP_DEVINFO_DAT
Public cbsize As Intege
Public ClassGuid As Gui
Public DevInst As Intege
Public Reserved As Lon
End Structur

Public Class Win3

Declare Sub HidD_GetHidGuid Lib "HID.dll" (ByRef GuidPtr As Guid

Declare Auto Function SetupDiGetClassDevs Lib "setupapi.dll" Alias "SetupDiGetClassDevsA"
(ByRef GuidPtr, ByRef EnumPtr, ByRef HwndParent, ByVal Flags) As Lon

Declare Auto Function SetupDiEnumDeviceInterfaces Lib "setupapi.dll" (ByVal Handle As Long, ByVal InfoPtr As Long, ByRef GuidPtr As Guid, ByVal MemberIndex As Integer, ByRef InterfaceDataPtr As SP_DEVINFO_DATA) As Intege

Declare Auto Function SetupDiGetDeviceInterfaceDetail Lib "setupapi.dll" Alias "SetupDiGetDeviceInterfaceDetailA"
(ByVal Handle, ByRef InterfaceDataPtr, ByRef InterfaceDetailPtr, ByVal DetailLength,
ByRef ReturnedLengthPtr, ByRef DevInfoDataPtr) As Boolea

'UPGRADE_WARNING: Structure Security_Attributes may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer,
ByVal dwShareMode As Integer, ByRef lpSecurityAttributes As Security_Attributes,
ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer,
ByVal hTemplateFile As Integer) As Intege

Declare Function HidD_GetAttributes Lib "HID.dll" (ByVal Handle As Integer, ByRef BufferPtr As Integer) As Intege

Declare Sub CloseHandle Lib "kernel32" (ByVal HandleToClose As Integer

Declare Function SetupDiDestroyDeviceInfoList Lib "setupapi.dll" (ByVal DeviceInfoSet As Integer) As Boolea

Declare Function GetLastError Lib "kernel32" () As Intege

Declare Function FormatMessage Lib "kernel32" (ByVal dwFlags As Int32, ByVal lpSource As Int32, ByVal dwMessageId As Int32,
ByVal lpBuffer As Int32, ByVal nSize As Int32, ByVal arguments As Long) As Intege

End Clas

Function I am using. Have not gotten all the way through it so there are probably several things that need fixed. I never got anything to come back from the Win32.SetupDiEnumDeviceInterfaces(PnPHandle, 0, HidGuid, HIDdevice, SpDeviceInfoData) cal

Function OpenUSB() As Boolea
Dim PnPHandle As Int6
Dim HIDdevice As Lon
Dim BytesReturned As Lon
Dim inx As Int1
Dim HidName As Strin
Dim ThisHIDdevice As HidD_Attribute
Dim TempInt As Intege
Dim result As Boolea
Dim strTemp As Strin

Dim DeviceInterfaceData As Device_Interface_Dat
Dim FunctionClassDeviceData As Device_Interface_Detai
Dim SpDeviceInfoData As SP_DEVINFO_DATA

On Error GoTo OpenUSBErrHndlr
OpenUSB = True
' First, get my class identifier
Call Win32.HidD_GetHidGuid(HidGuid)

'strTemp = HidGuid.ToString
'
' Get a handle for the Plug and Play node and request currently active HID devices
PnPHandle = Win32.SetupDiGetClassDevs(HidGuid, 0, 0, &H12)
'If (PnPHandle& = -1) Then ErrorExit ("Could not attach to PnP node")

FunctionClassDeviceData.Initialize()
DeviceInterfaceData.cbsize = 28 'Length of data structure in bytes
'
' Lets look for a maximum of 20 HID devices
For HIDdevice = 0 To 19

' Is there a HID device at this table entry

If Win32.SetupDiEnumDeviceInterfaces(PnPHandle, 0, HidGuid, HIDdevice, SpDeviceInfoData) Then
' There is a device here, get it's name
FunctionClassDeviceData.cbsize = 5
result = Win32.SetupDiGetDeviceInterfaceDetail(PnPHandle, DeviceInterfaceData, FunctionClassDeviceData.cbsize, _
Len(FunctionClassDeviceData), BytesReturned, 0)
' If (Success = 0) Then ErrorExit ("Could not find the system name for this HID device")
' Convert C string to Visual Basic String
HidName = "" : inx = 0
Do While FunctionClassDeviceData.DataPath(inx) <> 0
HidName = HidName & Chr(FunctionClassDeviceData.DataPath(inx))
inx = inx + 1
Loop
' Can now open this HID device

Dim SA As Security_Attributes

HidHandle = Win32.CreateFile(HidName, &HC0000000, 3, SA, 3, 0, 0)
If (HidHandle = -1) Then
' Note that Win2K opens system input devices (mouse, keyboard) for EXCLUSIVE use
strTemp = "System input HID detected"
Else
' Get VID and PID for display
result = Win32.HidD_GetAttributes(HidHandle, ThisHIDdevice.Size)

If (ThisHIDdevice.VendorID = 66) Then

OurDevHndl = HidHandle
Else
Call Win32.CloseHandle(HidHandle)
End If
End If
End If 'SetupDiEnumDeviceInterfaces
Next HIDdevice

Call Win32.SetupDiDestroyDeviceInfoList(PnPHandle)
Exit Function
OpenUSBErrHndlr:
Dim errNum As Int32
errNum = Win32.GetLastError()
OpenUSB = False
End Function
Jul 21 '05 #5

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

Similar topics

22
by: The Road To Utopia | last post by:
Here's one for the trolls...a common jibe from them is setting up audio/video hardware under linux. Ok, true story: at work today, someone asked me if I could tell him why his XP Home would play...
119
by: rhat | last post by:
I heard that beta 2 now makes ASP.NET xhtml compliant. Can anyone shed some light on what this will change and it will break stuff as converting HTML to XHTML pages DO break things. see,...
12
by: lkrubner | last post by:
Is there any reason why this code wouldn't prompt someone to save their work every 5 minutes? It doesn't seem to be working. This code shows up in the HEAD of an HTML document. function...
1
by: Lyle Fairfield | last post by:
I created a new MS-SQL Database, "TestODBC". I made Table1 and StoredProcedure1. I made an ODBC DSN for that MS-SQL Database. I created a new AccessXP mdb, "TestODBC". I linked to the...
5
by: MGFoster | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I've converted an ACC97 .mdb file to an ACC2K2 .adp. A report that worked in ACC97 doesn't work in ACC2K2. Report setup: ACC97 ...
9
by: kernelxu | last post by:
hi, everyone. now, I'am confused on such a problem: function setbuf(stdin, NULL) or setvbuf(stdin, NULL, _IONBF, 0) can set the stadard input stream unbuffered. however, why does my program...
1
by: Larry | last post by:
These is a interface " IPerson" and a class "SoftwareDeveloper" support the interface; public __gc __interface IPerson { void Eat(); void Sleep(; void Work();
9
by: tshad | last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't seem to get the Sub (writefile) to execute. It displays all the response.write lines that are called directly, but not...
15
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.