📄 mpdupdatedlg.cpp
字号:
WriteString(sock, "done"); easy_closesocket(sock); if (!pDlg->m_bForceUpdate) { version_old = mpd_version_string_to_int(str); mpd_version_int_to_string(version_old, str); mpd_version_int_to_string(version_new, str2); } if (pDlg->m_bForceUpdate || version_old < version_new) { if (UpdateMPICH(host, pDlg->m_cred_account, pDlg->m_cred_password, pDlg->m_mpd_port, pDlg->m_pszPhrase, bDeleteTmpMPICH ? pDlg->m_mpich_localfile : pDlg->m_mpich_filename, bDeleteTmpMPICH ? pDlg->m_mpich_localfiled : pDlg->m_mpich_filenamed, pszError, 256)) { results.Format("%s: success\r\n", host); pDlg->m_results += results; PostMessage(pDlg->m_hWnd, WM_USER + 5, 0, 0); } else { bFailure = true; results.Format("%s: mpich failure, %s\r\n", host, pszError); pDlg->m_results += results; PostMessage(pDlg->m_hWnd, WM_USER + 5, 0, 0); //MessageBox(NULL, pszError, "Unable to update mpd", MB_OK); } } else { CString s; if (version_old == version_new) { s.Format("MPICH version <%s> is already installed on %s", str, host); results.Format("%s: no action, mpich <%s> is already installed\r\n", host, str); pDlg->m_results += results; PostMessage(pDlg->m_hWnd, WM_USER + 5, 0, 0); //MessageBox(NULL, s, "Update aborted", MB_OK); } else { s.Format("MPICH version <%s> on %s is newer than version <%s>", str, host, str2); results.Format("%s: no action, mpich <%s> is newer than <%s>\r\n", host, str, str2); pDlg->m_results += results; PostMessage(pDlg->m_hWnd, WM_USER + 5, 0, 0); //MessageBox(NULL, s, "Update aborted", MB_OK); } } } else { CString str; bFailure = true; str.Format("Connect to mpd on %s failed", host); results.Format("%s: failure, connect to mpd failed\r\n", host); pDlg->m_results += results; PostMessage(pDlg->m_hWnd, WM_USER + 5, 0, 0); //MessageBox(NULL, str, "Unable to update mpich", MB_OK); } } PostMessage(pDlg->m_hWnd, WM_USER + 4, 0, 0); // re-enable the buttons if (bDeleteTmpMPICH) { DeleteFile(pDlg->m_mpich_localfile); DeleteFile(pDlg->m_mpich_localfiled); } if (pDlg->m_bShowHostConfig) { PostMessage(pDlg->m_hWnd, WM_USER+2, 0, 0); } } CloseHandle(pDlg->m_hUpdateBtnThread); pDlg->m_hUpdateBtnThread = NULL;}void CMPDUpdateDlg::OnUpdateBtn(){ DWORD dwThreadID; UpdateData(); m_results = ""; UpdateData(FALSE); if (m_host_list.GetCount() < 1) { MessageBox("Please add hosts to the list before selecting Update"); return; } if (!m_bUpdateMPD && !m_bUpdateMPICH) { MessageBox("Please check at least one of the update boxes before selecting Update"); return; } if (m_bNeedPassword) { if (m_bUseDefault) strcpy(m_pszPhrase, MPD_DEFAULT_PASSPHRASE); else strcpy(m_pszPhrase, m_mpd_pwd); } if (m_cred_account.GetLength() < 1) { MessageBox("Please enter the account information before selecting Update"); m_cred_account_edit.SetFocus(); return; } m_hUpdateBtnThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)UpdateBtnThread, this, 0, &dwThreadID);}#define BUFSIZE 1024*1024bool CMPDUpdateDlg::GetTmpMpdFromURL(){ char *buffer; DWORD num_read; char path[MAX_PATH]; CInternetSession session("MPDUpdate"); CStdioFile *fin, fout; CString str; if (GetTempPath(MAX_PATH, path)) { if (GetTempFileName(path, "mpd", 0, m_localfile.GetBuffer(MAX_PATH))) { m_localfile.ReleaseBuffer(); buffer = new char[BUFSIZE]; // open session fin = NULL; try { fin = session.OpenURL(m_urlname, 1, INTERNET_FLAG_TRANSFER_BINARY); } catch(...) { if (fin == NULL) { str.Format("OpenURL(%s) failed, error %d", m_urlname, GetLastError()); MessageBox(str, "Error"); delete buffer; return false; } str.Format("OpenURL(%s) failed", m_urlname); MessageBox(str, "Error"); delete buffer; return false; } // open local file if (!fout.Open(m_localfile, CFile::modeWrite | CFile::typeBinary)) { str.Format("Open(%s) failed, error %d", m_localfile, GetLastError()); fin->Close(); MessageBox(str, "Error"); delete buffer; return false; } while (true) { num_read = fin->Read(buffer, BUFSIZE); if (num_read > 0) { try { fout.Write(buffer, num_read); } catch(...) { str.Format("Write failed, error %d", m_localfile, GetLastError()); fin->Close(); MessageBox(str, "Error"); delete buffer; return false; } } else { if (num_read == 0) { fin->Close(); fout.Close(); delete buffer; //MessageBox(m_localfile, "File retrieved"); return true; } str.Format("Read failed, error %d", GetLastError()); MessageBox(str, "Error"); fin->Close(); CString fname = fout.GetFilePath(); fout.Close(); try { CFile::Remove(fname); } catch(...) {} delete buffer; return false; } } } str.Format("GetTempFileName failed, error %d", GetLastError()); MessageBox(str, "Error"); return false; } str.Format("GetTempPath failed, error %d", GetLastError()); MessageBox(str, "Error"); return false;}bool CMPDUpdateDlg::GetTmpMPICHFromURL(){ char *buffer; DWORD num_read; char path[MAX_PATH]; CInternetSession session("MPDUpdate"); CStdioFile *fin, fout; CString str; // Create two urls for mpich.dll and mpichd.dll CString str_url, str_urld; str_url = m_mpich_url; if (str_url.Find("mpich.dll") == -1) { str_url.TrimRight("/\\"); str_urld = str_url + "/mpichd.dll"; str_url += "/mpich.dll"; } else { str_urld = str_url; str_urld.Replace("mpich.dll", NULL); str_urld.TrimRight(); str_urld.TrimRight("/\\"); str_urld += "/mpichd.dll"; } // Get mpich.dll if (GetTempPath(MAX_PATH, path)) { if (GetTempFileName(path, "mpich", 0, m_mpich_localfile.GetBuffer(MAX_PATH))) { m_mpich_localfile.ReleaseBuffer(); buffer = new char[BUFSIZE]; // open session fin = NULL; try { fin = session.OpenURL(str_url, 1, INTERNET_FLAG_TRANSFER_BINARY); } catch(...) { if (fin == NULL) { str.Format("OpenURL(%s) failed, error %d", str_url, GetLastError()); MessageBox(str, "Error"); delete buffer; return false; } str.Format("OpenURL(%s) failed", str_url); MessageBox(str, "Error"); delete buffer; return false; } // open local file if (!fout.Open(m_mpich_localfile, CFile::modeWrite | CFile::typeBinary)) { str.Format("Open(%s) failed, error %d", m_mpich_localfile, GetLastError()); fin->Close(); MessageBox(str, "Error"); delete buffer; return false; } while (true) { num_read = fin->Read(buffer, BUFSIZE); if (num_read > 0) { try { fout.Write(buffer, num_read); } catch(...) { str.Format("Write failed, error %d", m_mpich_localfile, GetLastError()); fin->Close(); MessageBox(str, "Error"); delete buffer; return false; } } else { if (num_read == 0) { fin->Close(); fout.Close(); delete buffer; //MessageBox(m_localfile, "File retrieved"); //return true; break; } str.Format("Read failed, error %d", GetLastError()); MessageBox(str, "Error"); fin->Close(); CString fname = fout.GetFilePath(); fout.Close(); try { CFile::Remove(fname); } catch(...) {} delete buffer; return false; } } } else { str.Format("GetTempFileName failed, error %d", GetLastError()); MessageBox(str, "Error"); return false; } } else { str.Format("GetTempPath failed, error %d", GetLastError()); MessageBox(str, "Error"); return false; } // Get mpichd.dll if (GetTempPath(MAX_PATH, path)) { if (GetTempFileName(path, "mpich", 0, m_mpich_localfiled.GetBuffer(MAX_PATH))) { m_mpich_localfiled.ReleaseBuffer(); buffer = new char[BUFSIZE]; // open session fin = NULL; try { fin = session.OpenURL(str_urld, 1, INTERNET_FLAG_TRANSFER_BINARY); } catch(...) { if (fin == NULL) { str.Format("OpenURL(%s) failed, error %d", str_urld, GetLastError()); MessageBox(str, "Error"); delete buffer; return false; } str.Format("OpenURL(%s) failed", str_urld); MessageBox(str, "Error"); delete buffer; return false; } // open local file if (!fout.Open(m_mpich_localfiled, CFile::modeWrite | CFile::typeBinary)) { str.Format("Open(%s) failed, error %d", m_mpich_localfiled, GetLastError()); fin->Close(); MessageBox(str, "Error"); delete buffer; return false; } while (true) { num_read = fin->Read(buffer, BUFSIZE); if (num_read > 0) { try { fout.Write(buffer, num_read); } catch(...) { str.Format("Write failed, error %d", m_mpich_localfiled, GetLastError()); fin->Close(); MessageBox(str, "Error"); delete buffer; return false; } } else { if (num_read == 0) { fin->Close(); fout.Close(); delete buffer; //MessageBox(m_localfile, "File retrieved"); return true; } str.Format("Read failed, error %d", GetLastError()); MessageBox(str, "Error"); fin->Close(); CString fname = fout.GetFilePath(); fout.Close(); try { CFile::Remove(fname); } catch(...) {} delete buffer; return false; } } } str.Format("GetTempFileName failed, error %d", GetLastError()); MessageBox(str, "Error"); return false; } str.Format("GetTempPath failed, error %d", GetLastError()); MessageBox(str, "Error"); return false;}/*void CMPDUpdateDlg::GetTmpMpdFromFtp(){ char path[MAX_PATH]; CInternetSession session; CFtpSession *pFtp; if (GetTempPath(MAX_PATH, path)) { if (GetTempFileName(path, "mpd", 0, m_localfile.GetBuffer(MAX_PATH))) { m_localfile.ReleaseBuffer(); try { pFtp = session.GetFtpConnection("ftp.mcs.anl.gov"); pFtp->GetFile("/pub/mpi/nt/binaries/mpd.exe"); MessageBox(m_localfile, "File retrieved"); } catch (...) } }}*/bool GetLocalMPICHVersion(const char *filename, unsigned int &version){ void (*pGetMPICHVersion)(char *str, int length); HMODULE hModule; char err_str[256]; char err_msg[1024]; char version_str[100]; hModule = LoadLibrary(filename); if (hModule == NULL) { Translate_Error(GetLastError(), err_str, NULL); sprintf(err_msg, "LoadLibrary(%s) failed, %s\n", filename, err_str); MessageBox(NULL, err_msg, "Error: GetMPICHVersion failed", MB_OK); return false; } pGetMPICHVersion = (void (*)(char *, int))GetProcAddress(hModule, "GetMPICHVersion"); if (pGetMPICHVersion == NULL) { int error = GetLastError(); FreeLibrary(hModule); if (error == ERROR_PROC_NOT_FOUND) { version = 0; return true; } Translate_Error(error, err_msg, "GetProcAddress(\"GetMPICHVersion\") failed, "); MessageBox(NULL, err_msg, "Error: GetMPICHVersion failed", MB_OK); return false; } pGetMPICHVersion(version_str, 100); version = mpd_version_string_to_int(version_str); FreeLibrary(hModule); return true;}bool GetLocalVersionFromRun(const char *filename, unsigned int &version)//bool GetLocalVersion(const char *filename, unsigned int &version){ char line[1024]; HANDLE hIn, hOut, hErr; char err_str[256]; char err_msg[256]; int error; int pid; DWORD num_read; HANDLE hProcess; if (strlen(filename) > 1023) return false; sprintf(line, "%s -version", filename); hProcess = LaunchProcess(line, NULL, NULL, &hIn, &hOut, &hErr, &pid, &error, err_str); if (hProcess == INVALID_HANDLE_VALUE) { sprintf(err_msg, "%s%s", err_str, strerror(error)); MessageBox(NULL, err_msg, "Unable to launch the new mpd", MB_OK); return false; } line[0] = '\0'; while (true) { if (!ReadFile(hErr, line, 1024, &num_read, NULL)) { sprintf(err_str, "error %d", GetLastError());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -