douqin2108 2018-04-18 22:33
浏览 175
已采纳

用lxn / win处理golang中的LPTSTR

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

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)

This is the piece of code:

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

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

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

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)

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)

  • 写回答

1条回答 默认 最新

  • duandushang5148 2018-04-19 03:16
    关注

    Approach the problem in a logical, systematic fashion.


    Carefully read the Microsoft documentation for the function.

    SHGetSpecialFolderPath function


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

    package win

    import "github.com/lxn/win" 
    

    func SHGetSpecialFolderPath

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

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

    Output:

    true
    C:\Windows
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题