Jump to content

[Solved] I need help dealing with a C++ DLL [ pointer to pointer ]


Recommended Posts

welcome
I need help dealing with a C++ DLL

Specify that I need help with how to use this function

struct libusb_device;

typedef struct libusb_context libusb_context;

ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx,libusb_device ***list);

My problem is how to deal with "libusb_device ***list"

my code 

$libusb_device  = DllStructCreate("PTR")

Func libusb_init()
 $Var = DllCall($libusb0, "ptr", "libusb_init",'ptr',$_libusb_device_handle_Ptr)
 If @error Then Return False
 Return $Var[1]
EndFunc

Func libusb_get_device_list()
    MsgBox(0,VarGetType($_libusb_device_handle_Ptr),$_libusb_device_handle_Ptr)
 $Var = DllCall($libusb0, "int", "libusb_get_device_list","ptr",$_libusb_device_handle_Ptr,"ptr*",POINTER(POINTER($libusb_device)))
  $Error = @error
  If $Error Then Return False
 MsgBox(0,VarGetType($Var),$Error&@CRLF&$Var)
 _ArrayDisplay($Var)
  Return $Var[1]
EndFunc

 

Thank you

iLibUSB.dll iLibUSB_32.dll libusb.h

Edited by DevMode

dongel.png

Link to comment
Share on other sites

Hello. Please next time share a runnable code. Here is an example I wrote years ago.

 

#include <Array.au3>

Global Enum $LIBUSB_SUCCESS = 0
Global $g_sLibName = @AutoItX64 ? "iLibUSB.dll" : "iLibUSB_32.dll"
Global $g_hlibusb = DllOpen($g_sLibName)


_Test()

Func _Test()
    _libusb_init()
    Local $ahDevicesList = _libusb_get_device_list()
    _ArrayDisplay($ahDevicesList)
    _libusb_free_device_list($ahDevicesList)
    _libusb_exit()
EndFunc   ;==>_Test


Func _libusb_exit($hContext = 0)
    DllCall($g_hlibusb, "none", "libusb_exit", "ptr", $hContext)
    If @error Then Return SetError(1, 0, 0)
    Return 1
EndFunc   ;==>_libusb_exit

Func _libusb_free_device_list(ByRef $ahDevicesList, $iUnref_Devices = 1)
    If Not IsArray($ahDevicesList) Or Not UBound($ahDevicesList) Then Return SetError(1, 0, 0)
    Local $hDeviceList = (UBound($ahDevicesList, 2) = 4) ? $ahDevicesList[0][0] : $ahDevicesList[0]
    If Not $hDeviceList Then Return SetError(1, 0, 0)
    Local $aCall = DllCall($g_hlibusb, "none", "libusb_free_device_list", "ptr", $hDeviceList, "int", $iUnref_Devices)
    If @error Then Return SetError(1, 0, 0)
    Return 1
EndFunc   ;==>_libusb_free_device_list

Func _libusb_get_device_list($hContext = 0)
    Local $aCall = DllCall($g_hlibusb, "int", "libusb_get_device_list", "ptr", $hContext, "ptr*", 0)
    If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
    Local $tlibusb_device_list = DllStructCreate("ptr[" & $aCall[0] & "]", $aCall[2])
    Local $ahDevicesList[$aCall[0] + 1]

    $ahDevicesList[0] = $aCall[2] ;store libusb_device pointer
    For $i = 1 To $aCall[0]
        $ahDevicesList[$i] = DllStructGetData($tlibusb_device_list, 1, $i)
    Next
    Return $ahDevicesList
EndFunc   ;==>_libusb_get_device_list

Func _libusb_init($bNewhContext = False)
    If Not $g_hlibusb Then Return SetError(1, 0, -1)
    Local $sParamType = ($bNewhContext) ? "ptr*" : "ptr"
    Local $aCall = DllCall($g_hlibusb, "int", "libusb_init", $sParamType, 0)
    If @error Or Not $aCall[0] = $LIBUSB_SUCCESS Then Return SetError(1, 0, 0)
    Return $aCall[1]
EndFunc   ;==>_libusb_init

 

PD: I like your website. It's nice.

Saludos

Edited by Danyfirex
Added Free functions
Link to comment
Share on other sites

  • DevMode changed the title to [Solved] I need help dealing with a C++ DLL [ pointer to pointer ]
  • 1 month later...

