【发布时间】:2017-03-17 03:51:10
【问题描述】:
我正在编写一个使用go-bindata嵌入图像资源的golang程序,并使用Asset(string) ([]byte, error)函数访问资源。但是我现有的库代码是这样的:
func NewIconFromFile(filePath string) (uintptr, error) {
absFilePath, err := filepath.Abs(filePath)
if err != nil {
return 0, err
}
hicon, _, _ := LoadImage.Call(
0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(absFilePath))),
IMAGE_ICON,
0,
0,
LR_DEFAULTSIZE|LR_LOADFROMFILE)
if hicon == 0 {
return 0, errors.New("load image failed: " + filePath)
}
return hicon, nil
}
如何将这些函数重写为:
func NewIconFromRawBytes(imgBytes []byte) (uintptr, error)
所以它可以支持从[]byte 加载图像?有什么帮助吗?谢谢。
编辑:有a similar c++ version question,如何将其移植到golang。
【问题讨论】:
-
你为什么要返回
uintptr? -
什么是
LoadImage?您的代码中似乎有很多假设没有得到充分解释,无法提供答案。 -
@apxp:这不是警告的意思,只要你看到“不安全”就盲目地重复是没有用的。在 Go 中,很多东西都需要
unsafepackage,例如使用sys/syscall软件包,并且在未来版本中完全支持正确使用。该警告意味着可以以依赖内部实现或平台特定细节的方式使用 unsafe,而不是简单地导入它会使您的程序不合规。 -
@Flimzy 原始代码可以在github repo找到:xilp/systray。
-
@Flimzy 此代码严格处理 Windows API;如果您知道在 Go 中使用它是什么,则没有任何假设。