设置快捷方式

来源:互联网 发布:海岛奇兵英雄升级数据 编辑:程序博客网 时间:2024/05/16 01:41
//创建快捷方式 //快捷方式的目标应用程序名  //快捷方式的数据文件名(*.lnk)
static BOOL CreateLink(LPTSTR szPath, LPTSTR szLink, bool flag)
{
    HRESULT hres ;
    IShellLink * psl ;
    IPersistFile* ppf ;
    TCHAR wsz[ MAX_PATH] ;
    CoInitialize(NULL);
    //创建一个IShellLink实例
    hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl);
    if (FAILED(hres))    
    {
        CString strErr;
        strErr.Format(_T("CoCreateInstance fail code=%ld"), hres);
        ::MessageBox(NULL, strErr, 0, 0);
        return FALSE ;
    }
    //设置目标应用程序
    psl->SetPath(szPath) ;

    if ( flag)
    {
        psl->SetArguments(_T(" /MiniIcon 1"));//设置命令行参数
        CString mzdPath = szPath ;
        DWORD dwPos = mzdPath.ReverseFind('\\');
        mzdPath = mzdPath.Left(dwPos + 1) ;
        mzdPath += _T("MZDhttpsvc.ico") ;
        HRESULT hress = psl->SetIconLocation(mzdPath,0) ;//图标的位置设置
    }  
    //设置快捷键(此处设为Shift+Ctrl+'R')
    //psl -> SetHotkey( MAKEWORD( 'R', HOTKEYF_SHIFT |HOTKEYF_CONTROL)) ;

    //从IShellLink获取其IPersistFile接口
    //用于保存快捷方式的数据文件 (*.lnk)
    hres = psl->QueryInterface( IID_IPersistFile,(void**)&ppf) ;
    if (FAILED( hres))     
    {
        CString strErr;
        strErr.Format(_T("QueryInterface fail code=%ld"), hres);
        ::MessageBox(NULL, strErr, 0, 0);
        return FALSE ;
    }
#ifndef _UNICODE
    // 确保数据文件名为ANSI格式
    MultiByteToWideChar( CP_ACP, 0, szLink, -1, wsz, MAX_PATH) ;
#else
    lstrcpy(wsz, szLink);
#endif
    //调用IPersist:Save
    //保存快捷方式的数据文件 (*.lnk)
    hres = ppf->Save(wsz, STGM_READWRITE);
    if (FAILED(hres))     
    {
        CString strErr;
        strErr.Format(_T("Save fail code=%ld"), hres);
        ::MessageBox(NULL, strErr, 0, 0);
    }
    //释放IPersistFile和IShellLink接口
    ppf -> Release( ) ;
    psl -> Release( ) ;

    return TRUE;
}