DECLARE LIBRARY

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search

The DECLARE LIBRARY declaration allows for the utilization of external library SUB and FUNCTION procedures.


Syntax

DECLARE [DYNAMIC|CUSTOMTYPE|STATIC] LIBRARY ["Library_name"][, "Library_name2"][, ...]
{SUB|FUNCTION} Procedure_name [ALIAS Library_procedure] (BYVAL Parameter[{suffix| AS type[, Parameter2...][, ...]])
.
. ' Other SUBs or FUNCTIONs as required
.
END DECLARE


Description

  • The declaration can be used with:
    • C/C++ sub-procedures files (.h and .hpp)
    • Dynamically linked libraries and shared object files (.dll, .so and .dylib)
    • Static libraries (.a)
    • Object files (.o)
  • Library file names can be listed to combine more than one supported file name or path into one DECLARE LIBRARY block (except for dynamically linked libraries and shared object files). Do not include any file extension (.h, .dll, .a etc.).
  • The dynamically linked library or shared object file can be in a relative/absolute path specified along with the library name, in the operating system's directory (e.g., C:\Windows\System32), or in the QB64 directory (alongside your program's executable).
  • Specify "" as the library file name to inform QB64 that the function exists in a library that is already linked and that it must define the C function before calling it, as well as allowing QB64 code to call it.
  • It's a good idea to try declaring Operating System API libraries without a library file name first, as most common Operating System headers are already included in QB64 source.
  • When using a procedure from a dynamically linked library or shared object (.dll, .so and .dylib files) use DECLARE DYNAMIC LIBRARY.
  • When using a procedure from a static library (.a files) use DECLARE STATIC LIBRARY.
  • Procedure_name can be any desired procedure name designated by using ALIAS with the Library_procedure name following. Enclose Library_procedure in "" when using a C++ function with the scope operator (e.g., "std::clamp").
  • Parameters used by the library procedure can be passed by value (BYVAL) when needed, except for STRING values.
  • QB64 STRINGs are passed to external libraries as pointers to first character. Many external library procedures expect strings to be NULL terminated. Terminate STRINGs using CHR$(0) wherever it is needed.
  • Declarations can be made inside of SUB or FUNCTION procedures. Declarations do not need to be at program start.
  • C/C++ procedures can use a header file name and the file code must be included with program code.
  • C/C++ header files cannot be used with DECLARE DYNAMIC LIBRARY. Existence of any .h or .hpp file of the same name as the dynamically linked library or shared object file will cause DECLARE DYNAMIC LIBRARY to fail.
  • The _OFFSET in memory can be used in CUSTOMTYPE, STATIC and DYNAMIC LIBRARY declarations.
  • DECLARE DYNAMIC LIBRARY let's you specify any SUB/FUNCTION calling format you wish, but if the size of the parameters does not match, the size expected within the library, your code will probably cause a GPF (General Protection Fault). It is important to understand that pointers (if required) will be 32-bits in size (the equivalent of a LONG) when creating a 32-bit program (even under 64-bit Windows).
  • STATIC is the same as DYNAMIC except that it prioritizes linking to static libraries (.a and .o files) over dynamically linked libraries and shared object files, if both exist.
  • CUSTOMTYPE is already implied when using DECLARE DYNAMIC LIBRARY. This type of library just allows the same flexibility to apply when referencing STATIC libraries that are used to refer to dynamic libraries.
  • SUB procedures using DECLARE CUSTOMTYPE LIBRARY API procedures may error. Try DYNAMIC with the dynamically linked library or shared object file name.
  • It is up to the user to document and determine the suitability of all libraries and procedures they choose to use. QB64 cannot guarantee that any procedure will work and cannot guarantee any troubleshooting help.
  • QB64 version 1.000 and up produce standalone executables. External dynamically linked libraries or shared object files must be distributed with your program.
  • What libraries are or aren't automatically used in the linking process is not formally defined, nor is it guaranteed to stay that way in future versions of QB64.


Parameters

  • The Library_name is needed if a library is not already loaded by QB64. Do not include the file extension. Begin the Library_name with ./ or .\ to make it relative to the path where your source file is saved, so you can keep all your project files together.
  • Procedure_name is any program procedure name you want to designate by using ALIAS with the Library_procedure name.
  • Library procedure is the actual procedure name used inside of the library or header file.


