Sign in to follow this  
Followers 0
BlankMind

DllCall

10 posts in this topic

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.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

@ProgAndy:

The programmer of a compiler that removes function parameters at will should be beaten to death.

Share this post


Link to post
Share on other sites

possibly the compiler removes the argument when it is not used.

Nothing has changed. I've already tested the function actually using the argument.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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)

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

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

Share this post


Link to post
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)

Share this post


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
Sign in to follow this  
Followers 0