example.odin 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. // Load symbols from `lib.dll` into Symbols struct.
  19. // Each struct field is prefixed with `foo_` before lookup in the DLL's symbol table.
  20. // The library's Handle (to unload) will be stored in `sym._my_lib_handle`. This way you can load multiple DLLs in one struct.
  21. count, ok := dynlib.initialize_symbols(&sym, "lib.dll", "foo_", "_my_lib_handle")
  22. defer dynlib.unload_library(sym._my_lib_handle)
  23. fmt.printf("(Initial DLL Load) ok: %v. %v symbols loaded from lib.dll (%p).\n", ok, count, sym._my_lib_handle)
  24. if count > 0 {
  25. fmt.println("42 + 42 =", sym.add(42, 42))
  26. fmt.println("84 - 13 =", sym.sub(84, 13))
  27. fmt.println("hellope =", sym.hellope^)
  28. }
  29. count, ok = dynlib.initialize_symbols(&sym, "lib.dll", "foo_", "_my_lib_handle")
  30. fmt.printf("(DLL Reload) ok: %v. %v symbols loaded from lib.dll (%p).\n", ok, count, sym._my_lib_handle)
  31. if count > 0 {
  32. fmt.println("42 + 42 =", sym.add(42, 42))
  33. fmt.println("84 - 13 =", sym.sub(84, 13))
  34. fmt.println("hellope =", sym.hellope^)
  35. }
  36. }