Name

StdCall Directive

Syntax

Subroutine declaration; stdcall;

Description

The stdcall directive uses the Windows standard calling convention: arguments are pushed onto the stack, starting with the rightmost argument. The subroutine is responsible for popping the arguments from the stack.

Functions return ordinal values, pointers, and small records or sets in EAX and floating-point values on the FPU stack. Strings, dynamic arrays, Variants, and large records and sets are passed as a hidden var parameter. This hidden parameter is the last parameter, so it is pushed first onto the stack. If the subroutine is a method, Self is pushed just before the function’s var result (if one is needed).

Tips and Tricks

  • Use stdcall for subroutines you export from a DLL that will be called by other languages.

  • Use stdcall for Windows callback functions.

Example

// Make a list of all the top-level windows.
function Callback(Handle: HWND; List: TStrings): LongBool; stdcall;
var
  Caption: array[0..256] of Char;
  Len: Integer;
  Text: string;
begin
  Len := GetWindowText(Handle, Caption, SizeOf(Caption)-1);
  SetString(Text, Caption, Len);
  List.Add(Text);
  Result := True;
end;
...
EnumWindows(@Callback, LParam(ListBox1.Items));

See Also

CDecl Directive, Function Keyword, Pascal Directive, Procedure Keyword, Register Directive, SafeCall Directive

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.