Import - Loading External DLL Functions

The Import keyword allows you to load function declarations from external DLL libraries. This enables calling functions from pre-compiled DLLs like the BambooBasic Runtime library.



Basic Syntax

Use Import "filename.decls" to load function declarations from a .decls file:

Import "BBRuntimeLinux.decls"

The .decls file must be located in the userlibs/ directory.



How It Works
  1. Create a .decls file that specifies:
  2. Place the .decls file in the userlibs/ directory
  3. Place the DLL in the userlibs/ directory
  4. Use Import "filename.decls" in your BambooBasic code
  5. Call the imported functions normally


.decls File Format

A .decls file specifies the DLL and declares its functions:

; MyLibrary.decls

; Specify the shared library to load
.lib "MyLibrary.so"

; Declare functions with type suffixes
; % = Int, ! = Double, $ = String
MyFunction%(param1%, param2$):"MyFunction"
CalculateArea!(width!, height!):"CalculateArea"
GetName$():"GetName"

Type Suffixes:



Example: BambooBasic Runtime

BBRuntimeLinux.decls (excerpt):

; BBRuntimeLinux Declarations for BambooBasic

.lib "BBRuntimeLinux.so"

; ====================
; Graphics Initialization
; ====================
BBR_Graphics%(resWidth%,resHeight%,graphicsMode%):"BBR_Graphics"
BBR_Cls():"BBR_Cls"
BBR_Flip():"BBR_Flip"
BBR_SetClsColor(r%,g%,b%):"BBR_SetClsColor"

; ====================
; Mouse Input
; ====================
BBR_GetMouseX%():"BBR_GetMouseX"
BBR_GetMouseY%():"BBR_GetMouseY"
BBR_IsMouseHit%(vmButtonCode%):"BBR_IsMouseHit"
BBR_IsMouseDown%(vmButtonCode%):"BBR_IsMouseDown"

; ====================
; Drawing
; ====================
BBR_DrawRect(x%, y%, width%, height%, r%, g%, b%):"BBR_DrawRect"
BBR_DrawCircle(x%, y%, radius%, r%, g%, b%):"BBR_DrawCircle"

Using the imported functions:

Import "BBRuntimeLinux.decls"

Function Main()
    ; Initialize graphics window
    BBR_Graphics(800, 600, 0)
    BBR_SetClsColor(0, 0, 128)

    ; Game loop
    While True
        BBR_Cls()

        ; Draw shapes
        BBR_DrawRect(100, 100, 50, 50, 255, 0, 0)
        BBR_DrawCircle(400, 300, 75, 0, 255, 0)

        ; Get mouse position
        Local mx:Int = BBR_GetMouseX()
        Local my:Int = BBR_GetMouseY()

        ; Check mouse click
        If BBR_IsMouseHit(1)
            Print "Mouse clicked at: " & ToString(mx) & ", " & ToString(my)
        EndIf

        BBR_Flip()
    Wend

    Return False
EndFunction


Creating Your Own .decls File

To use your own DLL:

1. Create the .decls file (MyMath.decls):

; MyMath.decls - Custom math library

.lib "MyMathLib.so"

; Function declarations
Add%(a%, b%):"Add"
Multiply%(a%, b%):"Multiply"
SquareRoot!(value!):"SquareRoot"
FormatNumber$(value!, decimals%):"FormatNumber"

2. Place files in userlibs/:

userlibs/
    MyMath.decls
    MyMathLib.so

3. Import and use:

Import "MyMath.decls"

Function Main()
    Local sum:Int = Add(10, 20)
    Print "Sum: " & ToString(sum)

    Local product:Int = Multiply(5, 7)
    Print "Product: " & ToString(product)

    Local root:Double = SquareRoot(16.0)
    Print "Square root of 16: " & ToString(root)

    Local formatted:String = FormatNumber(3.14159, 2)
    Print "Formatted: " & formatted

    Return False
EndFunction


Function Name Mapping

The syntax BambooName:"ExternalName" maps a BambooBasic function name to the actual DLL function name:

; Map BBR_Graphics to the DLL function named BBR_Graphics
BBR_Graphics%(w%,h%,mode%):"BBR_Graphics"

; Map Calculate to the DLL function named PerformCalculation
Calculate%(x%, y%):"PerformCalculation"

; Map GetInfo to the DLL function named DLL_GetSystemInfo
GetInfo$():"DLL_GetSystemInfo"

This allows you to give imported functions more convenient names.



Parameter and Return Types
Suffix BambooBasic Type C/C++ Type Example
% Int int GetValue%()
! Double double GetPI!()
$ String const char* GetName$()
(none) Void void Initialize()


Multiple Imports

You can import multiple .decls files:

Import "BBRuntimeLinux.decls"
Import "MyMath.decls"
Import "MyPhysics.decls"
Import "MyAudio.decls"

Function Main()
    ; Use functions from all imported libraries
    BBR_Graphics(800, 600, 0)

    Local result:Int = Add(10, 20)
    ApplyGravity(player)
    PlaySound("jump.wav")

    Return False
EndFunction


Import vs Include vs Linker

DLL Deployment

When you build your BambooBasic application:

  1. Place your DLL in the userlibs/ directory
  2. When you compile, BambooBasic automatically copies the DLL to the output directory (where your .bam file is located)
  3. The compiled .exe will find the DLL in the same directory

Best Practice: Always place DLLs in the userlibs/ directory. BambooBasic handles copying them to the correct location automatically during compilation.



Common Errors

Error: "Could not load DLL"

Error: "Function not found"

Error: ".decls file not found"



Key Points

See Also

Examples

See the BambooRuntime Examples for demonstrations of using imported DLL functions.


BambooBasic © 2026 Michael Denathorn