简体   繁体   中英

Pass String to argument of Syscall in Go

I would like to use a C function compiled into a DLL. Here is the C function that I would like to use:

int setProxy(const proxy_config* p_proxy);

It takes a const pointer to a C structure defined like this:

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:

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")

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.

Thus, my question is:

How can I pass the whole strings in the parameters of Syscall and not just the first character?


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...

Pass String to argument of Syscall in Go

How can I pass the whole strings in the parameters of [Windows] Syscall?


An obvious answer is to look at the Go standard libraries, which make many Windows syscalls.


package windows

golang.org/x/sys/windows

func UTF16FromString

 func UTF16FromString(s string) ([]uint16, error)

UTF16FromString returns the UTF-16 encoding of the UTF-8 string s, with a terminating NUL added. If s contains a NUL byte at any location, it returns (nil, syscall.EINVAL).

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. If s contains a NUL byte at any location, it returns (nil, syscall.EINVAL).

func UTF16ToString

 func UTF16ToString(s []uint16) string

UTF16ToString returns the UTF-8 encoding of the UTF-16 sequence s, with a terminating NUL removed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM