Is it posible to make c-shared library whise callback functions using Go?

30 Views Asked by At

this is app that loads the dll library (loader.exe) :

package main

import (
    "fmt"
    "log"
    "syscall"
)

/*
void HelloTo(char* name);

*/
import "C"

//export HelloTo
func HelloTo(name *C.char) {
    fmt.Printf("Hello To : %s !!!", C.GoString(name))
}

func main() {
    dll, err := syscall.LoadDLL("test.dll")
    if err != nil {
        log.Fatalln(err)
    }
    proc, err := dll.FindProc("CALL_BACK")
    if err != nil {
        log.Fatalln(err)
    }
    _, _, _ = proc.Call(uintptr(C.HelloTo))
}

this is the dll library (test.dll) :

package main

import "C"

func main() {}

//export CALL_BACK
func CALL_BACK(f func(*C.char)) {
    str := C.CString("FDUTCH")
    f(str)
}

I tried check if it is nil :

//export CALL_BACK
func CALL_BACK(f func(*C.char)) {
    fmt.Println(f == nil)
}

but it's not nil

I expect it to print "Hello To : FDUTCH !!!"

but program just crashes whise error "unexpected fault address 0xffffffffffffffff ,fatal error: fault"

0

There are 0 best solutions below