example.odin 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package example
  2. import "core:dynlib"
  3. import "core:fmt"
  4. Symbols :: struct {
  5. // `foo_` is prefixed, so we look for the symbol `foo_add`.
  6. add: proc "c" (int, int) -> int,
  7. // We use the tag here to override the symbol to look for, namely `bar_sub`.
  8. sub: proc "c" (int, int) -> int `dynlib:"bar_sub"`,
  9. // Exported global (if exporting an i32, the type must be ^i32 because the symbol is a pointer to the export.)
  10. // If it's not a pointer or procedure type, we'll skip the struct field.
  11. hellope: ^i32,
  12. // Handle to free library.
  13. // We can have more than one of these so we can match symbols for more than one DLL with one struct.
  14. _my_lib_handle: dynlib.Library,
  15. }
  16. main :: proc() {
  17. sym: Symbols
  18. LIB_PATH :: "lib." + dynlib.LIBRARY_FILE_EXTENSION
  19. // Load symbols from `lib.dll` into Symbols struct.
  20. // Each struct field is prefixed with `foo_` before lookup in the DLL's symbol table.
  21. // The library's Handle (to unload) will be stored in `sym._my_lib_handle`. This way you can load multiple DLLs in one struct.
  22. count, ok := dynlib.initialize_symbols(&sym, LIB_PATH, "foo_", "_my_lib_handle")
  23. defer dynlib.unload_library(sym._my_lib_handle)
  24. fmt.printf("(Initial DLL Load) ok: %v. %v symbols loaded from " + LIB_PATH + " (%p).\n", ok, count, sym._my_lib_handle)
  25. if count > 0 {
  26. fmt.println("42 + 42 =", sym.add(42, 42))
  27. fmt.println("84 - 13 =", sym.sub(84, 13))
  28. fmt.println("hellope =", sym.hellope^)
  29. }
  30. count, ok = dynlib.initialize_symbols(&sym, LIB_PATH, "foo_", "_my_lib_handle")
  31. fmt.printf("(DLL Reload) ok: %v. %v symbols loaded from " + LIB_PATH + " (%p).\n", ok, count, sym._my_lib_handle)
  32. if count > 0 {
  33. fmt.println("42 + 42 =", sym.add(42, 42))
  34. fmt.println("84 - 13 =", sym.sub(84, 13))
  35. fmt.println("hellope =", sym.hellope^)
  36. }
  37. }