473,327 Members | 1,979 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,327 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 10333
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.