如何将LPCWSTR与Go和Swig一起使用?
我正在尝试使用C库和Go使用Swig。这是一段简化的代码,我知道我可以使用cgo,但我需要使用一个带有LPCWSTR参数和Swig的函数 我在上面看到,如何将LPCWSTR与Go和Swig一起使用?,go,swig,Go,Swig,我正在尝试使用C库和Go使用Swig。这是一段简化的代码,我知道我可以使用cgo,但我需要使用一个带有LPCWSTR参数和Swig的函数 我在上面看到,LPCWSTR相当于*uint16,所以syscall.UTF16PtrFromString()似乎是我需要的,但我在运行代码时遇到了一个异常 我想知道我是否应该使用SwigcptrLPCWSTR libtest.c #include <windows.h> #include <stdio.h> #include "li
LPCWSTR
相当于*uint16
,所以syscall.UTF16PtrFromString()
似乎是我需要的,但我在运行代码时遇到了一个异常
我想知道我是否应该使用SwigcptrLPCWSTR
libtest.c
#include <windows.h>
#include <stdio.h>
#include "libtest.h"
__stdcall void hello(const LPCWSTR s)
{
printf("hello: %ls\n", s);
}
main.swig
%module main
%{
#include "libtest.h"
%}
%include "windows.i"
%include "libtest.h"
梅因,加油
package main
import (
"syscall"
"unsafe"
)
func main() {
p, err := syscall.UTF16PtrFromString("test")
if err != nil {
panic(err)
}
Hello(SwigcptrLPCWSTR(unsafe.Pointer(p)))
}
痕迹
我怀疑您看到的问题是因为您传递到SWIG的是一个双指针而不是一个指针,即
wchar\u t**
而不是wchar\u t*
我认为这是因为调用UTF16PtrFromString
获取UTF16字符串的地址,然后调用unsafe.Pointer(p)
,我想再次调用它获取输入的地址
从go源代码:
func UTF16PtrFromString(s string) (*uint16) {
a := UTF16FromString(s)
return &a[0]
}
因此,我认为如果你改用:
func main() {
p, err := syscall.UTF16FromString("test") // Note the subtle change here
if err != nil {
panic(err)
}
Hello(SwigcptrLPCWSTR(unsafe.Pointer(p)))
}
它应该按预期工作。您的意思是
UTF16FromString
?您在其中留下了一个P
。如果我使用Hello(SwigcptrLPCWSTR(unsafe.Pointer(&P)),它会起作用。
Exception 0xc0000005 0x0 0xffffffffffffffff 0x7ffcc761f2e1
PC=0x7ffcc761f2e1
signal arrived during external code execution
main._Cfunc__wrap_hello_main_90cec49f7d68ac58(0xc082002110)
_/D_/lpcwstr/go/_obj/_cgo_gotypes.go:53 +0x38
main.Hello(0x500028, 0xc082002120)
_/D_/lpcwstr/go/_obj/main.go:63 +0x3c
main.main()
D:/lpcwstr/go/main.go:9 +0x96
goroutine 17 [syscall, locked to thread]:
runtime.goexit()
c:/go/src/runtime/asm_amd64.s:1696 +0x1
rax 0x6c007400690074
rbx 0x6c007400690074
rcx 0x7ffffffe
rdi 0x24fd73
rsi 0x7
rbp 0x24fb00
rsp 0x24fa00
r8 0xffffffffffffffff
r9 0x7ffcc75d0000
r10 0x0
r11 0x200
r12 0xffffffff
r13 0x24fd60
r14 0x10
r15 0x6264403a
rip 0x7ffcc761f2e1
rflags 0x10202
cs 0x33
fs 0x53
gs 0x2b
func UTF16PtrFromString(s string) (*uint16) {
a := UTF16FromString(s)
return &a[0]
}
func main() {
p, err := syscall.UTF16FromString("test") // Note the subtle change here
if err != nil {
panic(err)
}
Hello(SwigcptrLPCWSTR(unsafe.Pointer(p)))
}