Jump to content

DLL development questions (return value and callback)


jtredez
 Share

Recommended Posts

Hello,

I would like to develop a DLL for autoit but I have some questions.

I use Autoit 3.3.9.0beta and codeblock with mingw for the DLL part.

I have the DLL, a external program developped in C to test this DLL and an autoit program to test this DLL.

The DLL have only one function: addition to add two integer and return the result.

This is part of my source code (I put the full project in attachment):

int DLL_EXPORT addition(int integer1, int integer2) {
    return integer1 + integer2;
}
#define DLL_EXPORT __declspec(dllexport)
#ifdef __cplusplus
extern "C"
{
#endif
int DLL_EXPORT addition(int integer1, int integer2);
#ifdef __cplusplus
}
#endif
local $hDll = DllOpen("LibTest.dll")
$result = DllCall($hDll, "int:cdecl","addition", "int",3, "int",7)
MsgBox(4096, "In AutoIt", "3 + 7 = " & $result & @CRLF & "error = " & @error)
DllClose($hDll)
Exit
  • The returned value on my DLL function is correct (=10) with my C test program but not in autoit (=0). The parameters is correctly passed to the DLL in both case (I display it in the DLL with a msgbox). I have tested with __stdcall and without. What is wrong with my code?
  • When I use the __stdcall version, my DLL function name change to addition@8, why this? and how to avoid this? (sorry I not realy familiar with DLL development).
  • I would like to call an autoit function in my DLL. I suppose I need to use the DllCallbackRegister to get a pointer on my autoit function and to pass it to a function in my DLL, like what it’s done in the Example of DllCallbackRegister with EnumWindows. But in my DLL how I call this pointer? Does I need a specific mechanism or I can call it like another C function pointer? And how to pass parameters to the autoit callback function and eventualy get a return value?
Thank you for your help.

dll.zip

Link to comment
Share on other sites

The returned value on my DLL function is correct (=10) with my C test program but not in autoit (=0). The parameters is correctly passed to the DLL in both case (I display it in the DLL with a msgbox). I have tested with __stdcall and without. What is wrong with my code?

jtredez, Unless the call to DllCall() fails, the function will return an array. Your dll is fine. Just change the return value in the msgbox to :

MsgBox(4096, "In AutoIt", "3 + 7 = " & $result[0] & @CRLF & "error = " & @error)

When I use the __stdcall version, my DLL function name change to addition@8, why this? and how to avoid this? (sorry I not realy familiar with DLL development).

You can read about why this happens here.
Link to comment
Share on other sites

for #3:

Func Myau3Func($a, $b, $c)
    ConsoleWrite("Myfunc called with a = " & $a & " b = " & $b & " c = " & $c & @LF)
    Return $a + $b + $c
EndFunc
$cb = DllCallBackRegister("Myau3Func","int","int;int;int")
$pcb = DllCallBackGetPtr($cb)
DllCall($yourdll, "void:cdecl", "OnAutoItFunc","ptr",$pcb)
While sleep(10)
Wend
;C dll :
typedef int (_stdcall * au3_func)(int,int,int);

void _cdecl OnAutoItFunc(void * func) {
    int ret = 0;
    if(func) {
        au3_func MyAu3Func = (au3_func) func;
        ret  = MyAu3Func(10,20,30);
    }
}

e: symbol issues

Edited by Shaggi

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Link to comment
Share on other sites

Thank you, your answers helped me a lot.

Just one more clarification for the question #2:

Now I understand why I have this "name-decoration".

If I compile my DLL by declaring the function with __stdcall, when I call the function in autoit I need to do it like this:

$result = DllCall($hDll, "int","addition@8", "int",3, "int",7)

The default call convention on autoit is __stdcall, so I assumes I doesn't need to put the "name-decoration" to find this function, but it's not the case.

I would like to write it like this (without the :cdecl and without the @8):

$result = DllCall($hDll, "int","addition", "int",3, "int",7)

Link to comment
Share on other sites

If your function is cdecl, then you must use :cdecl in AutoIt. The extra 6 characters are really that much work?

If that's the case then I'm sure setting up a modules.def file in your C IDE will be too much as well... (hint)

Edited by wraithdu
Link to comment
Share on other sites

Since you are using MinGW, you could use the switch --add-stdcall-alias or --kill-at.

http://forums.codeblocks.org/index.php?topic=2616.msg20754#msg20754

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

I have found a usefull documentation on how to write DLL with MinGW (in case someone interested):

http://www.transmissionzero.co.uk/computing/building-dlls-with-mingw/

http://www.transmissionzero.co.uk/computing/advanced-mingw-dll-topics/

MinGW doesn't have the same "name-decoration" as VisualC++ and need Definition file (thank wraithdu to point me on this) to solve this issue (see link #2 for explanation).

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...