go 调用dll char*传输
【摘要】
参考:http://www.voidcn.com/article/p-ofnbgvoa-bpv.html
https://stackoverflow.com/questions/53721215/syscall-mustloaddll-mustfindproc-throws-the-specified-procedure-could-not-be-fo
c...
参考:http://www.voidcn.com/article/p-ofnbgvoa-bpv.html
c++:
MYLIBDLL char* testchar(int plus1);
MYLIBDLL void greet(char *name);
-
-
void greet(char *name)
-
{
-
printf("Hello, %s!\n", name);
-
}
-
char* testchar(int plus1) {
-
-
char* str = "hello world111111";
-
return str;
-
}
main.go代码:
-
package main
-
-
import (
-
"syscall"
-
"unsafe"
-
"fmt"
-
)
-
import "C"
-
-
func callDll() {
-
-
-
dll := syscall.MustLoadDLL("hik_client.dll")
-
-
procGreet := dll.MustFindProc("greet")
-
procGreet.Call(uintptr(unsafe.Pointer(syscall.StringBytePtr("Cynhard11"))))
-
-
procName := dll.MustFindProc("testchar")
-
r, _, _ := procName.Call(1)
-
// 获取C返回的指针。
-
// 注意C返回的r为char*,对应的Go类型为*byte
-
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)) // 移动指针,指向下一个char
-
p = (*byte)(unsafe.Pointer(r)) // 获取指针的值,此时指针已经指向下一个char
-
}
-
name := string(data) // 将data转换为字符串
-
-
fmt.Printf("Hello, %s!\n", name)
-
-
//var a = make([]byte, 256)
-
//
-
//
-
//dll32 := syscall.NewLazyDLL("hik_client.dll")
-
//fmt.Println("call dll:", dll32.Name)
-
//GetPeopleName := dll32.NewProc("testchar")
-
//
-
//ret,_,err := GetPeopleName.Call(uintptr(unsafe.Pointer(&a)),2)
-
//if err != nil {
-
// fmt.Println(ret,a)
-
// aaa := C.GoString(ret)
-
// //C.free(unsafe.Pointer(cs))
-
// //C.free(unsafe.Pointer(result))
-
// //fmt.Println(aaa,a)
-
//}
-
//result, a, _ := g.Call(4)
-
-
-
//fmt.Println("result is :", ret)
-
}
-
-
func main() {
-
fmt.Println("start111111111!")
-
callDll()
-
//fmt.Println(C.add(2, 1))
-
}
文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/88381387
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)