Sign in to follow this  
Followers 0
tkfu

New to AutoIT, trying to figure out DllCall

4 posts in this topic

Hi. I'm new to AutoIT and liking the ease of use and functionality. I just can't quite figure out how to properly work with DLLs. Basically, I'm trying to replicate this functionality in AutoIT (this is from Powershell):

[void][System.Reflection.Assembly]::LoadFile("path\to\smsclictr.automation.dll")
$SMSClient = New-Object -TypeName smsclictr.automation.SMSClient("some remote host")
$SMSClient.SoftwareDistribution.RerunAdv("blah", "blah", "blah")

Share this post


Link to post
Share on other sites



FROM HELPFILE: Learn it Love it Use it!!!

Function Reference

DllCall

--------------------------------------------------------------------------------

Dynamically calls a function in a DLL.

DllCall ( "dll", "return type", "function" [, "type1", param1 [, "type n", param n]] )

Parameters

dll The filename of the DLL to use. e.g. "user32.dll". A handle obtained from DllOpen can also be used (See Remarks).

return type The return type of the function (see below).

function The name, eg. "MessageBox" or the ordinal value, e.g. 62, of the function in the DLL to call.

type [optional] The type of the parameter (see remarks).

param [optional] The actual parameter (see remarks).

Valid Types are:

Type Details

none no value (only valid for return type - equivalent to void in C)

byte a 8 bit integer

ubyte an unsigned 8 bit integer

short a 16 bit integer

ushort an unsigned 16 bit integer

dword an unsigned 32 bit integer

udword an unsigned 32 bit integer

int a 32 bit integer

uint an unsigned 32 bit integer

long a 32 bit integer

ulong an unsigned 32 bit integer

int64 a 64 bit integer

uint64 an unsigned 64 bit integer

str an ANSI string (cannot be more than 65536 chars).

wstr a UNICODE wide character string (converted to/from an ANSI string during the call if needed). Cannot be more than 65536 chars.

hwnd a window handle (pointer)

ptr a general pointer (void *)

float a single precision floating point number

double a double precision floating point number

lresult/int_ptr/long_ptr an integer big enough to hold a pointer when running on x86 or x64 versions of AutoIt.

lparam/int_ptr/long_ptr an integer big enough to hold a pointer when running on x86 or x64 versions of AutoIt.

wparam/uint_ptr/ulong_ptr an unsigned integer big enough to hold a pointer when running on x86 or x64 versions of AutoIt.

* Add * to the end of another type to pass it by reference. For example "int*" passes a pointer to an "int" type.

Conversions from Windows API types to AutoIt types:

WINDOWS API Type AutoIt Type

LPCSTR/LPSTR str

LPCWSTR/LPWSTR wstr

LPVOID ptr

HWND hwnd

WPARAM wparam

LPARAM lparam

DWORD dword

LPDWORD dword*

HANDLE/HINSTANCE ptr

LONGLONG/LARGE_INTEGER int64

ULONGLONG/ULARGE_INTEGER uint64

UINT_PTR wparam

LONG_PTR lparam

Return Value

Success: @error = 0.

Failure: @error = 1 unable to use the DLL file,

@error = 2 unknown "return type",

@error = 3 "function" not found in the DLL file.

See remarks.

Remarks

If a dll filename is given then the DLL is automatically loaded and then closed at the end of the call. If you want to manually control the loading and unloading of the DLL then you should use DllOpen and DllClose and use a handle instead of a filename in this function.

By default, AutoIt uses the 'stdcall' calling method. To use the 'cdecl' method place ':cdecl' after the return type.

DllCall("SQLite.dll", "int:cdecl", "sqlite3_open", "str", $sDatabase_Filename , "long*", 0).

By default, AutoIt tries to use the ANSI version of a function name, i.e. MessageBoxA is attempted when MessageBox is given as the function name. To call the unicode version use MessageBoxW.

If the function call fails then @error is set to 1. Otherwise an array is returned that contains the function return value and a copy of all the parameters (including parameters that the function may have modified when passed by reference).

$return[0] = function return value

$return[1] = param1

$return[2] = param2

...

$return[n] = paramn

Related

DllCallbackFree, DllCallbackGetPtr, DllCallbackRegister, DllOpen, DllClose, DllStructCreate, DllStructGetPtr

Example

; *******************************************************
; Example 1 - calling the MessageBox API directly
; *******************************************************

$result = DllCall("user32.dll", "int", "MessageBox", "hwnd", 0, "str", "Some text", "str", "Some title", "int", 0)


; *******************************************************
; Example 2 - calling a function that modifies parameters
; *******************************************************

$hwnd = WinGetHandle("[CLASS:Notepad]")
$result = DllCall("user32.dll", "int", "GetWindowText", "hwnd", $hwnd, "str", "", "int", 32768)
msgbox(0, "", $result[0])   ; number of chars returned
msgbox(0, "", $result[2])   ; Text returned in param 2


; *******************************************************
; Example 3 - Show the Windows PickIconDlg
; *******************************************************

$sFileName  = @SystemDir & '\shell32.dll'

; Create a structure to store the icon index
$stIcon     =  DllStructCreate("int")
$stString       = DLLStructCreate("wchar[260]")
$structsize = DllStructGetSize($stString)/2
DllStructSetData($stString, 1, $sFileName)

; Run the PickIconDlg - '62' is the ordinal value for this function
DllCall("shell32.dll", "none", 62, "hwnd", 0, "ptr", DllStructGetPtr($stString), "int", $structsize, "ptr", DllStructGetPtr($stIcon))

$sFileName  = DllStructGetData($stString, 1)
$nIconIndex = DllStructGetData($stIcon, 1)

; Show the new filename and icon index
Msgbox(0, "Info", "Last selected file: " & $sFileName & @LF & "Icon-Index: " & $nIconIndex)

[Cheeky]Comment[/Cheeky]

Share this post


Link to post
Share on other sites

FROM HELPFILE: Learn it Love it Use it!!!

...

Gee, thanks. I never would have thought to look at the doco!

If I just needed to call a method within the dll and get a return value, that would be helpful. As you can see from the code sample, however, I need to get an object from that dll, then call a method of that object. That's the bit I can't figure out. And yes, I've read the ObjGet doco as well, but I don't understand it, at least as far as how it applies to my particular situation. If someone could provide a line of sample code for how to get that object, i would be most appreciative.

Share this post


Link to post
Share on other sites

The function to return the object will be specified in the API. You will need to specify the return value as that data type will be returned and needs to match.

In your case you will have to use a pointer, then reference the pointer to get to the object. What is the name of the dll and function prototype? It should be something int LoadFile(string str).


Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

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