4

I got a callback from a dll, the function ptr type defined:

typedef int32_t (WINAPI *fn) ();

if I use cgo, we can do like this:

/*
#include <windows.h>
#include <stdint.h>

typedef int32_t (WINAPI *fn) ();

int32_t call_callback(fn f){
   f();
}
*/
import "C"
import (
    "unsafe"
)

func main() {

    var callBackPtr uintptr // from dll function

    C.call_callback((C.fn)(unsafe.Pointer(callBackPtr)))

}

BUT! I don't want to use cgo. How can I achieve in pure go?

Ken White
  • 123,280
  • 14
  • 225
  • 444
lysS
  • 77
  • 3
  • Don't think it will work. You can call DLL functions from go without cgo with simple datatype args by serializing them with `syscall` [article here](https://github.com/golang/go/wiki/WindowsDLLs); however, I don't think you can (or should) be able to use syscall pass a go func and as c func without cgo. The Go memory manager probably wouldn't tolerate this without cgo as well – Liam Kelly Sep 28 '22 at 15:03
  • [This issue](https://github.com/golang/go/issues/18296) opened in 2013 indicates "one of the points of Cgo is to make this work in the first place". The latest comments ([this one](https://github.com/golang/go/issues/18296#issuecomment-468477837) and [this one](https://github.com/golang/go/issues/18296#issuecomment-1221724761)) point to two repositories : a [POC](https://github.com/notti/nocgo) by author of first comment, and a [subproject of ebitengine](https://github.com/ebitengine/purego) – LeGEC Oct 11 '22 at 03:41
  • thk, i solve it. – lysS Nov 09 '22 at 09:46

1 Answers1

0

just

   syscall.SyscallN(callBackPtr, [callBack's arg])

....

lysS
  • 77
  • 3