Solved

STDCALL and CDECL confusion

Posted on 1998-04-29
2
5,263 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
Comment Utility
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
Comment Utility
Can you post the the C declaration for the DLL routine?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

11 Experts available now in Live!

Get 1:1 Help Now