Availability

  • Available for Linux and macOS since QB64 v0.940


Examples

Example 1
Using GLUT library procedure as a program SUB procedure to set the mouse pointer style.
CONST CURSOR_NORMAL = 1
CONST CURSOR_HAND = 2
CONST CURSOR_HELP = 4
CONST CURSOR_CYCLE = 7
CONST CURSOR_TEXT = 8
CONST CURSOR_CROSSHAIR = 3
CONST CURSOR_UP_DOWN = 10
CONST CURSOR_LEFT_RIGHT = 11
CONST CURSOR_LEFT_RIGHT_CORNER = 16
CONST CURSOR_RIGHT_LEFT_CORNER = 17
CONST CURSOR_MOVE = 5
CONST CURSOR_NONE = 23

DECLARE LIBRARY
    SUB glutSetCursor (BYVAL style&)
END DECLARE

glutSetCursor CURSOR_CROSSHAIR


Example 2
Using ALIAS to create a program SUB or FUNCTION.
DECLARE LIBRARY
    FUNCTION GetMilliseconds~&& ALIAS GetTicks
END DECLARE

DO
    LOCATE , 1: PRINT "Seconds since program start:"; GetMilliseconds \ 1000;
LOOP UNTIL _KEYHIT = 27
Explanation: When a library procedure is used to represent another procedure name use ALIAS instead. Saves creating a SUB!


Example 3
Don't know if a C function is defined by C++ or QB64? Try using empty quotes.
DECLARE LIBRARY ""
    FUNCTION addone& (BYVAL value&)
END DECLARE
Explanation: The C function 'addone' exists in a library QB64 already links to, but it hasn't been defined as a C function or a QB64 function. By using "" we are telling QB64 the function exists in a library which is already linked to and that it must define the C function before calling it, as well as allowing QB64 code to call it. Trying the above code without the "" will fail.


Example 4
This example plays MIDI files using the playmidi32.dll documented here: Liberty Basic University. Download the following DLL file to your main QB64 directory: PlayMidi32.dll
DECLARE DYNAMIC LIBRARY "playmidi32"
    FUNCTION PlayMIDI& (filename AS STRING)
END DECLARE
result = PlayMIDI(".\samples\qb64\original\ps2battl.mid" + CHR$(0))
PRINT result


Example 5
Using a CUSTOMTYPE LIBRARY to return the Unicode version of the current running program's name.
SCREEN 12

DECLARE CUSTOMTYPE LIBRARY 'Directory Information using KERNEL32 provided by Dav
    FUNCTION GetModuleFileNameA& (BYVAL hModule AS LONG, lpFileName AS STRING, BYVAL nSize AS LONG)
    FUNCTION GetModuleFileNameW& (BYVAL hModule AS LONG, lpFileName AS STRING, BYVAL nSize AS LONG)
END DECLARE

'=== SHOW CURRENT PROGRAM
FileName$ = SPACE$(512)

Result = GetModuleFileNameA(0, FileName$, LEN(FileName$))
IF Result THEN PRINT "CURRENT PROGRAM (ASCII): "; LEFT$(FileName$, Result)

'load a unicode font
f = _LOADFONT("cyberbit.ttf", 24, "UNICODE")
_FONT f
Result = GetModuleFileNameW(0, FileName$, LEN(FileName$) \ 2)
LOCATE 2, 1
PRINT QuickCP437toUTF32$("CURRENT PROGRAM (UTF): ") + QuickUTF16toUTF32$(LEFT$(FileName$, Result * 2))
_FONT 16 'restore CP437 font

FUNCTION QuickCP437toUTF32$ (a$)
    b$ = STRING$(LEN(a$) * 4, 0)
    FOR i = 1 TO LEN(a$)
        ASC(b$, i * 4 - 3) = ASC(a$, i)
    NEXT
    QuickCP437toUTF32$ = b$
END FUNCTION

FUNCTION QuickUTF16toUTF32$ (a$)
    b$ = STRING$(LEN(a$) * 2, 0)
    FOR i = 1 TO LEN(a$) \ 2
        ASC(b$, i * 4 - 3) = ASC(a$, i * 2 - 1)
        ASC(b$, i * 4 - 2) = ASC(a$, i * 2)
    NEXT
    QuickUTF16toUTF32$ = b$
END FUNCTION
Code by Galleon


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage