您的位置:首页 > 其它

MFC 创建快捷方式(学习笔记)

2010-08-09 22:15 316 查看
下面的代码不是我写的,是用的别人的源代码,这里记录下来,以便以后学习

void CFileOperateDlg::OnCreateShotcut()
{
//创建快捷方式

CString lpszPathLink("C:/aaa.lnk");
LPCSTR lpszPathObj="C:/aaa.txt";
LPCSTR lpszDesc="link for a txt";
CoInitialize(NULL);
HRESULT hres;
IShellLink* psl;
// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
//设置快捷方式的目标路径,添加描述
psl->SetPath(lpszPathObj);
psl->SetDescription(lpszDesc);
// Query IShellLink for the IPersistFile interface for saving the
// shortcut in persistent storage.
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
if (SUCCEEDED(hres))
{
WCHAR wsz[MAX_PATH];
// Ensure that the string is Unicode.
MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);
// TODO: Check return value from MultiByteWideChar to ensure
// Save the link by calling IPersistFile::Save.
hres = ppf->Save(wsz, TRUE);
ppf->Release();
}
psl->Release();
}
// return hres;
CoUninitialize();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: