如何在Go和Swig中使用LPCWSTR?

huangapple go评论94阅读模式
英文:

How to use LPCWSTR with Go and Swig?

问题

我正在尝试使用Swig在Go中使用C库。这是简化的代码,我知道我可以使用cgo,但我需要在Swig中使用一个带有LPCWSTR参数的函数。

我在https://github.com/AllenDang/w32/blob/c92a5d7c8fed59d96a94905c1a4070fdb79478c9/typedef.go上看到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);
}

libtest.h

#ifndef EXAMPLE_DLL_H
#define EXAMPLE_DLL_H

#include <windows.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifdef BUILDING_EXAMPLE_DLL
#define EXAMPLE_DLL __declspec(dllexport)
#else
#define EXAMPLE_DLL __declspec(dllimport)
#endif

void __stdcall EXAMPLE_DLL hello(const LPCWSTR s);

#ifdef __cplusplus
}
#endif

#endif

我使用以下命令构建lib和DLL:

gcc -c -DBUILDING_EXAMPLE_DLL libtest.c
gcc -shared -o libtest.dll libtest.o -Wl,--out-implib,libtest.a

main.swig

%module main

%{
#include "libtest.h"
%}

%include "windows.i"
%include "libtest.h"

main.go

package main

import (
    "syscall"
    "unsafe"
)

func main() {
    p, err := syscall.UTF16PtrFromString("test")
    if err != nil {
        panic(err)
    }
    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
英文:

I'm trying to use a C library with Go using Swig. This is simplified code, I know I can use cgo but I need to use a function with a LPCWSTR argument with Swig.

I saw on https://github.com/AllenDang/w32/blob/c92a5d7c8fed59d96a94905c1a4070fdb79478c9/typedef.go that LPCWSTR is the equivalent of *uint16 so syscall.UTF16PtrFromString() seems to be what I need but I get an exception when I run the code.

I'm wondering if I'm supposed to use SwigcptrLPCWSTR or not.

libtest.c

#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;

#include &quot;libtest.h&quot;

__stdcall void hello(const LPCWSTR s)
{
	printf(&quot;hello: %ls\n&quot;, s);
}

libtest.h

#ifndef EXAMPLE_DLL_H
#define EXAMPLE_DLL_H

#include &lt;windows.h&gt;

#ifdef __cplusplus
extern &quot;C&quot; {
#endif

#ifdef BUILDING_EXAMPLE_DLL
#define EXAMPLE_DLL __declspec(dllexport)
#else
#define EXAMPLE_DLL __declspec(dllimport)
#endif

void __stdcall EXAMPLE_DLL hello(const LPCWSTR s);

#ifdef __cplusplus
}
#endif

#endif

I build the lib and the DLL with :

gcc -c -DBUILDING_EXAMPLE_DLL libtest.c
gcc -shared -o libtest.dll libtest.o -Wl,--out-implib,libtest.a

main.swig

%module main

%{
#include &quot;libtest.h&quot;
%}

%include &quot;windows.i&quot;
%include &quot;libtest.h&quot;

main.go

package main

import (
	&quot;syscall&quot;
	&quot;unsafe&quot;
)

func main() {
	p, err := syscall.UTF16PtrFromString(&quot;test&quot;)
	if err != nil {
		panic(err)
	}
	Hello(SwigcptrLPCWSTR(unsafe.Pointer(p)))
}

trace

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

答案1

得分: 2

我怀疑你遇到的问题是因为你传递给 SWIG 的是一个双指针,而不是一个指针,即 wchar_t** 而不是 wchar_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") // 注意这里的微妙变化
    if err != nil {
        panic(err)
    }
    Hello(SwigcptrLPCWSTR(unsafe.Pointer(p)))
}

它应该按预期工作。

英文:

I suspect the issue you're seeing is because what you're passing into SWIG is a double pointer instead of just a pointer, i.e. wchar_t** instead of just wchar_t*.

I think this comes about because you call UTF16PtrFromString which takes the address of the UTF16 string and then subsequently call unsafe.Pointer(p) which I think again takes the address of its input.

From the go source code:

func UTF16PtrFromString(s string) (*uint16) {
	a := UTF16FromString(s)
	return &amp;a[0]
}

So I think if you instead use:

func main() {
    p, err := syscall.UTF16FromString(&quot;test&quot;) // Note the subtle change here
    if err != nil {
        panic(err)
    }
    Hello(SwigcptrLPCWSTR(unsafe.Pointer(p)))
}

It should work as intended.

huangapple
  • 本文由 发表于 2015年8月19日 03:36:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/32081171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定