mfc 创建桌面快捷方式 控制面板卸载程序

/// <summary>
/// 删除快捷方式
/// </summary>
/// <param name="strName"></param>
/// <returns></returns>
BOOL DeleteDesktopShotCut(CString strName) {
	char Path[MAX_PATH + 1];
	CString strDestDir;
	int i = CSIDL_DESKTOPDIRECTORY;
	LPITEMIDLIST pidl;
	LPMALLOC pShell;
	if (SUCCEEDED(SHGetMalloc(&pShell)))
	{
		if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, i, &pidl)))
		{
			if (!SHGetPathFromIDList(pidl, Path))
			{
				pShell->Free(pidl);
				::CoUninitialize();
				return FALSE;
			}
			pShell->Release();
			strDestDir.Format("%s", Path);
			strDestDir += "\\";
			strDestDir += strName;//设置桌面快捷方式的名字
			strDestDir += ".lnk";

			DeleteFile(strDestDir);
		}
	}
}

BOOL CreateUninstall(string displayName, string uninstallExePath, string modifyPath,
	string displayVersion,
	string icoPath, string installPath, string publisher, string company, string helpLink,
	string helpTelephone, string urlInfoAbout)
{
	bool result = true;
	HKEY hKey = nullptr;
	string key = GetUninstallKey() + displayName;
	//string key = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + displayName;
	//string key = "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Test";
	if (ERROR_SUCCESS != RegCreateKey(HKEY_LOCAL_MACHINE, key.c_str(), &hKey))
	{
		return false;
	}

	//先创建可选项(不影响必选项创建结果)
	result = (ERROR_SUCCESS == RegWriteString(hKey, "DisplayVersion", displayVersion));
	result = (ERROR_SUCCESS == RegWriteString(hKey, "DisplayIcon", icoPath));
	result = (ERROR_SUCCESS == RegWriteString(hKey, "Publisher", publisher));
	result = (ERROR_SUCCESS == RegWriteString(hKey, "RegCompany", company));
	result = (ERROR_SUCCESS == RegWriteString(hKey, "HelpLink", helpLink));
	result = (ERROR_SUCCESS == RegWriteString(hKey, "HelpTelephone", helpTelephone));
	result = (ERROR_SUCCESS == RegWriteString(hKey, "URLInfoAbout", urlInfoAbout));
	result = (ERROR_SUCCESS == RegWriteString(hKey, "InstallLocation", installPath));
	/*if (modifyPath != "") {
		string modifyStr = modifyPath + " " + CONST_UPGRADE;

		result = (ERROR_SUCCESS == RegWriteString(hKey, "ModifyPath", modifyStr));
	}*/


	//必选项
	result = (ERROR_SUCCESS == RegWriteString(hKey, "DisplayName", displayName, false));

	if (uninstallExePath != "") {
		string uninstallStr = uninstallExePath + " " + CONST_UNINSTALL;
		result = (ERROR_SUCCESS == RegWriteString(hKey, "UninstallString", uninstallStr, false));
	}



	//EstimatedSize
	RegCloseKey(hKey);

	return result;
}

创建快捷方式

BOOL SystemUtil::CreateShotCut(CString lnkPath, CString strSourcePath, CString argv)
{
	if (FAILED(CoInitialize(NULL)))
	{
		return FALSE;
	}
	int i;
	char Path[MAX_PATH + 1];
	i = CSIDL_DESKTOPDIRECTORY;
	LPITEMIDLIST pidl;
	LPMALLOC pShell;
	if (SUCCEEDED(SHGetMalloc(&pShell)))
	{
		if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, i, &pidl)))
		{
			if (!SHGetPathFromIDList(pidl, Path))
			{
				pShell->Free(pidl);
				::CoUninitialize();
				return FALSE;
			}
			pShell->Release();
			IShellLink* psl;
			if (SUCCEEDED(CoCreateInstance
			(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl)))
			{
				Log::D(("创建快捷方式,目标路径:" + strSourcePath).GetString());
				psl->SetArguments(argv.GetString());
				psl->SetPath(strSourcePath);//设置快捷方式的目标位置
//比如目标位置为C:\windows\a.txt 起始位置就应该设置为C:\windows否则会导致不可预料的错误
//如果是文件夹的快捷方式起始位置和目标位置可以设置为一样
				psl->SetWorkingDirectory(FileUtil::GetFileDir(strSourcePath.GetString()).c_str());   //设置快捷方式的起始位置
				IPersistFile* ppf;
				if (SUCCEEDED(psl->QueryInterface(IID_IPersistFile, (LPVOID*)
					&ppf)))
				{
					WCHAR wsz[MAX_PATH];
					MultiByteToWideChar
					(CP_THREAD_ACP, MB_PRECOMPOSED, lnkPath, -1, wsz, MAX_PATH);//设置桌面快捷方式的名字
					SUCCEEDED(ppf->Save(wsz, TRUE));//保存快捷方式到桌面

				}
				ppf->Release();
				psl->Release();
			}
		}
	}
	::CoUninitialize();
	return FALSE;
}
posted @ 2021-06-18 16:40  Hey,Coder!  阅读(133)  评论(0编辑  收藏  举报