Hi all. The code of my DLL starts with STDCALL declaration, just like the example into MASM32 package, and it works fine. But I think STDCALL is strictly related to Win32 APIs calls. When a function in the DLL calls another function (*) in the same DLL the caller must balance the stack, differently the application program soon or later will crash. Is it true ? Thanks. Alvise. (*) the latter function being exported in the DEF file.
Well, first off, what do you mean by 'balancing the stack'? Here's what I mean, and it's also what STDCALL means: When the caller calls a procedure: (invoke, call, whatever) Foo, param1, param2, ... paramN It first pushed on paramN, then ParamN-1, until it gets to param1. Then control is passed to the procedure. The procedure expects the params to be in a certain order on the stack so it can find and use them. Finally, as the procedure is terminating, it pops all the params off the stack (really just adjusts the value of the stack pointer), and returns control back to the caller. That's STDCALL in a nutshell. Caller sets up the stack, callee cleans it up. Assuming one procedure is called many times, it makes for smaller code. When you start an asm file, you typically have some compiler directives like so:
By telling it to use the stdcall model you enable automatic cleanup of a proc. In MASM, PROC and RET are macros used to manage the stack for you. Try running a listing of your compiled code and you will see the instructions MASM generates for you just for the stack.
.386 .model flat,stdcall option casemap:none
Thanks for your answer. Forget for a moment about STDCALL and INVOKE. Suppose I wrote a function in a DLL and exported it in the DEF file. Suppose another function in the same DLL calls the former function this way: 1) push parameters onto the stack; 2) CALL (not INVOKE) the function What happens ? My experience is that if the caller does not add to ESP the appropriate number of DWORDS (immediately after CALL) the application program soon or later crashes. Am I right ? Regards from Alvise. P.S.: I also noticed that if my application program does not add to ESP the proper number of DWORDS after CALLing functions in WNASPI32.DLL the application program crashes. Would this mean that WNASPI32.DLL did'nt declare STDCALL ? Where can I find info about PROC, RET and ENDP macros ?
alvise, STDCALL is nothing more than a calling convention, most API calls are done in STDCALL but wsprintfA for example is done in C calling convention. As long as you write a dll with a calling convention for each function in mind, you can use whatever you like based within the PROC / ENDP system. You can of course do the stack manually if you want, the conventions for STDCALL and C call are published, but for example if you wished to pass parameters in registers or pass them in GLOBAL values or pass a structure in a register, you can code these types of parameter passing if you need to. Normal stuff applies here, make sure the stack is in the right place after the procedure you write has finished. Regards, firstname.lastname@example.org
You wrote: >make sure the stack is in the right place >after the procedure you write has finished This is a clever idea, I'll check the stack to dissolve my last doubts. Many thanks and regards from Alvise.