zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    python多进程(一)
    python操作memcached
    python操作redis
    SQLAlchemy总结
    SQLAlchemy-ORM
    python操作mysql二
    python操作mysql
    python正则二
    python正则
    python内置模块(三)
  • 原文地址:https://www.cnblogs.com/qintangtao/p/2918184.html
Copyright © 2011-2022 走看看