Jump to content

LIB USB - DLL C for loop with structures.


 Share

Recommended Posts

Hi, I have been playing arround with libusb0-win32 and was/am attempting to make the starts of a udf library as I haven't found one and it would come in handy for my current project.

I am having trouble figuring out how I would convert this code:

http://libusb.sourceforge.net/doc/examples-code.html

more notably this section

struct usb_bus *bus; int c, i, a; /* ... */ for (bus = busses; bus; bus = bus->next) { struct usb_device *dev; for (dev = bus->devices; dev; dev = dev->next) {

to work with autoit.

At the moment I have this code:

libusb0.au3

#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.4.0 Author:    Craig Gilhooley Script Function:    libusb0 Functions. #ce ---------------------------------------------------------------------------- ; Script Start $libusb0 = DllOpen("libusb0.DLL") Func usb_init()    DllCall ($libusb0, "none", "usb_init") EndFunc Func usb_find_busses()   $retval = DllCall ($libusb0, "int", "usb_find_busses")  Return $retval EndFunc Func usb_find_devices()  $retval = DllCall ($libusb0, "int", "usb_find_devices")     Return $retval EndFunc Func usb_get_busses()    $retval = DllCall ($libusb0, "ptr", "usb_get_busses")   Return $retval EndFunc Func usb_open($devicePointer)    $retval = DllCall ($libusb0, "int", "usb_open", "ptr", $devicePointer)  Return $retval EndFunc Func usb_close($devicePointer)   $retval = DllCall ($libusb0, "int", "usb_close", "ptr", $devicePointer)     Return $retval EndFunc

libusb test.au3

#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.4.0 Author:    Craig Gilhooley Script Function:    libusb0 test. #ce ---------------------------------------------------------------------------- ; Script Start #include <Array.au3> #include "libusb0.au3" usb_init() usb_find_busses() usb_find_devices() $busses = usb_get_busses() $bus = DllStructCreate("ptr;ptr;char[512];ptr;ULONG;ptr;", $busses[0]) MsgBox(0, "Busses Pointer", $busses[0]) $next = DllStructGetPtr($bus, 1) MsgBox(0, "Next Pointer", $next) $previous = DllStructGetPtr($bus, 2) MsgBox(0, "Previous Pointer", $previous) $path = DllStructGetData($bus, 3) MsgBox(0, "Path", $path) $devices = DllStructGetPtr($bus, 4) MsgBox(0, "Devices Pointer", $devices) $location = DllStructGetData($bus, 5) MsgBox(0, "Location", $location) $root_dev = DllStructGetPtr($bus, 6) MsgBox(0, "Root Device Pointer", $root_dev) $dev = $devices MsgBox(0, "Device Pointer", $dev) $dev_handle = usb_open($dev) MsgBox(0, "Device Handle", $dev_handle) $closed = usb_close($dev) MsgBox(0, "Device Closed", $closed) ;_ArrayDisplay($busses)

As I am testing it but I cant seem to get the usb_open to work and I dont have a clue how to convert the code I showed at the top of the post.

I have found relevant documentation here:

http://libusb.sourceforge.net/doc/

And I got the information about the structure of the STRUCT's from libusb.h, I beleive that the more relevant ones are:

struct usb_device { struct usb_device *next, *prev; char filename[LIBUSB_PATH_MAX]; struct usb_bus *bus; struct usb_device_descriptor descriptor; struct usb_config_descriptor *config; void *dev;      /* Darwin support */ unsigned char devnum; unsigned char num_children; struct usb_device **children; }; struct usb_bus { struct usb_bus *next, *prev; char dirname[LIBUSB_PATH_MAX]; struct usb_device *devices; unsigned long location; struct usb_device *root_dev; };

Here is a link to the DLL:

http://sourceforge.net/projects/libusb-w...in32-device-bin-0.1.12.2.tar.gz/download

And a link to the src (libusb.h):

http://sourceforge.net/projects/libusb-w...ibusb-win32-src-0.1.12.2.tar.gz/download

Any help or information towards my goal is appreciated.

Thanks,

Craig.

Edited by FaT3oYCG

Interpreters have great power!Although they live in the shadow of compiled programming languages an interpreter can do anything that a compiled language can do, you just have to code it right.

Link to comment
Share on other sites

An advice: paste your code after creating the autoit tags, and it won't be transformed into that that unreadable mess.

Edited by AdmiralAlkex
Link to comment
Share on other sites

  • 1 month later...

Hi, sorry I didn't pick up on this earlier, I went away to try and fix it on my own and did not realise the readability issues and since got distracted.

I currently have the functions for initialising the lib and finding the busses and devices sorted (hopefully) they seem to function as described.

To put my original problem in a different way, there is a simple example provided for the lib in C which I was attempting to convert but hit a road block when it came to the for loop.

Here is the example in C:

http://libusb.sourceforge.net/doc/examples-code.html

Here are the relevant structs from the usb.h file:

struct usb_device {
 struct usb_device *next, *prev;

 char filename[LIBUSB_PATH_MAX];

 struct usb_bus *bus;

 struct usb_device_descriptor descriptor;
 struct usb_config_descriptor *config;

 void *dev;     /* Darwin support */

 unsigned char devnum;

 unsigned char num_children;
 struct usb_device **children;
};

struct usb_bus {
 struct usb_bus *next, *prev;

 char dirname[LIBUSB_PATH_MAX];

 struct usb_device *devices;
 unsigned long location;

 struct usb_device *root_dev;
};

Some functions I made for the libusb0.au3:

Func usb_init()
    DllCall($libusb0, "none", "usb_init")
EndFunc ;==>usb_init

Func usb_find_busses()
    $retval = DllCall($libusb0, "int", "usb_find_busses")
    Return $retval
EndFunc ;==>usb_find_busses

Func usb_find_devices()
    $retval = DllCall($libusb0, "int", "usb_find_devices")
    Return $retval
EndFunc ;==>usb_find_devices

My test:

$libusb0 = DllOpen("libusb0.DLL")

#include "libusb0.au3"

usb_init()
usb_find_busses()
usb_find_devices()

Which is all fine and dandy bit it doesn't do anything at the moment, looking at the documentation I am finding it hard to see how I would use the usb_get_busses() function as it seems to return a struct or the data is placed into one by reference somehow.

Again here is the documentation:

http://libusb.sourceforge.net/doc/functions.html

and the usb_get_busses() page:

http://libusb.sourceforge.net/doc/function.usbgetbusses.html

usb_get_busses

Name

usb_get_busses -- Return the list of USB busses found<a name="AEN139">

Description

struct usb_bus *usb_get_busses(void);

usb_get_busses simply returns the value of the global variable usb_busses. This was implemented for those languages that support C calling convention and can use shared libraries, but don't support C global variables (like Delphi).

Some help with this would be appreciated a lot.

Thanks,

Craig.

Interpreters have great power!Although they live in the shadow of compiled programming languages an interpreter can do anything that a compiled language can do, you just have to code it right.

Link to comment
Share on other sites

Anyone got any ideas? I am actively working on this again.

Interpreters have great power!Although they live in the shadow of compiled programming languages an interpreter can do anything that a compiled language can do, you just have to code it right.

Link to comment
Share on other sites

Well determination and lots more research seem to have eventually prospered, I have found that I can use a pointer as the second argument to the DllStructCreate() function.

This gives me this at the moment:

#include <Array.au3>

$libusb0 = DllOpen("libusb0.DLL")

#include "libusb0.au3"

usb_init()
usb_find_busses()
usb_find_devices()

$busses = usb_get_busses()

$usb_bus = DllStructCreate("ptr;ptr;char[512];ptr;ulong;ptr", $busses[0])

MsgBox(0, "ugb", IsDllStruct($usb_bus))
MsgBox(0, "ugb", DllStructGetData($usb_bus, 3))

Which is working fine.

I hope to develop this into a UDF for use in more cases but I believe that I will be able to progress and provide some examples eventually.

Thanks,

Craig.

Interpreters have great power!Although they live in the shadow of compiled programming languages an interpreter can do anything that a compiled language can do, you just have to code it right.

Link to comment
Share on other sites

