Windows下c++创建快捷方式

// szStartAppPath : 点击后启动的程序
// szAddCmdLine : 传给main函数的lpCmdLine
// szDestLnkPath : 快捷方式的保存路径
// szIconPath : 快捷方式显示的图标
#ifdef _UNICODE
    typedef wstring tstring;
#else
    typedef string tstring;
#endif

bool CreateLinkFile(LPCTSTR szStartAppPath,LPCTSTR szAddCmdLine,LPCTSTR 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(_T("/\\"));
            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;
}
posted @ 2012-09-27 17:51  likebeta  阅读(4454)  评论(1编辑  收藏  举报