Hi,DanyfirexDevMode

       I looked for a long time for examples of USB device connections (including forums for autoit other languages) and couldn't find what I needed because it wasn't COM communication, until I saw hope here, and now I'm facing a USB 2.0 device communication, but I don't have a C language basis. In the libusb API document still cannot be converted to AUTOIT.

Libusb Description Document:https://libusb.sourceforge.io/api-1.0/libusb_api.html

How do I determine which model my device is?  it's a barcode reader.

in the USB DeviceViewer (Companion Hub Symbolic Link Name): USB#ROOT_HUB30#4&31765f9e&0&0#{f18a0e88-c30c-11d0-8815-00a0c906bed8}

in the regedit Device Parameters :\??\USB#VID_0720&PID_3014#5&aaebba4&0&7#{a5dcbf10-6530-11d2-901f-00c04fb951ed}

libusb_class_code {
LIBUSB_CLASS_PER_INTERFACE = 0x00, LIBUSB_CLASS_AUDIO = 0x01, LIBUSB_CLASS_COMM = 0x02, LIBUSB_CLASS_HID = 0x03,
LIBUSB_CLASS_PHYSICAL = 0x05, LIBUSB_CLASS_IMAGE = 0x06, LIBUSB_CLASS_PTP = 0x06, LIBUSB_CLASS_PRINTER = 0x07,
LIBUSB_CLASS_MASS_STORAGE = 0x08, LIBUSB_CLASS_HUB = 0x09, LIBUSB_CLASS_DATA = 0x0a, LIBUSB_CLASS_SMART_CARD = 0x0b,
LIBUSB_CLASS_CONTENT_SECURITY = 0x0d, LIBUSB_CLASS_VIDEO = 0x0e, LIBUSB_CLASS_PERSONAL_HEALTHCARE = 0x0f, LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc,
LIBUSB_CLASS_WIRELESS = 0xe0, LIBUSB_CLASS_MISCELLANEOUS = 0xef, LIBUSB_CLASS_APPLICATION = 0xfe, LIBUSB_CLASS_VENDOR_SPEC = 0xff
}

can you share your script? (like how to read the description, how to connect and communicate, how to determine the device model), thank you very much.

Link to comment
Share on other sites

Hello @Hadin Could you check with this tool and let me know if you see the information you want. So to be sure you're device expose required information. 

 

Saludos

Link to comment
Share on other sites

Hi,Danyfirex,Thank you for your attention and reply.

I saw the invoking method of Libusb.dll that you wrote above, and I tried to use it. Therefore, I also found relevant forums such as German, French, and Chinese. There is no such information, and I can only query it in the official libusb documentation. I also tried to translate it into autoit , I tried to understand what you translated, and then to translate the rest, but without the C language foundation, I did nothing. This open source library is very practical for future applications, more USB devices and protocols will be used in Windows systems in the future. But most of our use is RS232 COM applications, so there is an urgent need to learn USB communication.

Quote

#include <Array.au3>

Global Enum $LIBUSB_SUCCESS = 0
Global $g_sLibName = @AutoItX64 ? "iLibUSB.dll" : "iLibUSB_32.dll"
Global $g_hlibusb = DllOpen($g_sLibName)


_Test()

Func _Test()
    _libusb_init()
    Local $ahDevicesList = _libusb_get_device_list()
    _ArrayDisplay($ahDevicesList)
    _libusb_free_device_list($ahDevicesList)
    _libusb_exit()
EndFunc   ;==>_Test


Func _libusb_exit($hContext = 0)
    DllCall($g_hlibusb, "none", "libusb_exit", "ptr", $hContext)
    If @error Then Return SetError(1, 0, 0)
    Return 1
EndFunc   ;==>_libusb_exit

Func _libusb_free_device_list(ByRef $ahDevicesList, $iUnref_Devices = 1)
    If Not IsArray($ahDevicesList) Or Not UBound($ahDevicesList) Then Return SetError(1, 0, 0)
    Local $hDeviceList = (UBound($ahDevicesList, 2) = 4) ? $ahDevicesList[0][0] : $ahDevicesList[0]
    If Not $hDeviceList Then Return SetError(1, 0, 0)
    Local $aCall = DllCall($g_hlibusb, "none", "libusb_free_device_list", "ptr", $hDeviceList, "int", $iUnref_Devices)
    If @error Then Return SetError(1, 0, 0)
    Return 1
