• vc 创建桌面快捷方式


    BOOL CDlg::CreateDesktopShotCut(CString strName, CString strSourcePath)
    {
        if (FAILED(CoInitialize(NULL)))
            return FALSE;
        BOOL bRet = FALSE;
        
        TCHAR Path[MAX_PATH+1];
        CString strDestDir;
        LPITEMIDLIST pidl;
        LPMALLOC pShell;
        if(SUCCEEDED(SHGetMalloc(&pShell)))
        {
            if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOPDIRECTORY,&pidl)))
            {
                if(!SHGetPathFromIDList(pidl,Path))
                {
                    pShell->Free(pidl);
                }
                pShell->Release();
                strDestDir.Format(_T("%s"),Path);
                strDestDir+= _T("\\");
                strDestDir+= strName;//设置桌面快捷方式的名字
                strDestDir+= _T(".lnk");
                IShellLink* psl;
                
                if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl)))
                {
                    psl->SetPath(strSourcePath + _T("Player.exe"));//设置快捷方式的目标位置 
                    //比如目标位置为C:\windows\a.txt 起始位置就应该设置为C:\windows否则会导致不可预料的错误
                    
                    //如果是文件夹的快捷方式起始位置和目标位置可以设置为一样
                    
                    psl->SetWorkingDirectory(strSourcePath); //设置快捷方式的起始位置 
                    IPersistFile* ppf;
                    if(SUCCEEDED(psl->QueryInterface(IID_IPersistFile,(LPVOID*)&ppf)))
                    {
                        //WCHAR wsz[MAX_PATH];
                        //MultiByteToWideChar(CP_THREAD_ACP,MB_PRECOMPOSED, (LPCTSTR)strDestDir,-1,wsz,MAX_PATH);//设置桌面快捷方式的名字
                        if(SUCCEEDED(ppf->Save(strDestDir,TRUE)))//保存快捷方式到桌面 
                        {
                            ppf->Release();
                            psl->Release();
                            bRet = TRUE;
                        }else{
                            ppf->Release();
                            psl->Release();
                        }
                    }else{
                        ppf->Release();
                        psl->Release();
                    }
                }
            }
        }
    
        CoUninitialize();
    
        if (!bRet)
        {
            LPVOID   lpMsgBuf; 
            FormatMessage( 
                FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 
                NULL, 
                GetLastError(), 
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language 
                (LPTSTR) &lpMsgBuf, 
                0, 
                NULL 
                ); 
            //   Display   the   string. 
            TRACE(_T("----------------Create Link Error: %s\r\n"), (TCHAR*)lpMsgBuf);
            _ComnLog->LogOutV(_T("----------------CreateDesktopShotCut Failed: %s\r\n"), (TCHAR*)lpMsgBuf);
            //   Free   the   buffer. 
            LocalFree(   lpMsgBuf   ); 
        }
        return bRet;
    }
  • 相关阅读:
    js实现base64转换
    使用maven命令终端构建一个web项目及发布该项目
    使用eclipse构建Maven项目及发布一个Maven项目
    Maven在Windows上的安装与配置
    centos7下安装配置redis3.0.4
    Centos7下完美安装并配置mysql5.6
    linux常用命令总结
    VMware下centos桥接模式静态ip配置
    解决centos7下tomcat启动正常,无法访问项目的问题
    centos7系统下安装配置jdk、tomcat教程
  • 原文地址:https://www.cnblogs.com/qintangtao/p/2918184.html
Copyright © 2020-2023  润新知