lib_windows.odin 1.1 KB

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