在Go中创建Windows快捷方式(.lnk)

Ale*_*ues 1 windows go desktop-shortcut

我想在Golang中为桌面和startmenu创建Windows快捷方式(.lnk)。

实际上,我是通过gowin模块获得Desktop&Startmenu文件夹的,我想为这些位置创建一个快捷方式。

我进行了搜索,但没有找到任何golang项目。我应该创建它吗?还有其他漂亮的方法吗?

tmm*_*mm1 5

使用https://github.com/go-ole/go-ole

ole.CoInitializeEx(0, ole.COINIT_APARTMENTTHREADED|ole.COINIT_SPEED_OVER_MEMORY)
oleShellObject, err := oleutil.CreateObject("WScript.Shell")
if err != nil {
    return err
}
defer oleShellObject.Release()
wshell, err := oleShellObject.QueryInterface(ole.IID_IDispatch)
if err != nil {
    return err
}
defer wshell.Release()
cs, err := oleutil.CallMethod(wshell, "CreateShortcut", dst)
if err != nil {
    return err
}
idispatch := cs.ToIDispatch()
oleutil.PutProperty(idispatch, "TargetPath", src)
oleutil.CallMethod(idispatch, "Save")
Run Code Online (Sandbox Code Playgroud)

  • 在生产环境中运行此程序数月后,我注意到一些罕见的失败并显示消息“CoInitialize 尚未调用。”。要解决此问题,您需要确保调用 runtime.LockOSThread(),或使用 https://github.com/go-ole/go-ole/issues/124 中概述的 comshim 库 (2认同)