aboutsummaryrefslogtreecommitdiff
path: root/core/dynlib/lib_windows.odin
blob: 95372dac6e1017c002949ea51162ebc1387ba9e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#+build windows
#+private
package dynlib

import "base:runtime"

import win32 "core:sys/windows"
import "core:strings"
import "core:reflect"

_LIBRARY_FILE_EXTENSION :: "dll"

_load_library :: proc(path: string, global_symbols: bool, allocator: runtime.Allocator) -> (Library, bool) {
	// NOTE(bill): 'global_symbols' is here only for consistency with POSIX which has RTLD_GLOBAL
	wide_path := win32.utf8_to_wstring(path, allocator)
	defer free(rawptr(wide_path), allocator)
	handle := cast(Library)win32.LoadLibraryW(wide_path)
	return handle, handle != nil
}

_unload_library :: proc(library: Library) -> bool {
	ok := win32.FreeLibrary(cast(win32.HMODULE)library)
	return bool(ok)
}

_symbol_address :: proc(library: Library, symbol: string, allocator: runtime.Allocator) -> (ptr: rawptr, found: bool) {
	c_str := strings.clone_to_cstring(symbol, allocator)
	defer delete(c_str, allocator)
	ptr = win32.GetProcAddress(cast(win32.HMODULE)library, c_str)
	found = ptr != nil
	return
}

_last_error :: proc() -> string {
	err := win32.System_Error(win32.GetLastError())
	err_msg := reflect.enum_string(err)
	return "unknown" if err_msg == "" else err_msg
}