Calling conventions
When declaring a procedure or function, you can use one of the following indicators to indicate the calling convention: register, pascal, cdecl, stdcall, and safecall. for example,
function MyFunction(X, Y: Real): Real; cdecl;
...
Calling conventions determine the order in which parameters are passed to routines, and they also affect the use of registers when deleting parameters from the stack, passing parameters, and error and exception handling. The default calling convention is register.
• register and pascal calls pass parameters from left to right, that is, the leftmost parameters are calculated and passed at the earliest, and the rightmost parameters are finally calculated and passed; cdecl, stdcall and safecall calls pass parameters from right to left;
• In addition to cdecl calls, procedures and functions remove parameters from the stack before returning, while cdecl uses, when the call returns, the caller removes parameters from the stack;
• register calls can pass parameters using up to 3 CPU registers, while other calls all use the stack to pass parameters;
• The safecall call implements the exception "firewall", which implements inter-process COM error notification under Windows.
The following table summarizes the calling conventions:
-------------------------------------------------- ------------------------------------------------
-Indicator words---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ? |
-register--------------- Left-to-right ---------Routine -----------------Yes ---------------------||
-pascal --------------- Left-to-right ---------Routine -----------------No -------------------------|
-cdecl----------------- Right-to-left ---------Caller ---------------- ---No----------------------------|
-stdcall ----------------Right-to-left ----------Routine ---------------- ---No-------------------------|
-safecall--------------- Right-to-left ----------Routine ----------------- ---No----------------------|
-------------------------------------------------- -----------------------------------------------
The default register call is most efficient because it usually avoids creating a stack frame (a register must be used when accessing published properties); when calling functions from shared libraries written in C/C++, cdecl is Useful; generally, when calling external code, stdcall and safecall are recommended. In Windows, the system API uses stdcall and safecall, while other operating systems usually use cdecl (note that stdcall is more efficient than cdecl).
Methods that declare dual interfaces must use safecall; pascal calls are preserved for backward compatibility. For more information about the calling convention, refer to PRogram control.
Indicators near, far, and export are used in 16-bit Windows programming, and they have no effect on 32-bit programs, and they are retained for backward compatibility.