Go 语言 syscall.CreateProcess 错误:无法将 *string 转换为 *uint16
在 syscall.CreateProcess 函数的第二个参数中,您尝试将一个指向字符串的指针(*string)转换为指向 uint16 类型的指针(*uint16)。这是错误的,因为这两种指针类型是不兼容的。
要解决此错误,您可以使用 syscall.UTF16PtrFromString 函数将字符串转换为指向 uint16 类型的指针。例如,将以下行:
syscall.CreateProcess(nil, &cmd, nil, nil, true, 0, nil, nil, &si, &pi)
更改为:
cmdPtr, err := syscall.UTF16PtrFromString(cmd)
if err != nil {
panic(err)
}
syscall.CreateProcess(nil, cmdPtr, nil, nil, true, 0, nil, nil, &si, &pi)
注意:
syscall.UTF16PtrFromString函数将一个字符串转换为一个指向uint16类型的指针,该指针指向一个包含该字符串的 UTF-16 编码的字节数组。- 如果您在 Windows 上运行您的代码,则需要确保您正在使用正确的字符集。
原文地址: https://www.cveoy.top/t/topic/mrys 著作权归作者所有。请勿转载和采集!