Hi,
I need to pass value of struct type, which has C strings and functions to C
function as void* argument. C layer would provide this struct type data
back to go function. Below is the sample program. It panics if the handle
used is returned from getH function. It works fine if the handle is global.
Can the handle be dynamically returned from getH and make it work. Please
correct if i missed something..
package main
/*
struct dt {
void *context;
};
typedef struct dt dt;
extern void MyGoPrint(void *context);
static inline void myprint(struct dt *a1) {
MyGoPrint(a1->context);
}
*/
import "C"
import (
"context"
"runtime/cgo"
"unsafe"
)
//export MyGoPrint
func MyGoPrint(context unsafe.Pointer) {
h := *(*cgo.Handle)(context)
val := h.Value().(accessTokenCB)
println(val.id)
h.Delete()
}
type At struct {
Tok string
}
type accessTokenCB struct {
ctx context.Context
callback func(context.Context, *At) error
id uint64
ctoken *C.char
cprivateKey *C.char
}
func getH() cgo.Handle {
cb := func(ctx context.Context, tok *At) error {
tok.Tok = "123"
return nil
}
val := accessTokenCB{callback: cb, ctx: context.Background(), id: 32,
ctoken: nil, cprivateKey: nil}
h := cgo.NewHandle(val)
return h
}
var h cgo.Handle
func main() {
var h cgo.Handle // commenting this line runs the program successfully else
it panics. (cgo argument has Go pointer to unpinned Go pointer)
h = getH()
var poolCt C.dt
poolCt.context = unsafe.Pointer(&h)
C.myprint(&poolCt)
// Output: 32
}
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/1b34c3ba-cf4b-4663-b509-5ef84e9600c1n%40googlegroups.com.