Sign in to follow this  
Followers 0
ZiggyStardust

Lost with Dlls

4 posts in this topic

I just don't get it. My head is swimming. I have been out of programming for 15 years and have forgotten more then I ever knew. I love AutoIt but just don't get working with Dlls. I am looking at it as passing variables to a function and the function return the results and just don't get the general concept. I look at a dllcand can't figure out what parameters are going up to the dll and what or where the results are comming back. Can someone explain it in newbie terms?

Anyway ... I am trying to write a script to determine is a security dongle is present in the computer or not. I have the dll (sdk.dll) loaded and just can't seem to get the calls right. Here is a description of two simple calls from the security dongle manugacturer:

// ====================================================================

EXTERN_C int WINAPI SDX_Find();

// Find SecureDongle X attached to the computer

// Return value:

// <0 Error code

// =0 No SecureDongle X attached

// >0 The number of attached SecureDongle X(s)

// ====================================================================

EXTERN_C int WINAPI SDX_Open(int mode, DWORD uid, DWORD* hid);

// Open specified SecureDongle X

// Input:

// mode -- This parameter indicates the way to open SDX

// mode > 0, open the SDX according to the UID. The mode value is the SDX number,

// for example: uid=12345678, mode=2, this will open the second SDX with UID 12345678

// mode = -1, open the SDX according to the HID, and *hid can not be 0

// Define a Constant for ease of use:

#define HID_MODE -1

// uid -- UserID,You need to specify the SDX UID and this UID is generated with SDX_GenUID

// hid -- Hardware ID,Open SDX with HID of *hid

// The SDX HID will be returned to *hid regardless of how the SDX was opened.

// Return value:

// >=0 Success. The opened SDX handle is returned.

// < 0 Error code. Please refer to latter section

// ====================================================================

I am simple trying to start with the sdx_find() and can't get anywhere. Where do you start on something like this?

Share this post


Link to post
Share on other sites



Try this first at look where it fails, if at all.

Local $hDll = DllOpen("sdk.dll")
If $hDll < 0 Then
ConsoleWrite("Unable to locate or load sdk.dll" & @LF)
Else
Local $ret = DllCall($hDll, "int", "SDX_Find")
If @error Then
  ConsoleWrite("DllCall to SDX_Find() failed. @error set to " & @error & @LF)
Else
  Select
   Case $ret[0] < 0
    ConsoleWrite("SDX_Find() returned error " & $ret[0] & @LF)
   Case $ret[0] = 0
    ConsoleWrite("SDX_Find() didn't find any SecureDongle X attached." & @LF)
   Case Else
    ConsoleWrite("SDX_Find() found " & $ret[0] & " SecureDongle X attached." & @LF)
  EndSelect
EndIf
EndIf

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

I am blurry-eyed. That worked. I read the docs on the DllCall a million times and couldn't get it. Still a little confused but it is starting to make come sense. I continued on to the SDX_Open function above and used:

$ret = DllCall($hDll, "int", "SDX_Open", "int*", 1, "DWORD*", 715400947, "DWORD*", 0) assuming my userid is 715400947

Doesn't seem to work. I am a little confused as to when to use the asterick in the parameter type and when not to. Does the above line look like it should work?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

thing means, well: a thing

thing* means a "pointer to thing"

You need to conform to the API (C definition) of the function and pass stuff or stuff* accordingly.

Your code (incorrectly) passes a pointer containing 1 as the mode parameter. Of course accessing address 1 is illegal. Same problem with the SDX UID.

Since the function returns the HID value thru the supplied pointer, it has to point to something you own and where you can later read the HID supplied.

Local $hDll = DllOpen("sdk.dll")
If $hDll < 0 Then
ConsoleWrite("Unable to locate or load sdk.dll" & @LF)
Else
Local $ret = DllCall($hDll, "int", "SDX_Find")
If @error Then
  ConsoleWrite("DllCall to SDX_Find() failed. @error set to " & @error & @LF)
Else
  Select
   Case $ret[0] < 0
    ConsoleWrite("SDX_Find() returned error " & $ret[0] & @LF)
   Case $ret[0] = 0
    ConsoleWrite("SDX_Find() didn't find any SecureDongle X attached." & @LF)
   Case Else
    ConsoleWrite("SDX_Find() found " & $ret[0] & " SecureDongle X attached." & @LF)
    Local $tHID = DllStructCreate("DWORD")
    ; only works with latest AutoIt (no need to use DllStructGetPtr)
    $ret = DllCall($hDll, "int", "SDX_Open", "int", 1, "DWORD", 715400947, "DWORD*", $tHID)
    If @error Then
     ConsoleWrite("DllCall to SDX_Find() failed. @error set to " & @error & @LF)
    Else
     If $ret[0] < 0 Then
      ConsoleWrite("SDX_Open failed or no more SecureDongle X attached." & @LF)
     Else
      Local $HID = DllStructGetData($tHID, 1)
      ConsoleWrite("SDX_Open succeeded. HID is " & $HID & @LF)
      ; keep on using the stuff
     EndIf
    EndIf
  EndSelect
EndIf
EndIf

I'm showing basic program structure with a number of nested If, but you may rearrange that according to your application logic.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
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
Sign in to follow this  
Followers 0