用lxn/win处理golang中的LPTSTR [英] Handling LPTSTR in golang with lxn/win

查看:71
本文介绍了用lxn/win处理golang中的LPTSTR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码,它运行时不返回 err ,但是只是不执行其工作,因为它没有返回期望值.这个想法是使用 SHGetSpecialFolderPath ,以检索Windows目录的路径(例如, C:\ Windows ).此api调用具有以下签名:

I have this piece of code which runs without returning err but simply doesn't do its job because it doesn't return the expected value. The idea is to use SHGetSpecialFolderPath in order to retrieve the path to the Windows directory (C:\Windows for example). This api call has the following signature:

BOOL SHGetSpecialFolderPath(
        HWND   hwndOwner, 
        _Out_ LPTSTR lpszPath,
        _In_  int    csidl,
        _In_  BOOL   fCreate );

我知道它已被弃用,但即使在当前Windows版本上仍然可用.我必须使用此API,因为我需要支持Windows 7之前的Windows版本(我知道这些版本很旧,甚至寿命已尽)

I know it is deprecated, but still available even on current Windows versions. I have to use this API because I need to support Windows versions older than Windows 7 (I know that these are old or even end of life)

这是一段代码:

target := "XXX...XXX" // hard coded string with more than 600 characters
buffer, err := syscall.UTF16PtrFromString(target)
if err != nil {
        fmt.Println("conversion of string:", err)
}

result := win.SHGetSpecialFolderPath(0, buffer, win.CSIDL_WINDOWS, false)
if err != nil {
    fmt.Println("result of get folder:", err)
}

fmt.Println("folder retrieved ok: ", result)
fmt.Println("folder: ", target)
}

未设置任何 err ,API调用返回 true ,但字符串未更改:

None of the err is set, the API call returns true but the string is unchanged:

folder retrieved ok:  true
folder: XXX...XXX

在Windows 10 x64和运行Windows XP SP3的测试VM上,结果是相同的(我知道XP本质上是不安全的)

The result is the same on Windows 10 x64 and on my testing VM running Windows XP SP3 (I know that XP is inherently unsafe)

我看过示例如何将 LPTRSTR unsafe uintptr 结合使用,在这里"上,但没有一个在我的golang版本上编译(gocode版本为go1.10.1 windows/amd64 ,我使用 GOARCH = 386 )

I have seen examples how to use LPTRSTR with unsafe and uintptr here on SO and other sites but none of them compile on my version of golang (which is go version go1.10.1 windows/amd64, I compiled with GOARCH=386)

推荐答案

以逻辑,系统的方式解决问题.

Approach the problem in a logical, systematic fashion.

仔细阅读该功能的Microsoft文档.

Carefully read the Microsoft documentation for the function.

SHGetSpecialFolderPath函数

仔细阅读该功能的 lxn/win 软件包文档.

Carefully read the lxn/win package documentation for the function.

程序包胜利

import "github.com/lxn/win" 

功能SHGetSpecialFolderPath

func SHGetSpecialFolderPath(hwndOwner HWND, lpszPath *uint16, csidl CSIDL, fCreate bool) bool


现在,使用文档,在Go中实现函数调用.Go Unicode字符串是UTF-8编码的.Windows Unicode字符串是UTF-16编码的.


Now, using the documentation, implement the function call in Go. Go Unicode strings are UTF-8 encoded. Windows Unicode strings are UTF-16 encoded.

package main

import (
    "fmt"
    "syscall"

    "github.com/lxn/win"
)

func main() {
    buf := make([]uint16, win.MAX_PATH)
    rv := win.SHGetSpecialFolderPath(win.HWND(0), &buf[0], win.CSIDL_WINDOWS, false)
    fmt.Println(rv)
    path := syscall.UTF16ToString(buf)
    fmt.Println(path)
}

输出:

true
C:\Windows

这篇关于用lxn/win处理golang中的LPTSTR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