Skip to content

Instantly share code, notes, and snippets.

@stisa
Last active September 22, 2016 19:26
Show Gist options
  • Save stisa/8c29b228dc9bb3240954711053976d6f to your computer and use it in GitHub Desktop.
Save stisa/8c29b228dc9bb3240954711053976d6f to your computer and use it in GitHub Desktop.
## lib.nim #########
proc test1(): string {.exportc, dynlib .}=
result = "test"
## user.nim ########
import dynlib, osproc
# compile libs so I don't have to do it by hand
let l1 = execCmd("nim c --app:lib lib.nim")
# load the lib
let lib = loadLib("lib.dll")
# get a pointer to the proc in lib.nim
let testptr = lib.checkedsymAddr("test1")
#error: request for member 'ClEnv' in something not a structure or union
var testderef = cast[proc():string](testptr)
echo testderef() #
# SIGSEGV
var testderef2 = cast[ptr proc():string](testptr)[]
echo testderef2()
@stisa
Copy link
Author

stisa commented Sep 22, 2016

Changing var testderef = cast[proc():string](testptr) to
var testderef = cast[proc():string {.cdecl.}](testptr) works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment