Jump to content

DllCall


Recommended Posts

I'm trying to make a DLL to use with DllCall. I'm using Dev-cpp. Here is the code:

#define EXPORT __declspec(dllexport)

#include <cstdlib>
#include <cstdio>

extern "C"
{
       EXPORT int teste(int argumento)
       {
              return 23;
       }
}

And a test script calling the function:

$retorno = DllCall ("functions.dll","int","teste","int",10)
If @error <> 0 Then
    MsgBox(0,"","Error:" & @CRLF & @error)
Else
    MsgBox(0,"",$retorno[0])
EndIf

It gives a "This program found a problem..." Windows screen.

If I call the function without any arguments:

$retorno = DllCall ("functions.dll","int","teste")

it works and returns the correct value.

What am I doing wrong when using arguments?

Thanks.

Link to comment
Share on other sites

possibly the compiler removes the argument when it is not used. Just try something like:

#define EXPORT __declspec(dllexport)

#include <cstdlib>
#include <cstdio>

extern "C"
{
       EXPORT int teste(int argumento)
       {
              argumento = argumento;
              return 23;
       }
}
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

ah, i forgot. you are using extern C. So you have to use 'int:cdecl'. It is just strange that it works when you completely remove the param. That irritated me.

$retorno = DllCall ("functions.dll","int:cdecl","teste","int",10)

*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

ah, i forgot. you are using extern C. So you have to use 'int:cdecl'. It is just strange that it works when you completely remove the param. That irritated me.

Worked great, thanks!

It's in the function documentation, I should read it more carefully next time.

Link to comment
Share on other sites

It is just strange that it works when you completely remove the param. That irritated me.

Hi ProgAndy,

As I see it, in the case of parameterless functions the stack is essentially empty, hence the cleaning specification (cdecl/stdcall) being about the stack layout and who cleans it is irrelevant.

I won't dig into that, but isn't it the case that some compilers decorate function names differently depending on the calling convention? In whichcase, this wouldn't work with those compilers. Anyway, not declaring the correct calling convention is a perversion.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

ah, i forgot. you are using extern C. So you have to use 'int:cdecl'.

Just a quick correction, extern "C" prevents C++ name mangling, it has nothing to do with calling convention :D

If you leave out any calling convention from the declaration then the compiler will use the default one, which in most cases is cdecl.

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

Link to comment
Share on other sites

Just a quick correction, extern "C" prevents C++ name mangling, it has nothing to do with calling convention :D

If you leave out any calling convention from the declaration then the compiler will use the default one, which in most cases is cdecl.

I thought if nothing specified, the convention is stdcall when using c++ names and cdecl when using extern "C", but you can override this.

Hi ProgAndy,

As I see it, in the case of parameterless functions the stack is essentially empty, hence the cleaning specification (cdecl/stdcall) being about the stack layout and who cleans it is irrelevant.

I won't dig into that, but isn't it the case that some compilers decorate function names differently depending on the calling convention? In whichcase, this wouldn't work with those compilers. Anyway, not declaring the correct calling convention is a perversion.

Yeah, but i thought calling a function that has parameters would crash if no parametesr were specified. Ah, I see, when you don't use them it doesn't care whether they are given or not. 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

Just a little clarification, when I wrote "some compilers decorate function names differently depending on the calling convention" I was refering to the prefix (mostly _), not about name mangling C++ introduced, which is something completely different as Monoceres just pointed out.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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

  • Recently Browsing   0 members

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