Calling the operating system

Import

Import makes functions from a system library callable like ordinary procedures:

Import "kernel32.lib"
  GetTickCount64.q()
  Beep(frequency.l, duration.l)
EndImport

OpenConsole()
If GetTickCount64() > 0
  PrintN("the system is running")
EndIf

Output:

the system is running
  • The string names the library. "kernel32.lib" and "kernel32" both mean kernel32.dll.
  • Each line declares one function: its name, an optional return type tag and its parameters. Without a tag the function returns int.
  • The function name is used exactly as written, case included, when the program starts and Windows looks it up.
  • As "RealName" lets the program use a different name from the library's:
Import "user32.lib"
  ShowMessage(window, text, caption, flags.l) As "MessageBoxW"
EndImport

Arguments are passed using the platform's calling convention (Microsoft x64 on Windows). Integers, pointers and floats go where the operating system expects them, including the fifth and later arguments.

Return types

Many Windows functions return a 32-bit BOOL or int. Declare those with .l, so the upper half of the 64-bit register isn't read:

Import "kernel32.lib"
  GetConsoleMode.l(handle, *mode)
EndImport

Passing strings

Windows functions ending in W take UTF-16 text, which is what x strings are. Pass @text$ to give a function the characters:

Import "kernel32.lib"
  SetConsoleTitleW(*title)
EndImport

title$ = "My program"
SetConsoleTitleW(@title$)

The same function imported twice

A function can be imported in more than one place, for example by your program and by a library. Both refer to the same function.

Not yet available 🔜

  • ImportC and extern blocks.
  • PureBasic's Name_() shortcut for Windows API functions.
  • Importing variables.
  • Linking static .lib files: stage 0 only uses functions from DLLs.