c++创建快捷方式

//封装的创建快捷方式API
// szStartAppPath : 点击后启动的程序
// szAddCmdLine : 传给main函数的lpCmdLine
// szDestLnkPath : 快捷方式的保存路径
// szIconPath : 快捷方式显示的图标
bool CreateLinkFile(LPCTSTR szStartAppPath, LPCTSTR szAddCmdLine, LPCOLESTR szDestLnkPath, LPCTSTR szIconPath)
{
    HRESULT hr = CoInitialize(NULL);
    if (SUCCEEDED(hr))
    {
        IShellLink *pShellLink;
        hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pShellLink);
        if (SUCCEEDED(hr))
        {
            pShellLink->SetPath(szStartAppPath);
            tstring strTmp = szStartAppPath;
            int nStart = strTmp.find_last_of(TEXT("/\\"));
            pShellLink->SetWorkingDirectory(strTmp.substr(0, nStart).c_str());
            pShellLink->SetArguments(szAddCmdLine);
            if (szIconPath)
            {
                pShellLink->SetIconLocation(szIconPath, 0);
            }
            IPersistFile* pPersistFile;
            hr = pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile);
            if (SUCCEEDED(hr))
            {
                hr = pPersistFile->Save((szDestLnkPath), FALSE);
                if (SUCCEEDED(hr))
                {
                    return true;
                }
                pPersistFile->Release();
            }
            pShellLink->Release();
        }
        CoUninitialize();
    }
    return false;
}
//百度的string转换wstring
std::wstring s2ws(const std::string& s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;
}

 

原文链接: https://www.cnblogs.com/m1911/p/17050006.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++创建快捷方式

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/311691

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 下午12:06
下一篇 2023年2月16日 下午12:06

相关推荐