Solved

STDCALL and CDECL confusion

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now