c++部分:
typedef int(*ByteFunc)(unsigned char*, int);
MYLIBDLL int byte_url(char* url, ByteFunc tcallback(unsigned char*, int size));
AVPacket packet;
tcallback((unsigned char*)&packet.data[0],packet.size);
这个也可以:
p := (*byte)(unsafe.Pointer(r))
// 定义一个[]byte切片,用来存储C返回的字符串
data := make([]byte,size)
//for *p != 0 {
for a := 0; a < size; a++ {
data[a] = *p
r+=1
p = (*byte)(unsafe.Pointer(r))
}
dll := syscall.MustLoadDLL("hik_client.dll")
callback := syscall.NewCallback(func(r uintptr, size int) (ret uintptr) {
//var data = make([]byte, size)
//
//p := (unsafe.Pointer(r))
//q := (*[4]byte)(p)
//copy(data[0:], (*q)[0:])
p := (*byte)(unsafe.Pointer(r))
// 定义一个[]byte切片,用来存储C返回的字符串
data := make([]byte, 0)
var index int
index=0
for index<size {
data = append(data, *p) // 将得到的byte追加到末尾
r += unsafe.Sizeof(byte(0)) // 移动指针w,指向下一个char
p = (*byte)(unsafe.Pointer(r)) // 获取指针的值,此时指针已经指向下一个char
index+=1
}
//fmt.Printf("start send %d\n",size,len(data))
err := conn.ws.WriteMessage(2, data)
if err != nil {
fmt.Printf("conn.WriteMessage ERROR!!!\n")
}