runtime error: invalid memory address or nil pointer dereference
解决方法:
tcallback((unsigned char*)packet->data,packet->size);
但是只能收到第一个数据。
问题代码:
c++:
tcallback((unsigned char*)packet->data[0],packet->size);
go:
package main
import (
"syscall"
"unsafe"
"fmt"
)
import "C"
type Callback func(data []byte, size int,dtype int,width int,height int) int
func get_byte(data []byte, size int,dtype int,width int,height int) int{
fmt.Println(size,width)
return 0
}
func callDll() {
dll := syscall.MustLoadDLL("hik_client.dll")
callback := syscall.NewCallback(func(r uintptr, size int) (ret int) {
p := (*byte)(unsafe.Pointer(r))
// 定义一个[]byte切片,用来存储C返回的字符串
data := make([]byte, 0)
// 遍历C返回的char指针,直到 '\0' 为止
for *p != 0 {
data = append(data, *p) // 将得到的byte追加到末尾
r += unsafe.Sizeof(byte(0)) // 移动指针wwwwwwwwwwwwwwwwwwwwwwwwwwwww,指向下一个char
p = (*byte)(unsafe.Pointer(r)) // 获取指针的值,此时指针已经指向下一个char
}
fmt.Printf("from callback1: %d %b\n", size, cap(data))
return 0
})
var filename string
filename = "rtsp://admin:sbdwl123@192.168.25.45:554/h264/ch1/main/av_stream"
procGreet := dll.MustFindProc("byte_url")
procGreet.Call(uintptr(unsafe.Pointer(syscall.StringBytePtr(filename))), callback)