如何在桌面或者开始菜单中创建快捷方式
2005-03-22 10:06
701 查看
//在桌面或者开始菜单中创建快捷方式
//pazSrcPath:源文件路径
//bDesktop:标志位,用来判断是否在桌面上创建快捷方式,真,在桌面上创建,否则在开始菜单中创建
bool createShortcut(const char* pszSrcPath, bool bDesktop)
{
CoInitialize(NULL);
bool bRet = false;
IShellLink* psl;
LPITEMIDLIST pidl;
LPMALLOC pShellMalloc;
std::string strDesktopPath;
std::string strStartMenuPath;
const int nFolder[2] = { CSIDL_DESKTOPDIRECTORY,CSIDL_STARTMENU };
if (SUCCEEDED(SHGetMalloc(&pShellMalloc))) {
char Path[MAX_PATH + 1];
for (int i = 0; i < 2; i++) {
if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, nFolder[i], &pidl))) {
if (SHGetPathFromIDList(pidl, Path)) {
if (i == 0)
strDesktopPath = Path;
else
strStartMenuPath = Path;
}
pShellMalloc->Free(pidl);
}
}
pShellMalloc->Release();
}
char szFileTitle[MAX_PATH] = { 0 };
::GetFileTitle(pszSrcPath, szFileTitle, MAX_PATH);
std::string str;
if (bDesktop)
str = strDesktopPath;
else
str = strStartMenuPath;
str += "//";
str += std::string(szFileTitle);
str += ".lnk";
HRESULT hr = CoCreateInstance(CLSID_ShellLink,
NULL,CLSCTX_INPROC_SERVER,
IID_IShellLink,
(LPVOID*)&psl);
if (SUCCEEDED(hr)) {
IPersistFile* ppf;
psl->SetPath(pszSrcPath);
psl->SetDescription("Shortcut created by custom code");
psl->SetShowCmd(SW_SHOW);
if (SUCCEEDED(psl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf))) {
WORD mbw[MAX_PATH];
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str.c_str(), -1, mbw, MAX_PATH);
if (SUCCEEDED(ppf->Save(mbw, TRUE)))
bRet = true;
ppf->Release();
}
psl->Release();
}
CoUninitialize();
return bRet;
}
//pazSrcPath:源文件路径
//bDesktop:标志位,用来判断是否在桌面上创建快捷方式,真,在桌面上创建,否则在开始菜单中创建
bool createShortcut(const char* pszSrcPath, bool bDesktop)
{
CoInitialize(NULL);
bool bRet = false;
IShellLink* psl;
LPITEMIDLIST pidl;
LPMALLOC pShellMalloc;
std::string strDesktopPath;
std::string strStartMenuPath;
const int nFolder[2] = { CSIDL_DESKTOPDIRECTORY,CSIDL_STARTMENU };
if (SUCCEEDED(SHGetMalloc(&pShellMalloc))) {
char Path[MAX_PATH + 1];
for (int i = 0; i < 2; i++) {
if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, nFolder[i], &pidl))) {
if (SHGetPathFromIDList(pidl, Path)) {
if (i == 0)
strDesktopPath = Path;
else
strStartMenuPath = Path;
}
pShellMalloc->Free(pidl);
}
}
pShellMalloc->Release();
}
char szFileTitle[MAX_PATH] = { 0 };
::GetFileTitle(pszSrcPath, szFileTitle, MAX_PATH);
std::string str;
if (bDesktop)
str = strDesktopPath;
else
str = strStartMenuPath;
str += "//";
str += std::string(szFileTitle);
str += ".lnk";
HRESULT hr = CoCreateInstance(CLSID_ShellLink,
NULL,CLSCTX_INPROC_SERVER,
IID_IShellLink,
(LPVOID*)&psl);
if (SUCCEEDED(hr)) {
IPersistFile* ppf;
psl->SetPath(pszSrcPath);
psl->SetDescription("Shortcut created by custom code");
psl->SetShowCmd(SW_SHOW);
if (SUCCEEDED(psl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf))) {
WORD mbw[MAX_PATH];
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str.c_str(), -1, mbw, MAX_PATH);
if (SUCCEEDED(ppf->Save(mbw, TRUE)))
bRet = true;
ppf->Release();
}
psl->Release();
}
CoUninitialize();
return bRet;
}
相关文章推荐
- 如何在桌面或者开始菜单中创建快捷方式
- 如何创建快捷方式并添加到开始菜单、桌面、任务栏....
- 一分钟学会创建开始菜单程序组和桌面快捷方式
- C#如何编程在桌面创建快捷方式
- Linux系统:Ubuntu下如何在桌面创建一个应用程序的“快捷方式”?
- Win8如何在桌面创建快捷方式(3种简单的方式)
- Asp.Net-创建网站的快捷方式到桌面,开始菜单,收藏夹
- 如何在Linux的桌面上创建快捷方式或启动器
- 如何在Linux的桌面上创建快捷方式或启动器
- Android_如何创建桌面快捷方式
- NSIS制作卸载器时注意事项和如何创建桌面快捷方式
- Qt之创建桌面和开始菜单快捷方式
- 创建快捷方式 (并添加到快速启动栏 、桌面、开始菜单)
- 如何在linux桌面上创建webstorm快捷方式
- 如何在Linux的桌面上创建快捷方式或启动器
- 如何在macos下创建文件或者文件夹的快捷方式
- 如何创建桌面快捷方式
- ubuntu 创建 PyCharm 桌面快捷方式 (或者叫 启动器 )
- Android_如何创建桌面快捷方式
- 如何用WinCE CabManager制作的cab文件,创建桌面快捷方式?