[英]Pass String to argument of Syscall in Go
I would like to use a C function compiled into a DLL.我想使用编译成 DLL 的 C 函数。 Here is the C function that I would like to use:
这是我想使用的 C 函数:
int setProxy(const proxy_config* p_proxy);
It takes a const pointer to a C structure defined like this:它需要一个指向 C 结构的 const 指针,定义如下:
typedef struct
{
char *host_address;
int port;
char *username;
char *password;
} proxy_config;
To do so, I transformed this C_structure into a Go equivalent as follows:为此,我将这个 C_structure 转换为 Go 等价物,如下所示:
type proxy_configuration struct {
host_address *uint16
port int
username *uint16
password *uint16
}
To convert the strings, I use the following function:要转换字符串,我使用以下函数:
func StringToCString(s string) (*uint16, error) {
for i := 0; i < len(s); i++ {
if s[i] == 0 {
return nil, errors.New("Problem with the string : "+s)
}
}
p := utf16.Encode([]rune(s + "\x00"))
return &p[0], nil
}
Then I call it:然后我称之为:
import (
"syscall"
"unsafe"
)
func SetProxy(host_address string, port int, username string, password string) error {
[...Conversion code from Go string to Struct...]
var nargs uintptr = 1
ret, _, callErr := syscall.Syscall(
uintptr(setProxy),
nargs,
uintptr(unsafe.Pointer(&proxy)),
0,
0,
)
}
where setProxy
is the result of syscall.GetProcAddress(DLL,"setProxy")
其中
setProxy
是syscall.GetProcAddress(DLL,"setProxy")
The problem is that, when I have a look at the logs generated by the DLL, all the *uint16
contains only one character and not the whole string whereas port contains the good information.问题是,当我查看 DLL 生成的日志时,所有
*uint16
只包含一个字符而不是整个字符串,而 port 包含好的信息。
Thus, my question is:因此,我的问题是:
How can I pass the whole strings in the parameters of Syscall
and not just the first character?如何在
Syscall
的参数中传递整个字符串而不仅仅是第一个字符?
EDIT编辑
If I change return &p[0], nil
in the StringToCString
by return &p[i], nil
I obtain the i-th character of the string... So the problem is definely there, I just do know how to fix it...如果我通过
return &p[i], nil
在StringToCString
更改return &p[0], nil
我获得字符串的第 i 个字符......所以问题肯定存在,我只知道如何解决它。 ..
Pass String to argument of Syscall in Go
在 Go 中将字符串传递给 Syscall 的参数
How can I pass the whole strings in the parameters of [Windows] Syscall?
如何在 [Windows] Syscall 的参数中传递整个字符串?
An obvious answer is to look at the Go standard libraries, which make many Windows syscalls.一个显而易见的答案是查看 Go 标准库,它进行了许多 Windows 系统调用。
golang.org/x/sys/windows
func UTF16FromString
函数 UTF16FromString
func UTF16FromString(s string) ([]uint16, error)
UTF16FromString returns the UTF-16 encoding of the UTF-8 string s, with a terminating NUL added.
UTF16FromString 返回 UTF-8 字符串 s 的 UTF-16 编码,并添加终止 NUL。 If s contains a NUL byte at any location, it returns (nil, syscall.EINVAL).
如果 s 在任何位置包含 NUL 字节,则返回 (nil, syscall.EINVAL)。
func UTF16PtrFromString
func UTF16PtrFromString
func UTF16PtrFromString(s string) (*uint16, error)
UTF16PtrFromString returns pointer to the UTF-16 encoding of the UTF-8 string s, with a terminating NUL added.
UTF16PtrFromString 返回指向 UTF-8 字符串 s 的 UTF-16 编码的指针,并添加终止 NUL。 If s contains a NUL byte at any location, it returns (nil, syscall.EINVAL).
如果 s 在任何位置包含 NUL 字节,则返回 (nil, syscall.EINVAL)。
func UTF16ToString
函数 UTF16ToString
func UTF16ToString(s []uint16) string
UTF16ToString returns the UTF-8 encoding of the UTF-16 sequence s, with a terminating NUL removed.
UTF16ToString 返回 UTF-16 序列 s 的 UTF-8 编码,删除了终止 NUL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.