EndFunc   ;==>_libusb_free_device_list

Func _libusb_get_device_list($hContext = 0)
    Local $aCall = DllCall($g_hlibusb, "int", "libusb_get_device_list", "ptr", $hContext, "ptr*", 0)
    If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
    Local $tlibusb_device_list = DllStructCreate("ptr[" & $aCall[0] & "]", $aCall[2])
    Local $ahDevicesList[$aCall[0] + 1]

    $ahDevicesList[0] = $aCall[2] ;store libusb_device pointer
    For $i = 1 To $aCall[0]
        $ahDevicesList[$i] = DllStructGetData($tlibusb_device_list, 1, $i)
    Next
    Return $ahDevicesList
EndFunc   ;==>_libusb_get_device_list

Func _libusb_init($bNewhContext = False)
    If Not $g_hlibusb Then Return SetError(1, 0, -1)
    Local $sParamType = ($bNewhContext) ? "ptr*" : "ptr"
    Local $aCall = DllCall($g_hlibusb, "int", "libusb_init", $sParamType, 0)
    If @error Or Not $aCall[0] = $LIBUSB_SUCCESS Then Return SetError(1, 0, 0)
    Return $aCall[1]
EndFunc   ;==>_libusb_init

API page:https://libusb.sourceforge.io/api-1.0/libusb_api.html

libusb_get_device_descriptor,Official API Description:

◆ libusb_get_device_descriptor()
int libusb_get_device_descriptor    (   libusb_device *     dev,
struct libusb_device_descriptor *   desc 
)       
Get the USB device descriptor for a given device.
This is a non-blocking function; the device descriptor is cached in memory.
Note since libusb-1.0.16, LIBUSB_API_VERSION >= 0x01000102, this function always succeeds.
Parameters
dev the device
desc    output location for the descriptor data
Returns
0 on success or a LIBUSB_ERROR code on failure

I tried to add a libusb_get_device_descriptor function to your base, but it didn't work at all.

Func _libusb_get_des($prt)
    Local $tagSTRUCT1 = "struct;byte blength;byte bdescriptortype;word bcdusb;byte bdeviceclass;byte bdevicesubclass;byte bdeviceprotocol;byte bmaxpacketsize0;word idvendor;word idproduct;word bcddevice;byte imanufacturer;byte iproduct;byte iserialNumber;byte bnumconfigurations;endstruct"

     Local $tSTRUCT1 = DllStructCreate($tagSTRUCT1)
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "DllStructCreate error: " & @error)
        Return False
    EndIf
ConsoleWrite("$tagSTRUCT1:OK" &@CRLF)

    Local $aCall = DllCall($g_hlibusb, "int", "libusb_get_device_descriptor", "ptr", $prt, "struct", DllStructGetPtr($tSTRUCT1))
    If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
ConsoleWrite("libusb_get_device_descriptor:"&$aCall[0] &@CRLF)

    Return $aCall[0]

EndFunc
Quote

Returned information:

libusb_get_device_list:3
$ahDevicesList:0x011D5B28
$tagSTRUCT1:OK
Error=1

Here's my device information:

=========================== USB Port7 ===========================

Connection Status        : 0x01 (Device is connected)
Port Chain               : 1-7
Properties               : 0x01
 IsUserConnectable       : yes
 PortIsDebugCapable      : no
 PortHasMultiCompanions  : no
 PortConnectorIsTypeC    : no
ConnectionIndex          : 0x07 (Port 7)
CompanionIndex           : 0
 CompanionHubSymLnk      : USB#ROOT_HUB30#4&31765f9e&0&0#{f18a0e88-c30c-11d0-8815-00a0c906bed8}
 CompanionPortNumber     : 0x19 (Port 25)
 -> CompanionPortChain   : 1-25

      ========================== Summary =========================
