1. 程式人生 > 其它 >mfc 建立桌面快捷方式&刪除

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;
}