DVLib::FileVersionInfo DVLib::GetFileVersionInfo(const std::wstring& filename) { DVLib::FileVersionInfo result = { 0 }; DWORD dwVerHnd = 0; DWORD dwVerInfoSize = ::GetFileVersionInfoSize(filename.c_str(), & dwVerHnd); CHECK_WIN32_BOOL(dwVerInfoSize != 0, L"GetFileVersionInfoSize(" << filename << L")"); std::vector<byte> versioninfo_data; versioninfo_data.resize(dwVerInfoSize); dwVerInfoSize = versioninfo_data.size(); CHECK_WIN32_BOOL(::GetFileVersionInfo(filename.c_str(), dwVerHnd, dwVerInfoSize, & * versioninfo_data.begin()), L"GetFileVersionInfo(" << filename << L")"); // VS_FIXEDFILEINFO UINT fixed_len = 0; VS_FIXEDFILEINFO * lpvi = NULL; CHECK_WIN32_BOOL(0 != ::VerQueryValueW(& * versioninfo_data.begin(), L"\\", reinterpret_cast<LPVOID *>(& lpvi), & fixed_len), L"VerQueryValue(" << filename << L")"); result.fixed_info = * lpvi; // translation info UINT translation_len = 0; TranslationInfo * lpti = NULL; CHECK_WIN32_BOOL(0 != ::VerQueryValueW(& * versioninfo_data.begin(), L"\\VarFileInfo\\Translation", reinterpret_cast<LPVOID *>(& lpti), & translation_len), L"VerQueryValue(" << filename << L", \"\\VarFileInfo\\Translation\")"); CHECK_BOOL(translation_len <= sizeof(TranslationInfo), L"VerQueryValue(" << filename << L", \"\\VarFileInfo\\Translation\"): invalid size"); result.translation_info = * lpti; return result; }
DWORD DVLib::ExecCmd(const std::wstring& cmd, const std::wstring& working_directory, int nShow) { PROCESS_INFORMATION pi = { 0 }; RunCmd(cmd, & pi, 0, working_directory, nShow); auto_handle pi_thread(pi.hThread); auto_handle pi_process(pi.hProcess); CHECK_WIN32_BOOL(WAIT_OBJECT_0 == WaitForSingleObject(pi.hProcess, INFINITE), L"WaitForSingleObject"); DWORD dwExitCode = 0; CHECK_WIN32_BOOL(::GetExitCodeProcess(pi.hProcess, & dwExitCode), L"GetExitCodeProcess"); return dwExitCode; }
std::wstring DVLib::GetTemporaryDirectoryW() { wchar_t td[MAX_PATH] = { 0 }; CHECK_WIN32_BOOL(::GetTempPathW(MAX_PATH, td), "GetTempPathW"); return td; }
std::string DVLib::GetTemporaryDirectoryA() { char td[MAX_PATH] = { 0 }; CHECK_WIN32_BOOL(GetTempPathA(MAX_PATH, td), L"GetTempPathA"); return td; }
std::wstring DVLib::GetModuleFileNameW(HINSTANCE h) { wchar_t moduleFileName[MAX_PATH] = { 0 }; CHECK_WIN32_BOOL(::GetModuleFileNameW(h, moduleFileName, MAX_PATH), L"GetModuleFileNameW"); return moduleFileName; }
std::string DVLib::GetModuleFileNameA(HINSTANCE h) { char moduleFileName[MAX_PATH] = { 0 }; CHECK_WIN32_BOOL(::GetModuleFileNameA(h, moduleFileName, MAX_PATH), L"GetModuleFileNameA"); return moduleFileName; }
std::wstring DVLib::GetSystemWindowsDirectoryW() { wchar_t td[MAX_PATH] = { 0 }; CHECK_WIN32_BOOL(::GetSystemWindowsDirectoryW(td, MAX_PATH), L"GetSystemWindowsDirectoryW"); return td; }
std::string DVLib::GetWindowsDirectoryA() { char td[MAX_PATH] = { 0 }; CHECK_WIN32_BOOL(::GetWindowsDirectoryA(td, MAX_PATH), L"GetWindowsDirectoryA"); return td; }
std::string DVLib::GetSystemDirectoryA() { char td[MAX_PATH]; CHECK_WIN32_BOOL(::GetSystemDirectoryA(td, MAX_PATH), L"GetSystemDirectoryA"); return td; }
std::wstring DVLib::GetTemporaryFileNameW() { wchar_t tf[MAX_PATH]; CHECK_WIN32_BOOL(GetTempFileNameW(GetTemporaryDirectoryW().c_str(), L"DV", 0, tf), L"GetTempFileNameW"); return tf; }
std::string DVLib::GetCurrentDirectoryA() { char result[MAX_PATH] = { 0 }; CHECK_WIN32_BOOL(0 < ::GetCurrentDirectoryA(MAX_PATH, result), L"Error in GetCurrentDirectoryA"); return result; }
void ProcessComponent::Wait(DWORD tt) { CHECK_BOOL(m_process_handle != NULL, L"Invalid process handle") CHECK_WIN32_BOOL(WAIT_OBJECT_0 == WaitForSingleObject(m_process_handle, tt), L"WaitForSingleObject"); }
std::wstring DVLib::GetCurrentDirectoryW() { wchar_t result[MAX_PATH]; CHECK_WIN32_BOOL(0 < ::GetCurrentDirectoryW(MAX_PATH, result), L"Error in GetCurrentDirectoryW"); return result; }
std::string DVLib::GetTemporaryFileNameA() { char tf[MAX_PATH]; CHECK_WIN32_BOOL(GetTempFileNameA(GetTemporaryDirectoryA().c_str(), "DV", 0, tf), L"GetTempFileNameA"); return tf; }
void ConfigFileManager::CreateInstallerWindow(const std::wstring& title) { if (m_pInstallerWindow != NULL) { delete m_pInstallerWindow; m_pInstallerWindow = NULL; } MONITORINFO mi = { 0 }; mi.cbSize = sizeof(mi); CHECK_WIN32_BOOL(GetMonitorInfo(MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY), & mi), L"GetMonitorInfo"); int cx = 640; int cy = 480; m_pInstallerWindow = new InstallerWindow(); // deletes itself m_pInstallerWindow->Create( (mi.rcWork.left + mi.rcWork.right) / 2 - cx / 2, (mi.rcWork.top + mi.rcWork.bottom) / 2 - cy / 2, cx, cy, title.c_str()); CHECK_BOOL(m_pInstallerWindow->hwnd != NULL, L"InstallerWindow::Create"); }
void ConfigFileManager::CreateDownloadWindow(const DownloadDialogPtr& dialog) { if (m_pDownloadWindow != NULL) { delete m_pDownloadWindow; m_pDownloadWindow = NULL; } MONITORINFO mi = { 0 }; mi.cbSize = sizeof(mi); CHECK_WIN32_BOOL(GetMonitorInfo(MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY), & mi), L"GetMonitorInfo"); int cx = 550; int cy = 250; m_pDownloadWindow = new DownloadWindow(dialog); // deletes itself m_pDownloadWindow->Create( (mi.rcWork.left + mi.rcWork.right) / 2 - cx / 2, (mi.rcWork.top + mi.rcWork.bottom) / 2 - cy / 2, cx, cy, dialog->caption.GetValue().c_str()); CHECK_BOOL(m_pDownloadWindow->hwnd != NULL, L"DownloadWindow::Create"); }
void DVLib::FileMove(const std::wstring& from, const std::wstring& to) { if (FileExists(to)) FileDelete(to); CHECK_WIN32_BOOL(::MoveFileW(from.c_str(), to.c_str()), L"Error moving \"" << from << L"\" to \"" << to << L"\""); }
long DVLib::GetFileSize(const std::wstring& filename) { WIN32_FILE_ATTRIBUTE_DATA attr = { 0 }; CHECK_WIN32_BOOL(GetFileAttributesExW(filename.c_str(), GetFileExInfoStandard, & attr), L"Error getting file attributes of " << filename); CHECK_BOOL(0 == attr.nFileSizeHigh, L"File " << filename << L" is > 2GB (" << attr.nFileSizeHigh << ")"); return (long) attr.nFileSizeLow; }
DWORD ProcessComponent::GetProcessExitCode() const { DWORD l_ExitCode = 0xFFFFFF; if (m_process_handle != NULL) { CHECK_WIN32_BOOL(::GetExitCodeProcess(m_process_handle, & l_ExitCode), L"GetExitCodeProcess"); } return l_ExitCode; }
void DVLib::FileCreate( const std::wstring& filename, DWORD dwDesiredAccess, // GENERIC_READ | GENERIC_WRITE DWORD dwShareMode, // 0 DWORD dwCreationDisposition, // CREATE_ALWAYS DWORD dwFlagsAndAttributes) // FILE_ATTRIBUTE_NORMAL) { auto_hfile hFile(::CreateFile(filename.c_str(), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL)); CHECK_WIN32_BOOL(get(hFile) != NULL, L"Error opening \"" << filename << L"\""); }
std::vector<char> DVLib::FileReadToEnd(const std::wstring& filename) { std::vector<char> data; long size = GetFileSize(filename); if (size > 0) { auto_hfile hFile(::CreateFile(filename.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); CHECK_WIN32_BOOL(get(hFile) != NULL, L"Error opening \"" << filename << L"\""); data.resize(size); DWORD dwRead = 0; CHECK_WIN32_BOOL(::ReadFile(get(hFile), & * data.begin(), size, & dwRead, NULL), L"Error reading \"" << filename << L"\""); CHECK_BOOL(static_cast<long>(dwRead) == size, L"Invalid number of bytes read from \"" << filename << L"\""); } return data; }
void DVLib::FileWrite( const std::wstring& filename, const std::vector<char>& data, DWORD dwShareMode, // GENERIC_READ | GENERIC_WRITE DWORD dwCreationDisposition, // CREATE_ALWAYS DWORD dwFlagsAndAttributes) // FILE_ATTRIBUTE_NORMAL) { auto_hfile hFile(::CreateFile(filename.c_str(), dwShareMode, 0, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL)); CHECK_WIN32_BOOL(get(hFile) != NULL, L"Error opening " << filename); if (data.size() > 0) // empty files are ok { DWORD dwWritten = 0; CHECK_WIN32_BOOL(::WriteFile(get(hFile), & * data.begin(), data.size(), & dwWritten, NULL), L"Error writing " << data.size() << " byte(s) to \"" << filename << L"\""); CHECK_BOOL(dwWritten == data.size(), L"Invalid number of bytes written."); } }
void InstallerWindow::SetControlValues() { auto_any<HANDLE, close_handle> done; reset(done, ::CreateEvent(NULL, FALSE, FALSE, NULL)); CHECK_WIN32_BOOL(NULL != get(done), L"CreateEvent"); std::wstring error; htmlayout::queue::push(new SetControlValuesTask(body, get(done), & error), HtmlWindow::s_hwnd); ::WaitForSingleObject(get(done), INFINITE); if (! error.empty()) { THROW_EX(error); } }
void DVLib::ShellCmd(const std::wstring& cmd, int * rc, LPHANDLE lpProcessHandle, HWND hWnd, const std::wstring& working_directory, int nShow) { std::wstring cmd_expanded = DVLib::ExpandEnvironmentVariables(cmd); CHECK_BOOL(! cmd_expanded.empty(), L"Missing command"); // split arguments std::vector<std::wstring> cmd_args; cmd_args = DVLib::split(cmd_expanded, (cmd_expanded[0] == L'\"') ? L"\" " : L" ", 2); std::wstring cmd_file = DVLib::trimleft(cmd_args[0], L"\""); SHELLEXECUTEINFO sei = { 0 }; sei.cbSize = sizeof(sei); sei.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_UNICODE; sei.hwnd = hWnd; // set the current directory if it was specified LPCWSTR lpCurrentDirectory = NULL; if (working_directory != L"") { lpCurrentDirectory = working_directory.c_str(); } sei.lpDirectory = lpCurrentDirectory; sei.lpFile = cmd_file.c_str(); sei.lpParameters = cmd_args.size() == 2 ? cmd_args[1].c_str() : NULL; sei.nShow = nShow; CHECK_WIN32_BOOL(::ShellExecuteExW(&sei), L"Error running " << cmd_expanded); if (NULL != rc) { * rc = reinterpret_cast<int>(sei.hInstApp); } if (NULL != lpProcessHandle) { * lpProcessHandle = sei.hProcess; } }
std::wstring DVLib::GetEnvironmentVariable(const std::wstring& name) { DWORD size = ::GetEnvironmentVariableW(name.c_str(), NULL, 0); if (! size) { DWORD dwErr = ::GetLastError(); // no such environment variable if (dwErr == ERROR_ENVVAR_NOT_FOUND) return L""; // other error CHECK_WIN32_DWORD(dwErr, L"GetEnvironmentVariableW"); } std::vector<wchar_t> value; value.resize(size); CHECK_WIN32_BOOL(0 != (size = ::GetEnvironmentVariableW(name.c_str(), & * value.begin(), value.size())), L"GetEnvironmentVariableW"); return std::wstring(& * value.begin(), value.size() - 1); }
void DVLib::RunCmd(const std::wstring& cmd, LPPROCESS_INFORMATION lpi, int flags, const std::wstring& working_directory, int nShow) { // expand command line, using ShellExecuteEx API function with setting the flag // SEE_MASK_DOENVSUBST does not work because environment variables can also be // placed in the parameters for the new process STARTUPINFO si = { 0 }; si.cb = sizeof(si); DWORD startupInfoFlags = STARTF_USESHOWWINDOW; if (nShow == SW_HIDE) { startupInfoFlags |= CREATE_NO_WINDOW; } si.dwFlags = startupInfoFlags; si.wShowWindow = nShow; PROCESS_INFORMATION pi = { 0 }; std::wstring cmd_expanded = DVLib::ExpandEnvironmentVariables(cmd); // set the current directory if it was specified LPCWSTR lpCurrentDirectory = NULL; if (working_directory != L"") { lpCurrentDirectory = working_directory.c_str(); } CHECK_WIN32_BOOL(::CreateProcessW(NULL, & * cmd_expanded.begin(), NULL, NULL, FALSE, flags, NULL, lpCurrentDirectory, & si, lpi == NULL ? & pi : lpi), L"CreateProcessW: " << cmd_expanded); if (lpi == NULL) { ::CloseHandle(pi.hThread); ::CloseHandle(pi.hProcess); } }
BOOL InstallerWindow::on_mouse_dclick(HELEMENT /* he */, HELEMENT target, POINT pt, UINT /* mouseButtons */, UINT keyboardStates) { if (keyboardStates != 0) { htmlayout::dom::element target_element = target; if (target_element.is_valid()) { htmlayout::dom::element component_element = target_element.find_element(GetHwnd(), pt); if (component_element.is_valid()) { if (component_element.get_ctl_type() != CTL_CHECKBOX) component_element = component_element.parent(); if (component_element.is_valid() && component_element.get_ctl_type() == CTL_CHECKBOX) { const wchar_t * component_ptr = component_element.get_attribute("component_ptr"); if (component_ptr != NULL) { Component * p_component = reinterpret_cast<Component *>(DVLib::wstring2long(component_ptr, 16)); if ((keyboardStates & SHIFT_KEY_PRESSED) > 0) { p_component->checked = ! component_element.get_state(STATE_CHECKED); if (p_component->checked) { component_element["checked"] = L"true"; component_element.set_state(STATE_CHECKED, 0); } else { component_element.remove_attribute("checked"); component_element.set_state(0, STATE_CHECKED); } } else if ((keyboardStates & CONTROL_KEY_PRESSED) > 0) { // doesn't have to be thread-safe, running on UI thread if (m_running_component == NULL) { m_running_component = p_component; reset(m_pThread, AfxBeginThread(RunComponentOnThread, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED)); CHECK_WIN32_BOOL(get(m_pThread) != NULL, L"AfxBeginThread"); m_pThread->m_bAutoDelete = false; m_pThread->ResumeThread(); } } return TRUE; } } } } } return FALSE; }
DVLib::OperatingSystem DVLib::GetOperatingSystemVersion() { DVLib::OperatingSystem os = winNone; OSVERSIONINFOEX osvi = { 0 }; // use GetVersionEx, fallback on GetVersion when unavaialble osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if(! ::GetVersionEx(reinterpret_cast<LPOSVERSIONINFO>(& osvi))) { osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); CHECK_WIN32_BOOL(GetVersionEx(reinterpret_cast<LPOSVERSIONINFO>(& osvi)), L"GetVersionEx"); } SYSTEM_INFO si = { 0 }; ::GetSystemInfo(& si); switch (osvi.dwPlatformId) { // Test for the Windows NT product family. case VER_PLATFORM_WIN32_NT: // Newer Windows version if (((osvi.dwMajorVersion == 6 && osvi.dwMinorVersion > 2) || (osvi.dwMajorVersion > 6)) && osvi.wProductType == VER_NT_WORKSTATION) { os = winMax; } // Windows 8 Server else if ( osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 2 && osvi.wProductType != VER_NT_WORKSTATION) { os = win8Server; } // Windows 8 else if ( osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 2 && osvi.wProductType == VER_NT_WORKSTATION) { os = win8; } // Windows 7 else if ( osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 1 && osvi.wProductType == VER_NT_WORKSTATION) { os = win7; if (osvi.wServicePackMajor >= 1) os = win7sp1; } // Windows Server 2008 R2 else if ( osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 1 && osvi.wProductType != VER_NT_WORKSTATION) { os = winServer2008R2; } // Windows Server 2008 else if ( osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0 && osvi.wProductType != VER_NT_WORKSTATION) { os = winServer2008; if (osvi.wServicePackMajor >= 2) os = winServer2008sp2; } else if ( osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0 && osvi.wProductType == VER_NT_WORKSTATION) { os = winVista; if (osvi.wServicePackMajor == 1) os = winVistaSp1; else if (osvi.wServicePackMajor >= 2) os = winVistaSp2; } // Windows Server 2003 versions else if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 && osvi.wProductType != VER_NT_WORKSTATION && GetSystemMetrics(89 /* SM_SERVERR2 */) == 0) { os = winServer2003; if (osvi.wServicePackMajor == 1) os = winServer2003sp1; else if (osvi.wServicePackMajor >= 2) os = winServer2003sp2; } else if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 && osvi.wProductType != VER_NT_WORKSTATION && GetSystemMetrics(89 /* SM_SERVERR2 */) != 0) { os = winServer2003R2; if (osvi.wServicePackMajor == 1) os = winServer2003R2sp1; else if (osvi.wServicePackMajor >= 2) os = winServer2003R2sp2; } // Windows XP 64 bit Professional else if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 && osvi.wProductType == VER_NT_WORKSTATION) { os = winXP; if (osvi.wServicePackMajor == 1) os = winXPsp1; else if (osvi.wServicePackMajor == 2) os = winXPsp2; else if (osvi.wServicePackMajor >= 3) os = winXPsp3; } // Windows XP 32 bit versions else if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 ) { os = winXP; if (osvi.wServicePackMajor == 1) os = winXPsp1; else if (osvi.wServicePackMajor == 2) os = winXPsp2; else if (osvi.wServicePackMajor >= 3) os = winXPsp3; } // Windows 2000 versions else if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) { os = win2000; if (osvi.wServicePackMajor == 1) os = win2000sp1; else if (osvi.wServicePackMajor == 2) os = win2000sp2; else if (osvi.wServicePackMajor == 3) os = win2000sp3; else if (osvi.wServicePackMajor >= 4) os = win2000sp4; } // Windows NT versions else if ( osvi.dwMajorVersion == 4 ) { os = winNT4; // check if Sp6a if(0 == _wcsicmp(osvi.szCSDVersion, L"Service Pack 6")) { // Test for SP6 versus SP6a. if (DVLib::RegistryKeyExists(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009")) { os = winNT4sp6a; } else // Windows NT 4.0 prior to SP6a { os = winNT4sp6; } } } break; // Test for the Windows 95 product family. case VER_PLATFORM_WIN32_WINDOWS: if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90) { os = winME; } else if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10) { os = win98; //test windows 98 se if ( osvi.szCSDVersion[1] == 'A' ) os = win98se; } else if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0) { os = win95; //test Win95 osr2 if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' ) os = win95osr2; } break; } CHECK_BOOL(os != winNone, L"Unsupported operating system, major=" << osvi.dwMajorVersion << L", version=" << osvi.dwMinorVersion << L"." << osvi.dwMinorVersion << L", sp=" << osvi.wServicePackMajor << L"." << osvi.wServicePackMinor << L", type=" << osvi.wProductType); return os; }
void ProcessComponent::Wait(DWORD /* tt */) { CHECK_WIN32_BOOL(WAIT_OBJECT_0 == WaitForSingleObject(m_process_handle, INFINITE), L"WaitForSingleObject"); }
void DVLib::FileCopy(const std::wstring& from, const std::wstring& to, bool overwrite) { CHECK_WIN32_BOOL(::CopyFileW(from.c_str(), to.c_str(), overwrite), L"Error copying \"" << from << L"\" to \"" << to << L"\""); }