您的位置:首页 > 其它

创建快捷方式

2015-07-03 14:27 260 查看
BOOL CFileTool::CreateShortcuts(const CString *pFile,
const CString *pPath,
const CString *pIconLocation,
const CString *pArguments,
const CString *pWorkingDirectory,
WORD pHotkey,
int pShowCmd,
const PCIDLIST_ABSOLUTE pIDList,
const CString *pRelativePat,
const CString *pDescription)
{
// 入口参数检查
if (NULL == pFile || NULL == pPath)
{
return FALSE;
}

HRESULT hRet;
//初始化COM库
hRet = ::CoInitialize(NULL);
if (hRet != S_OK)  //初始化COM库失败,直接返回
{
return FALSE;
}

IShellLink *psl = NULL;        //IShellLink对象指针
IPersistFile *ppf = NULL;    //IPersisFil对象指针

//创建IShellLink实例
hRet = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (void**)&psl);
if (hRet != S_OK)  //初始化COM库失败,直接返回
{
//释放COM接口
::CoUninitialize();
return FALSE;
}

//从IShellLink对象中获取IPersistFile接口
hRet = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
if (hRet != S_OK)  //初始化COM库失败,直接返回
{
//释放IShellLink对象
psl->Release();
//释放COM接口
::CoUninitialize();
return FALSE;
}

//1.设置快捷方式中的程序路径
psl->SetPath(*pPath);

//2.设置图标
if (pIconLocation != NULL && pIconLocation[0] != L'\0')
{
psl->SetIconLocation(*pIconLocation, 0);
}

//3.设置快捷方式的工作目录
if (pWorkingDirectory != NULL && pWorkingDirectory[0] != L'\0')
{
psl->SetWorkingDirectory(*pWorkingDirectory);
}

//4.快捷键
if (pHotkey > 0)
{
psl->SetHotkey(pHotkey);
}

//5.快捷方式的运行方式,比如常规窗口,最大化
if (pShowCmd >= 0)
{
psl->SetShowCmd(pShowCmd);
}

//6.获得快捷方式的目标对象的item identifier list (Windows外壳中的每个对象如文件,目录和打印机等都有唯一的item identifiler list)
if (pIDList != NULL)
{
psl->SetIDList(pIDList);
}

//7.按照一定的搜索规则试图获得目标对象,即使目标对象已经被删除或移动,重命名
if (pRelativePat != NULL && pRelativePat[0] != L'\0')
{
psl->SetRelativePath(*pRelativePat, 0);
}

//8.参数信息
if (pArguments != NULL && pArguments[0] != L'\0')
{
psl->SetArguments(*pArguments);
}

//9.描述信息(备注行)
if (pDescription != NULL && pDescription[0] != L'\0')
{
psl->SetDescription(*pDescription);
}

//确保快捷方式路径由ANSI字符串组成
//!!这个文件名改变了,就需要删除原有快捷方式,没改,就直接创建,不用删除原有的。
//CString wsz = L"C:\\Documents and Settings\\Gongjian\\桌面\\vc创建的快捷方式.lnk";

//保存快捷方式
ppf->Save(*pFile, TRUE);

//释放IPersistFile接口
ppf->Release();
//释放IShellLink对象
psl->Release();
//释放COM接口
::CoUninitialize();

return TRUE;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: