BlankMind Posted January 31, 2010 Posted January 31, 2010 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]) EndIfIt 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.
ProgAndy Posted January 31, 2010 Posted January 31, 2010 (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 January 31, 2010 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
danielkza Posted January 31, 2010 Posted January 31, 2010 @ProgAndy: The programmer of a compiler that removes function parameters at will should be beaten to death.
BlankMind Posted January 31, 2010 Author Posted January 31, 2010 possibly the compiler removes the argument when it is not used.Nothing has changed. I've already tested the function actually using the argument.
ProgAndy Posted January 31, 2010 Posted January 31, 2010 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
BlankMind Posted January 31, 2010 Author Posted January 31, 2010 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.
jchd Posted January 31, 2010 Posted January 31, 2010 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 hereRegExp tutorial: enough to get startedPCRE 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)
monoceres Posted January 31, 2010 Posted January 31, 2010 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 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!
ProgAndy Posted January 31, 2010 Posted January 31, 2010 (edited) Just a quick correction, extern "C" prevents C++ name mangling, it has nothing to do with calling convention 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 January 31, 2010 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
jchd Posted January 31, 2010 Posted January 31, 2010 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 hereRegExp tutorial: enough to get startedPCRE 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)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now