Jump to content

Recommended Posts

Posted (edited)

I created a DLL in visual c++ with the express intent of calling it from Autoit. However, @error keeps getting set to 1 when I try to call it in my program.

Here is the code:

$myDll=dllOpen("testbasicdll.dll")

$test=dllCall("testbasicdll.dll","int","addnums","int",2,"int",3)

msgbox(0,@error,$test)

This is the code for the DLL:

#include "stdafx.h"

int returnval;

__declspec(dllexport) int addnums(int one, int two)
{
    returnval=one+two;
    return returnval;
}

And this is Exports.def:

LIBRARY testbasicdll
EXPORTS
     addnums

I have the .dll placed in the same directory as the Autoit script. I compiled the dll in visual c++ express edition 2008.

EDIT: I just realized that dllopen is returning -1. Anyone know why that is?

Edited by sharrakor
Posted

Add ' extern "C" ' to the function declaration, and since you're using the cdecl calling convention you need to add ":cdecl" to the return type in the DllCall() function.

Also, why are declaring returnval as global variable and you really don't need that exports.def file.

Broken link? PM me and I'll send you the file!

Posted (edited)

Add ' extern "C" ' to the function declaration, and since you're using the cdecl calling convention you need to add ":cdecl" to the return type in the DllCall() function.

Also, why are declaring returnval as global variable and you really don't need that exports.def file.

I just saw a simple tutorial on how to create a basic dll and that was how they said to do it. Glad to know I don't need the exports file.

When you say add extern, do you mean like this? Sorry, I haven't used extern before. This gives me an error still.

extern __declspec(dllexport) int addnums(int one, int two)
Edited by sharrakor
Posted

I just saw a simple tutorial on how to create a basic dll and that was how they said to do it. Glad to know I don't need the exports file.

When you say add extern, do you mean like this? Sorry, I haven't used extern before. This gives me an error still.

extern __declspec(dllexport) int addnums(int one, int two)
Not quite.

Please see this post for a description of how to create a dll and call it

Broken link? PM me and I'll send you the file!

Posted

Ok, I saw that, and I'm still having issues. The thing is, I'm getting -1 as my dllopen return value, so I'm assuming its some other sort of error. Opening user32.dll works fine.

The dll is in the same directory as the script, so it shouldn't be that. My project is a standard DLL that doesn't export any symbols.

I'm running XP btw. Any ideas?

Posted

The following code works without problems with your dll:

$handle=DllOpen("dlltest2.dll")
MsgBox(0,"",$handle)

$call=DllCall($handle,"int:cdecl","addnums","int",3,"int",4)
MsgBox(0,"",$call[0])

Broken link? PM me and I'll send you the file!

Posted

Hmm... copied and pasted it and it still gave me -1. Where are you storing the DLL file? Are you running it uncomplied? This is weird.

In the script directory. Even though unlikely the problem, try link the dll statically with the runtime.

Project Properties -> C/C++ -> Code generation -> Runtiem library -> change to just "multi-threading"

Broken link? PM me and I'll send you the file!

Posted

I am. I'm on windows XP 64 bit.

Calling any other DLL works fine for me though. I say this because I've got an appointment to go to now... So I won't be able to respond for a while.

So, might I ask what the problem is and how I fix it? :D

Thanks

Posted

64 bit applications cannot load 32 bit libraries and vice versa.

To fix this you either compile the dll as a 64 bit library or you run the 32 bit version of autoit (32 bit apps can still run on 64 bit os's).

Broken link? PM me and I'll send you the file!

Posted

It has to do with the width of pointers. In a real 64 bit application, pointers are 64 bits wide. In 32 bit applications, 32 bit pointers. When initializing the dll, the dll gets an HINSTANCE value, which is a pointer. If it's the wrong width, the function call fails, and the dll is not loaded.

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
×
×
  • Create New...