📄 bindinstalldlg.cpp
字号:
void CBINDInstallDlg::UnregisterService(BOOL uninstall) { BOOL rc = FALSE; SC_HANDLE hSCManager; SC_HANDLE hService; while(1) { SetCurrent(IDS_OPEN_SCM); hSCManager= OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (!hSCManager && uninstall == TRUE) { MsgBox(IDS_ERR_OPEN_SCM, GetErrMessage()); break; } SetCurrent(IDS_OPEN_SERVICE); hService = OpenService(hSCManager, BIND_SERVICE_NAME, STANDARD_RIGHTS_REQUIRED); if (!hService && uninstall == TRUE) { if (GetLastError() != ERROR_SERVICE_DOES_NOT_EXIST) { MsgBox(IDS_ERR_OPEN_SERVICE, GetErrMessage()); break; } } else { SetCurrent(IDS_REMOVE_SERVICE); if (!DeleteService(hService) && uninstall == TRUE) { DWORD err = GetLastError(); if (err != ERROR_SERVICE_MARKED_FOR_DELETE && err != ERROR_SERVICE_DOES_NOT_EXIST) { MsgBox(IDS_ERR_REMOVE_SERVICE, GetErrMessage()); break; } } } rc = TRUE; break; } if (hService) CloseServiceHandle(hService); if (hSCManager) CloseServiceHandle(hSCManager); if (uninstall) SetItemStatus(IDC_REG_SERVICE, rc);}void CBINDInstallDlg::RegisterMessages() { HKEY hKey; DWORD dwData; char pszMsgDLL[MAX_PATH], buf[MAX_PATH]; GetSystemDirectory(buf, MAX_PATH); sprintf(pszMsgDLL, "%s\\%s", buf, "bindevt.dll"); SetCurrent(IDS_REGISTER_MESSAGES); /* Create a new key for named */ if (RegCreateKey(HKEY_LOCAL_MACHINE, BIND_MESSAGE_SUBKEY, &hKey) != ERROR_SUCCESS) throw(Exception(IDS_ERR_CREATE_KEY, GetErrMessage())); /* Add the Event-ID message-file name to the subkey. */ if (RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ, (LPBYTE)pszMsgDLL, strlen(pszMsgDLL) + 1) != ERROR_SUCCESS) throw(Exception(IDS_ERR_SET_VALUE, GetErrMessage())); /* Set the supported types flags and addit to the subkey. */ dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE; if (RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (LPBYTE)&dwData, sizeof(DWORD)) != ERROR_SUCCESS) throw(Exception(IDS_ERR_SET_VALUE, GetErrMessage())); RegCloseKey(hKey); SetItemStatus(IDC_REG_MESSAGE);}void CBINDInstallDlg::UnregisterMessages(BOOL uninstall) { BOOL rc = FALSE; HKEY hKey = NULL; while(1) { SetCurrent(IDS_UNREGISTER_MESSAGES); /* Open key for Application Event Log */ if (RegOpenKey(HKEY_LOCAL_MACHINE, EVENTLOG_APP_SUBKEY, &hKey) != ERROR_SUCCESS) break; /* Remove named from the list of messages sources */ if (RegDeleteKey(hKey, BIND_MESSAGE_NAME) != ERROR_SUCCESS) break; rc = TRUE; break; } if (hKey) RegCloseKey(hKey); if (uninstall) SetItemStatus(IDC_REG_MESSAGE, rc);}/* * Install failed - clean up quietly */void CBINDInstallDlg::FailedInstall() { UnregisterMessages(FALSE); UnregisterService(FALSE); DeleteFiles(FALSE); RemoveDirs(FALSE);}/* * Set the checklist tags for install */void CBINDInstallDlg::InstallTags() { CString tag; tag.LoadString(IDS_INSTALL_FILE); GetDlgItem(IDC_COPY_TAG)->SetWindowText(tag); GetDlgItem(IDC_COPY_FILE)->SetWindowText(""); tag.LoadString(IDS_INSTALL_DIR); GetDlgItem(IDC_DIR_TAG)->SetWindowText(tag); GetDlgItem(IDC_CREATE_DIR)->SetWindowText(""); GetDlgItem(IDC_REG_SERVICE)->SetWindowText(""); tag.LoadString(IDS_INSTALL_SERVICE); GetDlgItem(IDC_SERVICE_TAG)->SetWindowText(tag); tag.LoadString(IDS_INSTALL_MESSAGE); GetDlgItem(IDC_MESSAGE_TAG)->SetWindowText(tag); GetDlgItem(IDC_REG_MESSAGE)->SetWindowText("");}/* * Set the checklist tags for uninstall */void CBINDInstallDlg::UninstallTags() { CString tag; tag.LoadString(IDS_UNINSTALL_FILES); GetDlgItem(IDC_COPY_TAG)->SetWindowText(tag); GetDlgItem(IDC_COPY_FILE)->SetWindowText(""); tag.LoadString(IDS_UNINSTALL_DIR); GetDlgItem(IDC_DIR_TAG)->SetWindowText(tag); GetDlgItem(IDC_CREATE_DIR)->SetWindowText(""); tag.LoadString(IDS_UNINSTALL_SERVICE); GetDlgItem(IDC_SERVICE_TAG)->SetWindowText(tag); GetDlgItem(IDC_REG_SERVICE)->SetWindowText(""); tag.LoadString(IDS_UNINSTALL_MESSAGE); GetDlgItem(IDC_MESSAGE_TAG)->SetWindowText(tag); GetDlgItem(IDC_REG_MESSAGE)->SetWindowText("");}void CBINDInstallDlg::SetItemStatus(UINT nID, BOOL bSuccess) { GetDlgItem(nID)->SetWindowText(bSuccess == TRUE ? "Done" : "Failed");}/* * Set the text in the current operation field - use a string table string */void CBINDInstallDlg::SetCurrent(int id, ...) { CString format; va_list va; char buf[128]; format.LoadString(id); memset(buf, 0, 128); va_start(va, id); vsprintf(buf, format, va); va_end(va); m_current.Format("%s", buf); UpdateData(FALSE);}/* * Stop the BIND service */void CBINDInstallDlg::StopBINDService() { SERVICE_STATUS svcStatus; SetCurrent(IDS_STOP_SERVICE); SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (!hSCManager) { MsgBox(IDS_ERR_OPEN_SCM, GetErrMessage()); } SC_HANDLE hBINDSvc = OpenService(hSCManager, BIND_SERVICE_NAME, SERVICE_ALL_ACCESS); if (!hBINDSvc) { MsgBox(IDS_ERR_OPEN_SERVICE, GetErrMessage()); } BOOL rc = ControlService(hBINDSvc, SERVICE_CONTROL_STOP, &svcStatus);}/* * Start the BIND service */void CBINDInstallDlg::StartBINDService() { SetCurrent(IDS_START_SERVICE); SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (!hSCManager) { MsgBox(IDS_ERR_OPEN_SCM, GetErrMessage()); } SC_HANDLE hBINDSvc = OpenService(hSCManager, BIND_SERVICE_NAME, SERVICE_ALL_ACCESS); if (!hBINDSvc) { MsgBox(IDS_ERR_OPEN_SERVICE, GetErrMessage()); } BOOL rc = StartService(hBINDSvc, 0, NULL);}/* * Check to see if the BIND service is running or not */BOOL CBINDInstallDlg::CheckBINDService() { SERVICE_STATUS svcStatus; SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hSCManager) { SC_HANDLE hBINDSvc = OpenService(hSCManager, BIND_SERVICE_NAME, SERVICE_ALL_ACCESS); if (hBINDSvc) { BOOL rc = ControlService(hBINDSvc, SERVICE_CONTROL_INTERROGATE, &svcStatus); if (!rc) DWORD err = GetLastError(); return (svcStatus.dwCurrentState == SERVICE_RUNNING); } } return (FALSE);}/* * Display message boxes with variable args, using string table strings * for the format specifiers */int CBINDInstallDlg::MsgBox(int id, ...) { CString format; va_list va; char buf[BUFSIZ]; format.LoadString(id); memset(buf, 0, BUFSIZ); va_start(va, id); vsprintf(buf, format, va); va_end(va); return (MessageBox(buf));}int CBINDInstallDlg::MsgBox(int id, UINT type, ...) { CString format; va_list va; char buf[BUFSIZ]; format.LoadString(id); memset(buf, 0, BUFSIZ); va_start(va, type); vsprintf(buf, format, va); va_end(va); return(MessageBox(buf, NULL, type));}/* * Call GetLastError(), retrieve the message associated with the error */CString CBINDInstallDlg::GetErrMessage(DWORD err) { LPVOID msgBuf; static char buf[BUFSIZ]; DWORD len = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err == -1 ? GetLastError() : err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &msgBuf, 0, NULL ); strcpy(buf, (LPTSTR)msgBuf); LocalFree(msgBuf); /* Strip off the period and the \n */ buf[len - 3] = 0; return(buf);}void CBINDInstallDlg::ProgramGroup(BOOL create) { TCHAR path[MAX_PATH], commonPath[MAX_PATH], fileloc[MAX_PATH], linkpath[MAX_PATH]; HRESULT hres; IShellLink *psl = NULL; LPMALLOC pMalloc = NULL; ITEMIDLIST *itemList = NULL; HRESULT hr = SHGetMalloc(&pMalloc); if (hr != NOERROR) { MessageBox("Could not get a handle to Shell memory object"); return; } hr = SHGetSpecialFolderLocation(m_hWnd, CSIDL_COMMON_PROGRAMS, &itemList); if (hr != NOERROR) { MessageBox("Could not get a handle to the Common Programs folder"); if (itemList) { pMalloc->Free(itemList); } return; } hr = SHGetPathFromIDList(itemList, commonPath); pMalloc->Free(itemList); if (create) { sprintf(path, "%s\\ISC", commonPath); CreateDirectory(path, NULL); sprintf(path, "%s\\ISC\\BIND", commonPath); CreateDirectory(path, NULL); hres = CoInitialize(NULL); if (SUCCEEDED(hres)) { // Get a pointer to the IShellLink interface. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *)&psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; sprintf(linkpath, "%s\\BINDCtrl.lnk", path); sprintf(fileloc, "%s\\BINDCtrl.exe", m_binDir); psl->SetPath(fileloc); psl->SetDescription("BIND Control Panel"); hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf); if (SUCCEEDED(hres)) { WCHAR wsz[MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, linkpath, -1, wsz, MAX_PATH); hres = ppf->Save(wsz, TRUE); ppf->Release(); } if (GetFileAttributes("readme.txt") != -1) { sprintf(fileloc, "%s\\Readme.txt", m_targetDir); sprintf(linkpath, "%s\\Readme.lnk", path); psl->SetPath(fileloc); psl->SetDescription("BIND Readme"); hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf); if (SUCCEEDED(hres)) { WCHAR wsz[MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, linkpath, -1, wsz, MAX_PATH); hres = ppf->Save(wsz, TRUE); ppf->Release(); } psl->Release(); } } CoUninitialize(); } } else { TCHAR filename[MAX_PATH]; WIN32_FIND_DATA fd; sprintf(path, "%s\\ISC\\BIND", commonPath); sprintf(filename, "%s\\*.*", path); HANDLE hFind = FindFirstFile(filename, &fd); if (hFind != INVALID_HANDLE_VALUE) { do { if (strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..")) { sprintf(filename, "%s\\%s", path, fd.cFileName); DeleteFile(filename); } } while (FindNextFile(hFind, &fd)); FindClose(hFind); } RemoveDirectory(path); sprintf(path, "%s\\ISC", commonPath); RemoveDirectory(path); }}CString CBINDInstallDlg::DestDir(int destination) { switch(destination) { case FileData::TargetDir: return m_targetDir; case FileData::BinDir: return m_binDir; case FileData::EtcDir: return m_etcDir; case FileData::WinSystem: return m_winSysDir; } return("");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -