Solved

STDCALL and CDECL confusion

Posted on 1998-04-29
2
5,418 Views
Last Modified: 2012-05-04
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(sPascalString) + 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.
0
Comment
Question by:andrewjackson
2 Comments
 
LVL 4

Accepted Solution

by:
d003303 earned 200 total points
ID: 1339338
Yo,
stdcall and cdecl function calls are stubbed by the compiler, so you do not have to care about the low-level stuff or need special adjustments for the different call styles. Cleanup means that you put your parameters on the stack before you call the function, and after the function returns you have to remove the parameters by yourself. In stdcall, the parameters are removed by the called function.
To debug this low-level stuff, I recommend to add the following value to the registry. In
HKEY_CURRENT_USER\Software\Borland\Delphi\3.0\Debugging
create a new string value called EnableCPU withthe value 1. Now you can see the status of all CPU registers and the assember code (View->CPU window).
Compile your app with the library function first as cdecl, second as stdcall, set a breakpoint before the call and see if the stack is correctly restored after the call.

Slash/d003303
0
 
LVL 1

Expert Comment

by:ergates
ID: 1339339
Can you post the the C declaration for the DLL routine?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question