Solved

STDCALL and CDECL confusion

Posted on 1998-04-29
2
5,351 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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