Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

STDCALL and CDECL confusion

Posted on 1998-04-29
2
Medium Priority
?
5,860 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
d003303 earned 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

705 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