调用Windows函数(获取字体目录)

我尝试找出Windows安装上的字体文件夹。 AFAICS提出的方法是在 Shell32.dll </ code>中调用 SHGetKnownFolderPath </ code>,并将 KNOWNFOLDERID </ code>设置为 FOLDERID_Fonts </ code>。 </ p>

我不知道如何在下面的代码中传递给 Call </ code>函数:</ p>

 包主 

import(
“ syscall”

func main(){
// HRESULT SHGetKnownFolderPath(
// In REFKNOWNFOLDERID rfid,
// In DWORD dwFlags,
// In_opt HANDLE hToken,
// Out PWSTR * ppszPath
//);

var(
shell32 = syscall.NewLazyDLL(“ Shell32.dll”)
shGetKnowFolderPath = shell32.NewProc(“ SHGetKnownFolderPath”)

//当然不起作用:
folderId int
标志int
处理int
retval int

shGetKnowFolderPath.Call(uintptr(folderId),uintptr(flags),uintptr (句柄),uintptr(retval))
}
</ code> </ pre>

有什么想法吗? (我想目前的解决方法是坚持使用%windir%\ Fonts </ code>,但我想获得适当的解决方案。)</ p>

参考: </ p>

展开原文

原文

I try to find out the font folder on a windows installation. AFAICS the proposed way is to call SHGetKnownFolderPath in Shell32.dll with KNOWNFOLDERID set to FOLDERID_Fonts.

I have no idea what to pass to the Call function in the code below:

package main

import (
    "syscall"
)

func main() {
    // HRESULT SHGetKnownFolderPath(
    //   _In_      REFKNOWNFOLDERID rfid,
    //   _In_      DWORD dwFlags,
    //   _In_opt_  HANDLE hToken,
    //   _Out_     PWSTR *ppszPath
    // );

    var (
        shell32             = syscall.NewLazyDLL("Shell32.dll")
        shGetKnowFolderPath = shell32.NewProc("SHGetKnownFolderPath")

        // Doesn't work, of course:
        folderId int
        flags    int
        handle   int
        retval   int
    )

    shGetKnowFolderPath.Call(uintptr(folderId), uintptr(flags), uintptr(handle), uintptr(retval))
}

Any idea? (I guess a workaround for now would be to stick to %windir%\Fonts, but I'd like to get a proper solution).

References:

duanlu1876
duanlu1876 为什么要下票?我该怎么做才能改善这个问题?
大约 7 年之前 回复

4个回答



golang.org/x/sys/windows </ code>现在具有用于 SHGetKnownFolderPath </ code的包装器 >,所以这要容易得多:</ p>

 包main 

import(
“ fmt”

“ golang .org / x / sys / windows“

func main(){
路径,错误:= windows.KnownFolderPath(windows.FOLDERID_Fonts,0)
如果错误!= nil {
fmt.Println (err)
返回
}
fmt.Println(path)
}
</ code> </ pre>
</ div>

展开原文

原文

golang.org/x/sys/windows now has a wrapper for SHGetKnownFolderPath, so this is much easier:

package main

import (
    "fmt"

    "golang.org/x/sys/windows"
)

func main() {
    path, err := windows.KnownFolderPath(windows.FOLDERID_Fonts, 0)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(path)
}

dsrw29618
dsrw29618 比以前接受的答案容易得多。
大约一年之前 回复

For example,

package main

import (
    "fmt"
    "syscall"
    "unsafe"
)

type GUID struct {
    Data1 uint32
    Data2 uint16
    Data3 uint16
    Data4 [8]byte
}

var (
    FOLDERID_Fonts = GUID{0xFD228CB7, 0xAE11, 0x4AE3, [8]byte{0x86, 0x4C, 0x16, 0xF3, 0x91, 0x0A, 0xB8, 0xFE}}
)

var (
    modShell32               = syscall.NewLazyDLL("Shell32.dll")
    modOle32                 = syscall.NewLazyDLL("Ole32.dll")
    procSHGetKnownFolderPath = modShell32.NewProc("SHGetKnownFolderPath")
    procCoTaskMemFree        = modOle32.NewProc("CoTaskMemFree")
)

func SHGetKnownFolderPath(rfid *GUID, dwFlags uint32, hToken syscall.Handle, pszPath *uintptr) (retval error) {
    r0, _, _ := syscall.Syscall6(procSHGetKnownFolderPath.Addr(), 4, uintptr(unsafe.Pointer(rfid)), uintptr(dwFlags), uintptr(hToken), uintptr(unsafe.Pointer(pszPath)), 0, 0)
    if r0 != 0 {
        retval = syscall.Errno(r0)
    }
    return
}

func CoTaskMemFree(pv uintptr) {
    syscall.Syscall(procCoTaskMemFree.Addr(), 1, uintptr(pv), 0, 0)
    return
}

func FontFolder() (string, error) {
    var path uintptr
    err := SHGetKnownFolderPath(&FOLDERID_Fonts, 0, 0, &path)
    if err != nil {
        return "", err
    }
    defer CoTaskMemFree(path)
    folder := syscall.UTF16ToString((*[1 << 16]uint16)(unsafe.Pointer(path))[:])
    return folder, nil
}

func main() {
    folder, err := FontFolder()
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println("Font Folder:", folder)
}

Output:

Font Folder: C:\Windows\Fonts
duanchao4445
duanchao4445 不错的答案...仅添加代码不会对新开发人员有所帮助...对于您添加流程的详细信息将非常有用
大约 7 年之前 回复
dongtuoleng8624
dongtuoleng8624 很好,谢谢! 仍在尝试了解这一点。 给我一天:)
大约 7 年之前 回复



以防万一有人需要golang格式文档的文件夹ID:</ p>

将其替换为 peterSO的代码示例:</ p>

  FOLDERID_Documents = GUID {0xFDD39AD0,0x238F,0x46AF,[8] byte {0xAD,0xB4,0x6C,0x85,0x48,0x03,0x69,0xC7}}  

FOLDERID_PublicDocuments = GUID {0xED4824AF,0xDCE4、0x45A8,[8] byte {0x81、0xE2、0xFC,0x79、0x65、0x08、0x36、0x34}}
</ code> </ pre>

< p>请参见</ p>

https://msdn.microsoft.com/zh-cn/library/windows/desktop/dd378457(v = vs.85).aspx </ p>
</ div >

展开原文

原文

Just in case someone needs the folder id for documents in golang format:

Replace this in peterSO's code example:

FOLDERID_Documents = GUID{0xFDD39AD0, 0x238F, 0x46AF, [8]byte{0xAD, 0xB4, 0x6C, 0x85, 0x48, 0x03, 0x69, 0xC7}}

FOLDERID_PublicDocuments = GUID{0xED4824AF, 0xDCE4, 0x45A8, [8]byte{0x81, 0xE2, 0xFC, 0x79, 0x65, 0x08, 0x36, 0x34}}

see

https://msdn.microsoft.com/en-us/library/windows/desktop/dd378457(v=vs.85).aspx

字体目录的



folderId </ code>是 {FD228CB7-AE11-4AE3-864C-16F3910AB8FE} < / code> </ p>

标志</ code>可能是 SHGFP_TYPE_DEFAULT </ code> </ p>

handle </ code>可能是 NULL </ code> </ p>

retval </ code> 我认为</ em>应该是 bool </ 代码>,但不确定。</ p>

所有答案均来自您先前链接的 SHGetFolderPath函数。</ p>
</ div>

展开原文

原文

folderId for the fonts directory is {FD228CB7-AE11-4AE3-864C-16F3910AB8FE}

flags can probably be SHGFP_TYPE_DEFAULT

handle can probably be NULL

retval I think should be a bool but I'm not sure.

All answers are from your previously linked SHGetFolderPath function.

doutang1873
doutang1873 谢谢,这很有意义。
大约 7 年之前 回复
dongxian8272
dongxian8272 返回值是HRESULT类型。 可以使用宏SUCCEEDED(hr)或FAILED(hr)将其转换为布尔值成功代码。
大约 7 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