Import : EndImport


Syntax
Import "Filename"
  FunctionName.<type>(<parameter>, [, <parameter> [= DefaultValue]...]) [As "SymbolName"]
  ...
  VariableName.<type> [As "SymbolName"]
EndImport
Description
For advanced programmers. Import : EndImport allows to easy declare external functions and variables from a library (.lib) or an object (.obj) file.

Once declared, the imported functions are directly available for use in the program, like any other commands. The compiler doesn't check if the functions really exists in the imported file, so if an error occurs, it will be reported by the linker.

This feature can replace the OpenLibrary()/CallFunction() sequence as it has some advantages: type checking is done, number of parameters is validated. Unlike CallFunction(), it can deal with double, float and quad without any problem.

The last parameters can have a default value (need to be a constant expression), so if these parameters are omitted when the function is called, the default value will be used.

By default the imported function symbol is 'decorated' in the following way: _FunctionName@callsize. That should work for most of the functions which use the standard call convention (stdcall). If the library is a C one, and the function are not stdcall, the ImportC variant should be used instead. In this case, the default function symbol is decorated like: _FunctionName.

The pseudotypes can be used for the parameters, but not for the returned value.

Remarks

On x64, there is only one calling convention, so ImportC will behave the sames as Import.

Example

  CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
    CompilerError "This sample only works on Windows"
  CompilerEndIf

  Import "User32.lib"
   
    ; No need to use 'As' as PureBasic decorates the function correctly
    ; We also define the 'Flags' as optional, with a default value of 0 (when omitted)
    ;
    MessageBoxW(Window.i, Body$, Title$, Flags.i = 0)
    
  EndImport
  
  MessageBoxW(0, "Hello", "World") ; We don't specify the flags

Example: With pseudotypes

  CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
    CompilerError "This sample only works on Windows"
  CompilerEndIf

  Import "User32.lib"
   
    ; We use the 'p-unicode' pseudotype for the string parameters, as
    ; MessageBoxW() is an unicode only function. The compiler will 
    ; automatically converts the strings to unicode when needed.
    ;
    MessageBoxA(Window.i, Body.p-ascii, Title.p-ascii, Flags.i = 0)
                                                  
  EndImport
  
  MessageBoxA(0, "Hello", "World")