lib_windows.odin 1.1 KB

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