andrewjackson
asked on
STDCALL and CDECL confusion
Can someone please explain what it means in programming terms when I use STDCALL or CDECL when calling a C/C++ DLL function...
I've looked in the Delphi help files and, as I understand it, the only difference between these two conventions is that for STDCALL the DLL handles the stack 'cleanup', whereas in the CDECL case the calling (i.e Delphi) function is responsible for this. But what does 'cleanup' actually mean in this context? Is it the same as freeing dynamically allocated memory? If so, does that mean that I need to write my Delphi routines differently for STDCALL and CDECL conventions, or is the function I've written below valid in both cases and the Delphi compiler handles all the low level differences for me?
function SomeFunction(const sPascalString : string);
var
szCString : PChar;
iResult : integer;
begin
{ allocate memory for C-style string }
szCString := StrAlloc(Length(sPascalStr ing) + 1);
StrPCopy(szCString, sPascalString);
try
{ call DLL function which has been GetProcAddress'd earlier }
iResult := fnDLLFunction(szCString);
ShowMessage('the result is ' + IntToStr(iResult));
finally
{ tidy up }
StrDispose(szCString);
end;
end;
The reason I ask is that I'm trying to call a C++ DLL function which is DEFINATELY of the CDECL style in the way shown above but seem to get EInvalidPointerOperation errors on exiting my function. I presume this is stack corruption. These don't arise if I use STDCALL. It doesn't make sense!
Thanks in advance for any help.
I've looked in the Delphi help files and, as I understand it, the only difference between these two conventions is that for STDCALL the DLL handles the stack 'cleanup', whereas in the CDECL case the calling (i.e Delphi) function is responsible for this. But what does 'cleanup' actually mean in this context? Is it the same as freeing dynamically allocated memory? If so, does that mean that I need to write my Delphi routines differently for STDCALL and CDECL conventions, or is the function I've written below valid in both cases and the Delphi compiler handles all the low level differences for me?
function SomeFunction(const sPascalString : string);
var
szCString : PChar;
iResult : integer;
begin
{ allocate memory for C-style string }
szCString := StrAlloc(Length(sPascalStr
StrPCopy(szCString, sPascalString);
try
{ call DLL function which has been GetProcAddress'd earlier }
iResult := fnDLLFunction(szCString);
ShowMessage('the result is ' + IntToStr(iResult));
finally
{ tidy up }
StrDispose(szCString);
end;
end;
The reason I ask is that I'm trying to call a C++ DLL function which is DEFINATELY of the CDECL style in the way shown above but seem to get EInvalidPointerOperation errors on exiting my function. I presume this is stack corruption. These don't arise if I use STDCALL. It doesn't make sense!
Thanks in advance for any help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you post the the C declaration for the DLL routine?