  • 11 months later...
  • 2 months later...
  • 1 year later...
  • Moderators

Selevo, we discourage necroing old posts, especially when the member has not been active in 2 years. AutoIt has grown and changed a lot in two years; what may have worked in 2010 may not work the same today. If you are attempting a specific project, by all means feel free to start a new topic and post your code; we'll do our best to help :)

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • 9 months later...

Hi,

I need help to make a script which recognize PID and VID device and connect this device.

i used libusb0 but i can't find the device connected and other informations.

 

This is my code :

 can you copy and paste it to understand my problem,?..

$libusb0 = DllOpen("libusb0.DLL") 


hotkeyset("{Esc}","Terminate")
Func Terminate()
    Exit 0
EndFunc   ;==>Terminate


 Func usb_init()
 DllCall ($libusb0, "none", "usb_init") 
 EndFunc 
 
 Func usb_find_busses() 
 $retval = DllCall ($libusb0, "int", "usb_find_busses") 
 Return $retval 
 EndFunc 
 
  Func usb_device()
 $retval = DllCall ($libusb0, "Ptr", "usb_device") 
 Return $retval 
 EndFunc 
 
 Func usb_find_devices() 
 $retval = DllCall ($libusb0, "Ptr", "usb_find_devices") 
 Return $retval
 EndFunc 
 
 Func usb_get_busses() 
 $retval = DllCall ($libusb0, "ptr", "usb_get_busses")
 Return $retval 
 EndFunc 
 
 
 Func usb_open($devicePointer) 
 $retval = DllCall ($libusb0, "HWnd", "usb_open", "Ptr", $devicePointer) 
 Return $retval 
 EndFunc
  

 Func usb_close($devicePointer) 
 $retval = DllCall ($libusb0, "int", "usb_close", "ptr", $devicePointer)
 Return $retval
 EndFunc 


#include <Array.au3> 


usb_init() 
$findbus = usb_find_busses() 
$busses = usb_get_busses() 
$find = usb_find_devices() 



;*******************find busses*********************************************



$usb_find_busses = DllStructCreate("Int", $findbus[0]) 
$fbus= DllStructGetptr($usb_find_busses,1)
If($fbus=0x00000001) then
ConsoleWrite("Busses found  "& $fbus & @CRLF)
Else
ConsoleWrite("Busses Not found ...  "& $fbus & @CRLF)
EndIf


;*******************get busses*********************************************


$usb_get_busses = DllStructCreate("ptr;ptr;char[512];ptr;ULONG;ptr;", $busses[0]) 

$next = DllStructGetPtr($usb_get_busses, 1) 
MsgBox(0, "Next Pointer", $next)

$previous = DllStructGetPtr($usb_get_busses, 2) 
MsgBox(0, "Previous Pointer", $previous) 
$path = DllStructGetptr($usb_get_busses, 3) 
MsgBox(0, "Path", $path) 
$devices = DllStructGetPtr($usb_get_busses, 4) 
MsgBox(0, "Devices Pointer", $devices) 
$location = DllStructGetData($usb_get_busses, 5) 
MsgBox(0, "Location", $location) 
$root_dev = DllStructGetPtr($usb_get_busses, 6) 
MsgBox(0, "Root Device Pointer", $root_dev) 

;**********************data devices*********************************************

$usb_find_devices = DllStructCreate("Int",$find[0])
$devi = DllStructGetPtr($usb_find_devices) 
If($devi=0x00000001) then
ConsoleWrite("A device is connected  "& $devi & @CRLF)


; WORK ON IT :::
$id=0x00000001

$d = usb_open($id)
$usb = DllStructCreate("HWnd",$d[0])
$de = DllStructGetPtr($usb) 

ConsoleWrite("data" & $usb)
;*********************************


Else
ConsoleWrite("Not device...  "& @CRLF)
EndIf



;*******************************open / close*****************************************




DllClose("libusb0.DLL")
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...