Jump to content



Photo

DLL development questions (return value and callback)

Dll DllCallbackRegister CodeBlock

  • Please log in to reply
7 replies to this topic

#1 jtredez

jtredez

    Seeker

  • Active Members
  • 8 posts

Posted 15 February 2012 - 04:44 PM

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.

Attached Files

  • Attached File  dll.zip   15.21KB   107 downloads








#2 SkinnyWhiteGuy

SkinnyWhiteGuy

    Feed the bunny!

  • Active Members
  • PipPipPipPipPipPip
  • 421 posts

Posted 15 February 2012 - 05:27 PM

Reread the DllCall help entry, the result variable you have doesn't work the way you are using it.

#3 Beege

Beege

    Universalist

  • MVPs
  • 843 posts

Posted 15 February 2012 - 11:57 PM

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.

#4 Shaggi

Shaggi

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 296 posts

Posted 16 February 2012 - 12:13 AM

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, 16 February 2012 - 12:15 AM.

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

#5 jtredez

jtredez

    Seeker

  • Active Members
  • 8 posts

Posted 16 February 2012 - 03:08 PM

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)



#6 wraithdu

wraithdu

    I am less fun than a twisted ankle.

  • MVPs
  • 2,140 posts

Posted 16 February 2012 - 03:13 PM

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, 16 February 2012 - 03:14 PM.


#7 ProgAndy

ProgAndy

    You need AutoItObject

  • MVPs
  • 2,508 posts

Posted 16 February 2012 - 04:18 PM

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, 16 February 2012 - 04:20 PM.

*GERMAN* Posted Image [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

#8 jtredez

jtredez

    Seeker

  • Active Members
  • 8 posts

Posted 16 February 2012 - 05:49 PM

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





Also tagged with one or more of these keywords: Dll, DllCallbackRegister, CodeBlock

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users