在桌面和快速启动中创建快捷方式

//---------------------------------------------------------------------------

#ifndef ShortCutCreatorH
#define ShortCutCreatorH
#include <vcl.h>
#pragma hdrstop

#include <shlobj.h>

struct TShortcutCfg
{
    // 构造函数
    TShortcutCfg()
    {
        nShowCmd = SW_SHOWNORMAL;
        wHotKey = 0;
        nIconIndex = 0;
    }
    // 结构成员:
    AnsiString  strShortcutName; //
    AnsiString  strLnkDir;       //
    AnsiString  strDestFile;     //
    AnsiString  strArguments;    //
    AnsiString  strIconFile;     //
    int         nIconIndex;      //
    AnsiString  strWorkingDir;   //
    AnsiString  strDescription;  //
    WORD        wHotKey;         //
    int         nShowCmd;        //
};
class ShortCutCreator
{
public:
 bool CreateDesktopShortcut(String& destFile,String& shortCutName,String& arguments);
 bool CreateQuickLaunchShortcut(String& destFile,String& shortCutName,String& arguments);
private:
 bool CreateShortcut(String& destFile,String& shortCutName,String& dir,String& arguments);
};

#endif

//---------------------------------------------------------------------------

#include "ShortCutCreator.h"

#pragma argsused
// 在快速启动栏创建快捷方式
bool ShortCutCreator::CreateDesktopShortcut(String& destFile,String& shortCutName,String& arguments)
{
 char szBuf[MAX_PATH];
 LPITEMIDLIST lpItemIdList;
 SHGetSpecialFolderLocation(0, CSIDL_DESKTOPDIRECTORY, &lpItemIdList);
 SHGetPathFromIDList(lpItemIdList, szBuf);
 String dir = String(szBuf)+"//";
 bool result = CreateShortcut(destFile,shortCutName,dir,arguments);
 return result;
}
bool ShortCutCreator::CreateQuickLaunchShortcut(String& destFile,String& shortCutName,String& arguments)
{
    char szBuf[MAX_PATH];
 LPITEMIDLIST lpItemIdList;
 SHGetSpecialFolderLocation(0, CSIDL_APPDATA, &lpItemIdList);
 SHGetPathFromIDList(lpItemIdList, szBuf);
 String dir = String(szBuf) + "//Microsoft//Internet Explorer//Quick Launch//";
 bool result = CreateShortcut(destFile,shortCutName,dir,arguments);
 return result;
}
bool ShortCutCreator::CreateShortcut(String& destFile,String& shortCutName,String& dir,String& arguments)
{
 TShortcutCfg scConfig;
 scConfig.strDestFile = destFile;
 scConfig.strShortcutName = shortCutName;
 scConfig.strArguments = arguments;
 bool bReturn = true;
    wchar_t wszBuf[MAX_PATH];
    IShellLink *pShellLink;
 AnsiString strShortcutFile;

 strShortcutFile = dir + shortCutName + ".lnk";
 strShortcutFile.WideChar(wszBuf, MAX_PATH);

    if(bReturn)
    {
        bReturn = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                IID_IShellLink, (void **)&pShellLink) >= 0;

        if(bReturn)
        {
            IPersistFile *ppf;
            bReturn = pShellLink->QueryInterface(IID_IPersistFile, (void **)&ppf) >= 0;
            if(bReturn)
            {
                // 目标文件
    if(scConfig.strDestFile != EmptyStr)
     bReturn = pShellLink->SetPath(scConfig.strDestFile.c_str()) >= 0;
                // 参数
                if(bReturn && scConfig.strArguments != EmptyStr)
                 bReturn = pShellLink->SetArguments(scConfig.strArguments.c_str()) >= 0;
                // 显示图标
                if(bReturn && scConfig.strIconFile != EmptyStr && FileExists(scConfig.strIconFile))
                    pShellLink->SetIconLocation(scConfig.strIconFile.c_str(),
                            scConfig.nIconIndex);
                // 起始位置
                if(bReturn && scConfig.strWorkingDir != EmptyStr)
                    pShellLink->SetWorkingDirectory(scConfig.strWorkingDir.c_str());
                // 备注
                if(bReturn && scConfig.strDescription != EmptyStr)
                    pShellLink->SetDescription(scConfig.strDescription.c_str());
                // 快捷键
                if(bReturn && scConfig.wHotKey != 0)
                    pShellLink->SetHotkey(scConfig.wHotKey);
                // 运行方式
                if(bReturn && scConfig.nShowCmd != 0)
                    pShellLink->SetShowCmd(scConfig.nShowCmd);

                if(bReturn)
                    bReturn = (ppf->Save(wszBuf, TRUE) >= 0);

                ppf->Release ();
            }
            pShellLink->Release ();
        }
    }
    return bReturn;
}

int main(int argc,char* argv[])
{
 UnicodeString strDestFile = L"C://Program Files//Internet Explorer//iexplore.exe";
 UnicodeString strShortcutName = L"test125";
 UnicodeString arguments = L"fdafdafd";
    ShortCutCreator shortCutCreator;
 shortCutCreator.CreateDesktopShortcut(strDestFile,strShortcutName,arguments);
}

原文地址:https://www.cnblogs.com/jerry1999/p/3677363.html