Vendor ID                : 0x0720 (Keyence Corporation)
Product ID               : 0x3014
USB Version              : 2.00 -> wrong, Device is Full-Speed only
Port maximum Speed       : High-Speed (Companion Port 1-25 supports SuperSpeed)
Device maximum Speed     : Full-Speed
Device Connection Speed  : Full-Speed
Self powered             : yes
Demanded Current         : 100 mA
Used Endpoints           : 8

      ======================== USB Device ========================

        +++++++++++++++++ Device Information ++++++++++++++++++
Device Description       : KEYENCE SR-700
Device Path 1            : \\?\USB#VID_0720&PID_3014#5&aaebba4&0&7#{24074605-7ee7-4f86-8dd5-87d6e091969e}
Device Path 2            : \\?\USB#VID_0720&PID_3014#5&aaebba4&0&7#{a5dcbf10-6530-11d2-901f-00c04fb951ed} (GUID_DEVINTERFACE_USB_DEVICE)
Kernel Name              : \Device\USBPDO-2
Device ID                : USB\VID_0720&PID_3014\5&AAEBBA4&0&7
Hardware IDs             : USB\VID_0720&PID_3014&REV_0002 USB\VID_0720&PID_3014
Driver KeyName           : {36fc9e60-c465-11cf-8056-444553540000}\0005 (GUID_DEVCLASS_USB)
Driver                   : \SystemRoot\System32\drivers\KEYENCE_SR-700.sys (Version: 2.0.3.1  Date: 2020-07-22)
Driver Inf               : C:\WINDOWS\inf\oem78.inf
Legacy BusType           : PNPBus
Class                    : USB
Class GUID               : {36fc9e60-c465-11cf-8056-444553540000} (GUID_DEVCLASS_USB)
Service                  : KEYENCE_SR-700
Enumerator               : USB
Location Info            : Port_#0007.Hub_#0001
Location IDs             : PCIROOT(0)#PCI(1400)#USBROOT(0)#USB(7), ACPI(_SB_)#ACPI(PCI0)#ACPI(XHC_)#ACPI(RHUB)#ACPI(HS07)
Container ID             : {98e9b4ce-38f0-11ec-9113-6c4b90e9bb70}
Manufacturer Info        : KEYENCE CORPORATION
Capabilities             : 0x84 (Removable, SurpriseRemovalOK)
Status                   : 0x0180600A (DN_DRIVER_LOADED, DN_STARTED, DN_DISABLEABLE, DN_REMOVABLE, DN_NT_ENUMERATOR, DN_NT_DRIVER)
Problem Code             : 0
Address                  : 7
HcDisableSelectiveSuspend: 0
EnableSelectiveSuspend   : 0
SelectiveSuspendEnabled  : 0
EnhancedPowerMgmtEnabled : 0
IdleInWorkingState       : 0
WakeFromSleepState       : 0
Power State              : D0 (supported: D0, D3, wake from D0)

        +++++++++++++++++ Registry USB Flags +++++++++++++++++
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\usbflags\072030140002
 osvc                    : REG_BINARY 00 00

        ---------------- Connection Information ---------------
Connection Index         : 0x07 (Port 7)
Connection Status        : 0x01 (DeviceConnected)
Current Config Value     : 0x01 (Configuration 1)
Device Address           : 0x04 (4)
Is Hub                   : 0x00 (no)
Device Bus Speed         : 0x01 (Full-Speed)
Number Of Open Pipes     : 0x07 (7 pipes to data endpoints)
Pipe[0]                  : EndpointID=1  Direction=OUT  ScheduleOffset=0  Type=Bulk       wMaxPacketSize=64   bInterval=0
Pipe[1]                  : EndpointID=1  Direction=IN   ScheduleOffset=0  Type=Bulk       wMaxPacketSize=64   bInterval=0
Pipe[2]                  : EndpointID=2  Direction=OUT  ScheduleOffset=0  Type=Bulk       wMaxPacketSize=64   bInterval=0
Pipe[3]                  : EndpointID=2  Direction=IN   ScheduleOffset=0  Type=Bulk       wMaxPacketSize=64   bInterval=0
Pipe[4]                  : EndpointID=3  Direction=OUT  ScheduleOffset=0  Type=Bulk       wMaxPacketSize=64   bInterval=0
Pipe[5]                  : EndpointID=3  Direction=IN   ScheduleOffset=0  Type=Bulk       wMaxPacketSize=64   bInterval=0
Pipe[6]                  : EndpointID=4  Direction=IN   ScheduleOffset=0  Type=Interrupt  wMaxPacketSize=64   bInterval=10
Data (HexDump)           : 07 00 00 00 12 01 00 02 00 00 00 08 20 07 14 30   ............ ..0
                           02 00 01 02 00 01 01 01 00 04 00 07 00 00 00 01   ................
                           00 00 00 07 05 01 02 40 00 00 00 00 00 00 07 05   .......@........
                           81 02 40 00 00 00 00 00 00 07 05 02 02 40 00 00   ..@..........@..
                           00 00 00 00 07 05 82 02 40 00 00 00 00 00 00 07   ........@.......
                           05 03 02 40 00 00 00 00 00 00 07 05 83 02 40 00   ...@..........@.
                           00 00 00 00 00 07 05 84 03 40 00 0A 00 00 00 00   .........@......

 

