Jump to content

DLL development questions (return value and callback)


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 post
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 post
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 post
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 post
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 post
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 post
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 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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By bobflumox
      Hi all,
      My programming knowledge is very basic.
      I have an old script that creates shares and assign permissions. It normally registers SetAcl.ocx if necessary and creates an object to assign permissions.
      The command that registers SetAcl was apparently working fine under Windows 7 but is not working under Windows 10.
      RunWait("regsvr32.exe path\to\setacl.ocx /s", "", @SW_HIDE) As I'm logged in as admin, I changed this command to :
      RunAsWait(@UserName, "", "", 0, "regsvr32.exe path\to\setacl.ocx /s", "", @SW_HIDE) It seems to terminate correctly but the script still doesn't work as expected.
      To check that, I've created that small script :
      Local $objSetAcl = ObjCreate("SETACL.SetACLCtrl.1") If IsObj($objSetAcl) Then ConsoleWrite("Object successfully created." & @CRLF) Else ConsoleWrite("Object not created. Registering SetAcl.ocx" & @CRLF) Local $result = RunAsWait(@UserName, "", "", 0, "regsvr32.exe path\to\setacl.ocx /s", "", @SW_HIDE); Use of my admin username to elevate CMD ConsoleWrite("Return code : " & $result & @CRLF) ConsoleWrite("Creating object" & @CRLF) $objSetAcl = ObjCreate("SETACL.SetACLCtrl.1") If IsObj($objSetAcl) Then ConsoleWrite("Object successfully created." & @CRLF) Else ConsoleWrite("Object creation failed." & @CRLF) EndIf EndIf It tries to register SetAcl.ocx, return code 0 seems to be fine but still can't use SetAcl.
      But if I go to CMD as admin, run the regsvr32 command and restart my script, it can create the object without issue.
      I know my poor knowledge makes me miss something. Anyone can help me figure this out ?
    • By Gowrisankar
      Dear members of the forum,
      I'm working on a project in which I have to use Image recognition technique. 
      Due to client restrictions, I couldn't use AutoIt for this project. 
      Is there a way to use this DLL "ImageSearchDLL.dll" (which is used to do image recognition steps in AutoIt) in VB.Net to achieve the same result? 
      I have used this DLL few years before and got good results. If there is a latest version of this DLL and if you can share it, that will be helpful too.
      Any guidance is deeply appreciated.
    • By Colduction
      Hi AutoIt Scripters/Programmers. I have a question about MIME Tools for Notepad++:
      I've recently found a UDF about Base64 in forum, but they can't decode\encode correctly some emojis , other UTFs and etc. so i decided to use mimeTools.dll of Notepad++ or main site
      My problem is how to use this dll in AutoIt Language?

      I will be happy with your comments and answers❤ Thanks.
    • By FrancescoDiMuro
      Good morning Forums 
      In these days, I am working on a project that involved me to use some Windows APIs to obtain some information about Terminal Servers.
      I'm doing this using wtsapi32.dll in a VBA Project, but, the lack of knowledge about few things threated in the articles make this quite difficult to
      implement and understand at the same time.
      The most difficult thing I'm facing is "translating" C/C++ functions or struct in VBA when pointers are used, or pointers of pointers, and so on.
      Since VBA seems to not have a pointer type, to make those functions work I need to implement other functions taken from other DLLs, and this confuses me a lot.
      For example, starting from this code, I splitted all the functions and all the definitions to understand why they are there, and why I need to use them.
      At the end, I've found out that the code I was going to implement starting from the functions provided in the Microsoft Docs won't be ever be able to work without some supplementary functions which are not mentioned anywhere.
      So, I was wondering if someone would please point me out to a good and practical exaplanation about pointers (in general) or specifically for VBA, because I need to use them quite often in these days, and I'd like to understand what I am doing.
      Thanks in advance.

      Best Regards and Stay at home 🏡
       
    • By zuladabef
      All my scripts were working fine and now I am getting this error.  How do I resolve it?  Which folder(s) do the DLLs need to be in?
      ! Dll not found or Call Dll error !  
×
×
  • Create New...