创建快捷方式到桌面、任务栏、开始菜单、快速启动栏、程序组中

//得到快速启动栏的路径    
BOOL GetIEQuickLaunchPath(WCHAR *pszIEQueickLaunchPath)
{
	LPITEMIDLIST  ppidl;

	if (SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &ppidl) == S_OK)
	{
		BOOL flag = SHGetPathFromIDList(ppidl, pszIEQueickLaunchPath);
		wcscat(pszIEQueickLaunchPath, L"\\Microsoft\\Internet Explorer\\Quick Launch");
		CoTaskMemFree(ppidl);
		return flag;
	}
	return FALSE;
}

//得到 开始->程序组 的路径    
BOOL GetProgramsPath(WCHAR *pszProgramsPath)
{
	LPITEMIDLIST  ppidl;

	if (SHGetSpecialFolderLocation(NULL, CSIDL_STARTMENU, &ppidl) == S_OK)
	{
		BOOL flag = SHGetPathFromIDList(ppidl, pszProgramsPath);
		CoTaskMemFree(ppidl);
		return flag;
	}
	return FALSE;
}

//得到当前桌面路径    
BOOL GetDesktopPath(WCHAR *pszDesktopPath)
{
	LPITEMIDLIST  ppidl = NULL;

	if (SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &ppidl) == S_OK)
	{
		BOOL flag = SHGetPathFromIDList(ppidl, pszDesktopPath);
		CoTaskMemFree(ppidl);
		return flag;
	}
	return FALSE;
}

bool CreateShortcut(const WCHAR* pszSrcPath)
{
	CoInitialize(NULL);

	bool bRet = false;

	WCHAR chQuick[MAX_PATH] = { 0 };
	WCHAR chPrograms[MAX_PATH] = { 0 };
	WCHAR chDesktop[MAX_PATH] = { 0 };
	GetDesktopPath(chDesktop);
	GetIEQuickLaunchPath(chQuick);
	GetProgramsPath(chPrograms);

	std::wstring str;
	str = chQuick;

	WCHAR szFileTitle[MAX_PATH] = { 0 };
	::GetFileTitle(pszSrcPath, szFileTitle, MAX_PATH);

	str += L"//";
	str += std::wstring(szFileTitle);
	str += L".lnk";

	IShellLink* psl;
	HRESULT hr = CoCreateInstance(CLSID_ShellLink,NULL, CLSCTX_INPROC_SERVER,IID_IShellLink,(LPVOID*)&psl);
	if (SUCCEEDED(hr)) {
		IPersistFile* ppf;
		psl->SetPath(pszSrcPath);
		psl->SetDescription(L"测试描述");
		psl->SetShowCmd(SW_SHOW);
		if (SUCCEEDED(psl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf))) {
			if (SUCCEEDED(ppf->Save(str.c_str(), TRUE)))
				bRet = true;
			ppf->Release();
		}
		psl->Release();
	}

	CoUninitialize();


	::ShellExecute(NULL, TEXT("StartPin"), str.c_str(), NULL, NULL, SW_SHOW);//锁定到开始菜单
	::ShellExecute(NULL, L"TaskbarPin", str.c_str(), NULL, NULL, SW_SHOW);//锁定到任务栏
	return bRet;
}

    原文作者:奈何小洪
    原文地址: https://blog.csdn.net/qingzai_/article/details/78688161
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