Link to comment
Share on other sites

Hello You need to do this _libusb_get_device_descriptor then _libusb_get_string_descriptor_ascii. Here is are the function I wrote time ago let me know if they work.

Func _libusb_get_device_descriptor($hDevice, ByRef $tDeviceDescriptor)
    Local $aCall = DllCall($g_hlibusb, "int", "libusb_get_device_descriptor", "ptr", $hDevice, "ptr", DllStructGetPtr($tDeviceDescriptor))
    If @error Or Not $aCall[0] = $LIBUSB_SUCCESS Then Return SetError(1, 0, 0)
    Return 1
EndFunc   ;==>_libusb_get_device_descriptor

Func _libusb_get_string_descriptor_ascii($hDeviceHandle, $iDesc_Index)
    Local $tDescription=DllStructCreate("char String[256]")
    Local $aCall = DllCall($g_hlibusb, "int", "libusb_get_string_descriptor_ascii", "ptr", $hDeviceHandle, "int", $iDesc_Index, "ptr", DllStructGetPtr($tDescription), "int", 256)
    If @error Or Not ($aCall[0] > 1) Then Return SetError(1, 0, "")
    Return DllStructGetData($tDescription,1)
EndFunc   ;==>_libusb_get_string_descriptor_ascii



;Usage would be
_libusb_get_device_descriptor($hDeviceHandle , $tDeviceDescriptor)
local $sManufacturer=_libusb_get_string_descriptor_ascii($hDeviceHandle, $tDeviceDescriptor.iManufacturer)

Saludos

Link to comment
Share on other sites

Hi,Danyfirex,The function call still fails.

$hDeviceHandle:0x00E15B28
!>21:03:11 AutoIt3.exe End.rc:-1073741819

Quote
Func _Test()
    _libusb_init()
    Local $ahDevicesList = _libusb_get_device_list()
    If @error Then MsgBox(4112,@error,_WinAPI_GetLastError())
    _ArrayDisplay($ahDevicesList,$ahDevicesList[1])

    $hDeviceHandle=$ahDevicesList[1]

    ConsoleWrite("$hDeviceHandle:"&$ahDevicesList[1] &@CRLF)

    Local $tagSTRUCT1 = "struct;byte blength;byte bdescriptortype;word bcdusb;byte bdeviceclass;byte bdevicesubclass;byte bdeviceprotocol;byte bmaxpacketsize0;word idvendor;word idproduct;word bcddevice;byte imanufacturer;byte iproduct;byte iserialNumber;byte bnumconfigurations;endstruct"
    Local $tDeviceDescriptor = DllStructCreate($tagSTRUCT1)
    
    ;If tDeviceDescriptor is empty, the same is returned.
    
    _libusb_get_device_descriptor($hDeviceHandle , $tDeviceDescriptor)
    If @error Then ConsoleWrite("Error _libusb_get_string_descriptor_ascii"&@CRLF)
    
    local $sManufacturer=_libusb_get_string_descriptor_ascii($hDeviceHandle, $tDeviceDescriptor.iManufacturer)
    If @error Then ConsoleWrite("Error="&@error&@CRLF)
     ConsoleWrite("$sManufacturer="&$sManufacturer&@CRLF)

    _libusb_free_device_list($ahDevicesList)
    _libusb_exit()
EndFunc   ;==>_Test

 

 

Link to comment
Share on other sites

Send a PM. so I would like connect remotely to check deeply about the issue.

 

Saludos

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...