Jump to content

Calling a DLL I write in VC++


 Share

Recommended Posts

I'm taking an intermediate C++ course in college, and I'm wondering how to write a DLL that can be called within AutoIt3. I tried to make a very simple dll with a function that adds two integers, however when I try to call it from within AutoIt, the script either locks up or throws an error 3 ("function" not found in the DLL file). I was able to compile an AutoIt plugin that worked fine, but I can't get a standard DLL to work ...

Here is the dll (test dll.cpp):

__declspec(dllexport) int TestFunc(int x, int y)
{
    return x + y;
}

And here is the script:

$dll = DllOpen("Test DLL.dll")
if (@error) Then FatalError("Unable to open DLL")

$result = DllCall($dll, "int", "TestFunc", "int", 3, "int", 4)
if (@error) Then FatalError("Error calling DLL: " & @error)

MsgBox(0, "Result", $result[0])

Func FatalError($msg)
    MsgBox(0, "Fatal Error", $msg)
    Exit
EndFunc

Any help would be much appreciated. Thanks, Andy

Edited by CoderDunn
Link to comment
Share on other sites

What calling convention are you using in your project settings? If cdecl, then your second argument to DllCall has to be "<return_type>:cdecl".

I figured it out, thanks! I changed the function to use a stdcall and a .def file to export the function, and now it works :idea:

int _stdcall TestFunc(int x, int y)
{
    return x + y;
}
Edited by CoderDunn
Link to comment
Share on other sites

  • 3 months later...

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...