lib_windows.odin 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #+build windows
  2. #+private
  3. package dynlib
  4. import "base:runtime"
  5. import win32 "core:sys/windows"
  6. import "core:strings"
  7. import "core:reflect"
  8. _LIBRARY_FILE_EXTENSION :: "dll"
  9. _load_library :: proc(path: string, global_symbols: bool, allocator: runtime.Allocator) -> (Library, bool) {
  10. // NOTE(bill): 'global_symbols' is here only for consistency with POSIX which has RTLD_GLOBAL
  11. wide_path := win32.utf8_to_wstring(path, allocator)
  12. defer free(wide_path, allocator)
  13. handle := cast(Library)win32.LoadLibraryW(wide_path)
  14. return handle, handle != nil
  15. }
  16. _unload_library :: proc(library: Library) -> bool {
  17. ok := win32.FreeLibrary(cast(win32.HMODULE)library)
  18. return bool(ok)
  19. }
  20. _symbol_address :: proc(library: Library, symbol: string, allocator: runtime.Allocator) -> (ptr: rawptr, found: bool) {
  21. c_str := strings.clone_to_cstring(symbol, allocator)
  22. defer delete(c_str, allocator)
  23. ptr = win32.GetProcAddress(cast(win32.HMODULE)library, c_str)
  24. found = ptr != nil
  25. return
  26. }
  27. _last_error :: proc() -> string {
  28. err := win32.System_Error(win32.GetLastError())
  29. err_msg := reflect.enum_string(err)
  30. return "unknown" if err_msg == "" else err_msg
  31. }