Skip to content

dl.loader #


dl.loader is an abstraction layer over dl that provides a more user-friendly API in the V way. It can be used to Dynamically Load a library during runtime in scenarios where the library to load does not have a determined path an can be located in different places.

It also provides a way to load a library from a specific path, or from a list of paths, or from a custom environment variable that contains a list of paths.


import dl.loader

// Load a library from a list of paths
const default_paths = [
    // 'C:\\Windows\\System32\\shell32.dll',

fn main() {
    mut dl_loader := loader.get_or_create_dynamic_lib_loader(
        key: 'LibExample'
        env_path: 'LIB_PATH'
        paths: default_paths

    defer {

    sym := dl_loader.get_sym('CommandLineToArgvW')!
    assert !isnil(sym)

Constants #

const dl_no_path_issue_code = 1
const dl_open_issue_code = 1
const dl_sym_issue_code = 2
const dl_close_issue_code = 3
const dl_register_issue_code = 4
const dl_no_path_issue_err = error_with_code(dl_no_path_issue_msg, dl_no_path_issue_code)
const dl_open_issue_err = error_with_code(dl_open_issue_msg, dl_open_issue_code)
const dl_sym_issue_err = error_with_code(dl_sym_issue_msg, dl_sym_issue_code)
const dl_close_issue_err = error_with_code(dl_close_issue_msg, dl_close_issue_code)
const dl_register_issue_err = error_with_code(dl_register_issue_msg, dl_register_issue_code)

fn get_or_create_dynamic_lib_loader #

fn get_or_create_dynamic_lib_loader(conf DynamicLibLoaderConfig) !&DynamicLibLoader

get_or_create_dynamic_lib_loader returns a DynamicLibLoader. If the DynamicLibLoader is not registered, it creates a new DynamicLibLoader.

fn registered_dl_loader_keys #

fn registered_dl_loader_keys() []string

registered_dl_loader_keys returns the keys of registered DynamicLibLoader.

struct DynamicLibLoader #

struct DynamicLibLoader {
	key   string
	flags int = dl.rtld_lazy
	paths []string
	handle  voidptr
	sym_map map[string]voidptr

DynamicLibLoader is a wrapper around dlopen, dlsym and dlclose.

fn (DynamicLibLoader) open #

fn (mut dl_loader DynamicLibLoader) open() !voidptr

load loads the dynamic library.

fn (DynamicLibLoader) close #

fn (mut dl_loader DynamicLibLoader) close() !

close closes the dynamic library.

fn (DynamicLibLoader) get_sym #

fn (mut dl_loader DynamicLibLoader) get_sym(name string) !voidptr

get_sym gets a symbol from the dynamic library.

fn (DynamicLibLoader) unregister #

fn (mut dl_loader DynamicLibLoader) unregister()

unregister unregisters the DynamicLibLoader.

struct DynamicLibLoaderConfig #

struct DynamicLibLoaderConfig {
	// flags is the flags for dlopen.
	flags int = dl.rtld_lazy
	// key is the key to register the DynamicLibLoader.
	key string
	// env_path is the environment variable name that contains the path to the dynamic library.
	env_path string
	// paths is the list of paths to the dynamic library.
	paths []string

DynamicLibLoaderConfig is a configuration for DynamicLibLoader.