系统调用 - 如何在 Go 中使用 LPWSTR?
syscall - How to use LPWSTR in Go?
我正在尝试为 Go 创建 Windows MMDevice API 的薄包装器,但我遇到了有关字符串的 Windows 数据类型的问题。
根据 IMMDevice::GetId method 的文档,它采用以下参数:
HRESULT GetId(
[out] LPWSTR *ppstrId
);
这是我的 Go 代码,对应于上面的方法。 (github.com/moutend/ywca/immdevice_windows.go:13)
func getId(mmd *IMMDevice, strId *uint16) (err error) {
hr, _, _ := syscall.Syscall(
mmd.VTable().GetId,
2,
uintptr(unsafe.Pointer(mmd)),
uintptr(unsafe.Pointer(strId)),
0)
// ...
}
我的理解是LPWSTR是指向uint16值数组的指针,但它会导致无效指针错误。
在这种情况下我应该使用什么类型?谢谢
首先要做的是阅读 Windows 函数的文档。
HRESULT GetId(
[out] LPWSTR *ppstrId
);
Parameters
ppstrId
[out]
Pointer to a pointer variable into which the method writes the address
of a null-terminated, wide-character string containing the endpoint
device ID. The method allocates the storage for the string. The caller
is responsible for freeing the storage, when it is no longer needed,
by calling the CoTaskMemFree function. If the GetId call fails,
*ppstrId is NULL. For information about CoTaskMemFree, see the Windows SDK documentation.
Return value
If the method succeeds, it returns S_OK. If it fails, possible return
codes include, but are not limited to, the values shown in the
following table.
特别是,"ppstrId [out] Pointer to a pointer variable ..." 你有 strId *uint16
或 *pstrId
,而我希望你有 strId **uint16
或 *ppstrId
。
它是一个指向指针的指针。 LPWSTR 类型是 wchar_t*
,因此该方法中的参数是 wchar_t**
.
您没有传入要填充的方法的字符串缓冲区。该方法会用CoTaskMemAlloc
和return分配内存,这个内存地址填满后还给你。您有责任使用 CoTaskMemAlloc
.
释放此内存
我正在尝试为 Go 创建 Windows MMDevice API 的薄包装器,但我遇到了有关字符串的 Windows 数据类型的问题。 根据 IMMDevice::GetId method 的文档,它采用以下参数:
HRESULT GetId(
[out] LPWSTR *ppstrId
);
这是我的 Go 代码,对应于上面的方法。 (github.com/moutend/ywca/immdevice_windows.go:13)
func getId(mmd *IMMDevice, strId *uint16) (err error) {
hr, _, _ := syscall.Syscall(
mmd.VTable().GetId,
2,
uintptr(unsafe.Pointer(mmd)),
uintptr(unsafe.Pointer(strId)),
0)
// ...
}
我的理解是LPWSTR是指向uint16值数组的指针,但它会导致无效指针错误。 在这种情况下我应该使用什么类型?谢谢
首先要做的是阅读 Windows 函数的文档。
HRESULT GetId( [out] LPWSTR *ppstrId );
Parameters
ppstrId
[out]Pointer to a pointer variable into which the method writes the address of a null-terminated, wide-character string containing the endpoint device ID. The method allocates the storage for the string. The caller is responsible for freeing the storage, when it is no longer needed, by calling the CoTaskMemFree function. If the GetId call fails, *ppstrId is NULL. For information about CoTaskMemFree, see the Windows SDK documentation.
Return value
If the method succeeds, it returns S_OK. If it fails, possible return codes include, but are not limited to, the values shown in the following table.
特别是,"ppstrId [out] Pointer to a pointer variable ..." 你有 strId *uint16
或 *pstrId
,而我希望你有 strId **uint16
或 *ppstrId
。
它是一个指向指针的指针。 LPWSTR 类型是 wchar_t*
,因此该方法中的参数是 wchar_t**
.
您没有传入要填充的方法的字符串缓冲区。该方法会用CoTaskMemAlloc
和return分配内存,这个内存地址填满后还给你。您有责任使用 CoTaskMemAlloc
.