go dll char[]

 

 

这个会返回4,是错的:

tcallback((unsigned char*)pFrameRGB->data

 

func callDll() {
    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 := (*uint8)(unsafe.Pointer(&r))

        //fmt.Printf("value a is %s\n",(*uint8)(unsafe.Pointer(&r)))
       // 定义一个[]byte切片,用来存储C返回的字符串
       data := make([]uint8, 0)
       // 遍历C返回的char指针,直到 '\0' 为止
       for *p != 0 {
          data = append(data, *p)  // 将得到的byte追加到末尾
          r += unsafe.Sizeof(byte(0))  // 移动指针w,指向下一个char
          p = (*uint8)(unsafe.Pointer(r))  // 获取指针的值,此时指针已经指向下一个char
       }
       //name := string(data)
       fmt.Printf("from callback1: %d %d\n", size, len(data))//,name)
       return 0
    })

你可能感兴趣的:(go)