📄 jelinstaller.cpp
字号:
HRESULT JELShortcutInstaller::Uninstall(){ DeleteShortcut("\\jEdit.lnk", Desktop); DeleteShortcut("\\jEdit.lnk", StartMenu); int nResult = DeleteShortcut("\\jEdit\\jEdit.lnk", ProgramsMenu); nResult &= DeleteShortcut("\\jEdit\\Set jEdit parameters.lnk", ProgramsMenu); nResult &= DeleteShortcut("\\jEdit\\Uninstall jEdit.lnk", ProgramsMenu); if(nResult == 1) { ITEMIDLIST *id; char szLinkPath[MAX_PATH]; ZeroMemory(szLinkPath, sizeof(szLinkPath)); SHGetSpecialFolderLocation(NULL, ProgramsMenu, &id); SHGetPathFromIDList(id, szLinkPath); strcat(szLinkPath, "\\jEdit"); RemoveDirectory(szLinkPath); } return S_OK;}int JELShortcutInstaller::DeleteShortcut(const char* szName, LinkLocation location){ // delete shortcuts if resolves to files in current directory // ITEMIDLIST *id; char szLinkPath[MAX_PATH]; ZeroMemory(szLinkPath, sizeof(szLinkPath)); SHGetSpecialFolderLocation(NULL, location, &id); SHGetPathFromIDList(id, szLinkPath); strcat(szLinkPath, szName); if(-1 == GetFileAttributes(szLinkPath)) return 1; // does this resolve to current directory? IShellLink *psl; HRESULT hres; WIN32_FIND_DATA wfd; char szTargetPath[MAX_PATH]; IPersistFile *ppf; hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *)&psl); if (SUCCEEDED(hres)) { hres = psl->QueryInterface(__uuidof(IPersistFile), (LPVOID *)&ppf); if (SUCCEEDED(hres)) { WORD wsz[MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, szLinkPath, -1, wsz, MAX_PATH); hres = ppf->Load(wsz, STGM_READ); if (SUCCEEDED(hres)) { hres = psl->Resolve(0, SLR_NO_UI | SLR_NOSEARCH | SLR_NOTRACK | SLR_NOUPDATE); if (SUCCEEDED(hres)) { hres = psl->GetPath(szTargetPath, MAX_PATH, (WIN32_FIND_DATA *)&wfd, 0);//SLGP_SHORTPATH ); if(SUCCEEDED(hres)) { char szDir[MAX_PATH]; strcpy(szDir, szTargetPath); InstallData::ShortenToDirectory(szDir); if(strcmp(szDir, pData->strInstallDir) == 0) { hres = DeleteFile(szLinkPath) ? S_OK : E_FAIL; } } } else { hres = DeleteFile(szLinkPath) ? S_OK : E_FAIL; } } ppf->Release(); } psl->Release(); } return (hres == S_OK) ? 1 : 0;}HRESULT JELShortcutInstaller::SendMessage(LPVOID p){ p; return E_NOTIMPL;}/* Implementation of JELApplicationInstaller */JELApplicationInstaller::JELApplicationInstaller(const char* ptrJavaHome, const char* ptrInstallDir, BOOL bLeaveFiles) : pData(0), pFileInstaller(0), pRegistryInstaller(0), pShortcutInstaller(0), szJavaHome(ptrJavaHome), szInstallDir(ptrInstallDir), leaveFiles(bLeaveFiles), hrCOM(E_FAIL), pLog(0){ InstallerLog::Init();}JELApplicationInstaller::~JELApplicationInstaller(){ InstallerLog::Exit(); Cleanup();}bool JELApplicationInstaller::Init(){ Cleanup(); hrCOM = CoInitialize(0); if(FAILED(hrCOM)) { InstallerLog::Log(Error, "Could not initialize COM facilities.\n"); return false; } const char *pEmpty = _T(""); if(szJavaHome == 0) szJavaHome = pEmpty; pData = new InstallData(szJavaHome, szInstallDir); if(pData == 0 || !pData->Init()) { InstallerLog::Log(Error, "Could not initialize installation data.\n"); return false; } pFileInstaller = new JELFileInstaller(pData, this); pRegistryInstaller = new JELRegistryInstaller(pData, this); pShortcutInstaller = new JELShortcutInstaller(pData, this); return pFileInstaller != 0 && pRegistryInstaller != 0 && pShortcutInstaller != 0;}void JELApplicationInstaller::Cleanup(){ delete pData; delete pFileInstaller; delete pRegistryInstaller; delete pShortcutInstaller; pData = 0; pFileInstaller = 0; pRegistryInstaller = 0; pShortcutInstaller = 0; if(hrCOM == S_OK) { CoUninitialize(); hrCOM = E_FAIL; }}HRESULT JELApplicationInstaller::Install(){ InstallerLog::Log(Message, "Commencing install. . . .\n"); if(!Init()) return E_FAIL; HRESULT hrFile = pFileInstaller->Install(); if(hrFile == E_FAIL) return E_FAIL; HRESULT hr = pRegistryInstaller->Install(); if(hr == E_FAIL) { // recover by undoing PendingFileOperations return E_FAIL; } hr = pShortcutInstaller->Install(); if(hrFile == S_FALSE) { InstallerLog::Log(Message, "Reboot required to complete installation.\n"); QueryReboot(); } CString strMsg(_T("Installation of jEditLauncher ")); strMsg += hr == E_FAIL ? _T("failed.") : _T("was successful."); MessageBox(0, strMsg, "jEditLauncher", hr == E_FAIL ? MB_ICONERROR : MB_ICONINFORMATION); InstallerLog::Log(Message, "%s\n", (LPCTSTR)strMsg); return hr;}HRESULT JELApplicationInstaller::Uninstall(){ if(!Init()) return E_FAIL; CString strConfirm; // Note: resource file has commented version of query // for use when full uninstall is activated UINT nMsg = leaveFiles ? IDS_QUERY_UNREGISTER : IDS_QUERY_UNINSTALL_FULL; strConfirm.Format(nMsg, pData->strInstallVersion, pData->strInstallDir); if(IDOK != MessageBox(0, strConfirm, pData->strApp, MB_ICONQUESTION | MB_OKCANCEL | MB_DEFBUTTON2)) { MessageBox(0, "The uninstallation of jEditLauncher was cancelled.", "jEditLauncher", MB_ICONINFORMATION); return S_FALSE; } HRESULT hr = pRegistryInstaller->Uninstall(); hr |= pShortcutInstaller->Uninstall(); // File deletion is disabled when uninstall // is invoked in jedit.exe and // enabled when doing full uninstall with unlaunch.exe HRESULT hrFile = S_OK; if(!leaveFiles) hrFile = pFileInstaller->Uninstall(); if(hrFile == E_FAIL) return E_FAIL; if(hrFile == S_FALSE) { QueryReboot(); } CString strMsg(_T("The uninstallation of jEditLauncher ")); strMsg += hr == E_FAIL ? _T("failed.") : _T("was successful."); MessageBox(0, strMsg, "jEditLauncher", hr == E_FAIL ? MB_ICONERROR : MB_ICONINFORMATION); InstallerLog::Log(Message, "%s\n", strMsg); return hr;}HRESULT JELApplicationInstaller::SendMessage(LPVOID p){ return S_OK;}BOOL WINAPI ExitWindowsNT(DWORD dwOptions);BOOL WINAPI ExitWindows9x(DWORD dwOptions);#if !defined(EWX_FORCEIFHUNG)#define EWX_FORCEIFHUNG 0x00000010#endifvoid JELApplicationInstaller::QueryReboot(){ BOOL bRet = FALSE; if(IDYES == MessageBox(0, CString((LPCSTR)IDS_QUERY_REBOOT), pData->strApp, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON1)) { // reboot routine: if(pData->bIsWinNT) bRet = ExitWindowsNT(EWX_REBOOT | EWX_FORCEIFHUNG); else bRet = ExitWindows9x(EWX_REBOOT | EWX_FORCE); if(!bRet) { MessageBox(0, "The system could not reboot. Please do so as soon as possible.", "jEditLauncher", MB_ICONEXCLAMATION); } } else { MessageBox(0, "Please reboot to complete installation as soon as possible.", "jEditLauncher", MB_ICONEXCLAMATION); }}// signatures for NT functions used by ExitWindowsNT()typedef BOOL(WINAPI *OPTFunc)(HANDLE, DWORD, PHANDLE);typedef BOOL(WINAPI *LPVFunc)(LPCTSTR, LPCTSTR, PLUID);typedef BOOL(WINAPI *ATPFunc)(HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);BOOL WINAPI ExitWindowsNT(DWORD dwOptions){ // set shutdown privilege for current process HANDLE hToken; TOKEN_PRIVILEGES tkp; HINSTANCE hModule = ::LoadLibrary("advapi32.dll"); if(hModule == 0) return FALSE; OPTFunc optProc = (OPTFunc)GetProcAddress(hModule, "OpenProcessToken"); LPVFunc lpvProc = (LPVFunc)GetProcAddress(hModule, "LookupPrivilegeValueA"); ATPFunc atpProc = (ATPFunc)GetProcAddress(hModule, "AdjustTokenPrivileges"); DWORD dwError = GetLastError(); BOOL bRet = (optProc != 0) && (optProc)(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken) && (lpvProc != 0) && (atpProc != 0); if(!bRet) { ::FreeLibrary(hModule); return FALSE; } (lpvProc)(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // one privilege to set tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; (atpProc)(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); ::FreeLibrary(hModule); return GetLastError() == ERROR_SUCCESS && ExitWindowsEx(dwOptions, 0);}#include <Tlhelp32.h>// signatures for functions ised by ExitWindows9x()typedef HANDLE (WINAPI *CTHFunc)(DWORD, DWORD);typedef BOOL (WINAPI *PFFunc)(HANDLE, PPROCESSENTRY32);typedef BOOL (WINAPI *PNFunc)(HANDLE, PPROCESSENTRY32);BOOL WINAPI ExitWindows9x(DWORD dwOptions){ if (dwOptions & EWX_FORCE) { // Terminate all processes named "Explorer" before calling // ExitWindowEx() to prevent log-in window from appearing HMODULE hModule = LoadLibrary("kernel32.dll"); if(hModule == 0) return FALSE; CTHFunc ctsProc = (CTHFunc)GetProcAddress(hModule, "CreateToolhelp32Snapshot"); PFFunc pfProc = (PFFunc)GetProcAddress(hModule, "Process32First"); PNFunc pnProc = (PNFunc)GetProcAddress(hModule, "Process32Next"); if( ctsProc == 0 || pfProc == 0 || pnProc == 0) { ::FreeLibrary(hModule); return FALSE; } // ToolHelp snapshot of the system's processes. HANDLE hSnapShot = (ctsProc)(TH32CS_SNAPPROCESS, 0); if( hSnapShot == INVALID_HANDLE_VALUE ) { ::FreeLibrary(hModule); return FALSE; } // Get process information and terminate "explorer.exe" PROCESSENTRY32 processEntry; processEntry.dwSize = sizeof(PROCESSENTRY32); BOOL bFound = (pfProc)(hSnapShot, &processEntry); while(bFound) { HANDLE hProcess; int nPos = lstrlen(processEntry.szExeFile); if (nPos) { while (processEntry.szExeFile[--nPos] != '\\' ); if(!lstrcmpi("explorer.exe", &(processEntry.szExeFile[nPos+1]))) { // Terminate the process. hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processEntry.th32ProcessID); TerminateProcess(hProcess, 1); CloseHandle(hProcess); } } processEntry.dwSize = sizeof(PROCESSENTRY32) ; bFound = (pnProc)(hSnapShot, &processEntry); } CloseHandle(hSnapShot); FreeLibrary(hModule); } return ExitWindowsEx(dwOptions, 0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -