chore: C++ google style

This commit is contained in:
huanghongxun 2021-05-30 02:42:39 +08:00
parent 258a6628e4
commit 6d75c4d34f
10 changed files with 276 additions and 302 deletions

View File

@ -1,2 +1,4 @@
# Use the Google style in this project. # Use the Google style in this project.
BasedOnStyle: Google BasedOnStyle: Google
SortIncludes: false

View File

@ -162,7 +162,7 @@
<ClInclude Include="Resource.h" /> <ClInclude Include="Resource.h" />
<ClInclude Include="stdafx.h" /> <ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" /> <ClInclude Include="targetver.h" />
<ClInclude Include="Version.h" /> <ClInclude Include="version.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="java.cpp" /> <ClCompile Include="java.cpp" />
@ -174,7 +174,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile> </ClCompile>
<ClCompile Include="Version.cpp" /> <ClCompile Include="version.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="HMCL.rc" /> <ResourceCompile Include="HMCL.rc" />

View File

@ -27,7 +27,7 @@
<ClInclude Include="main.h"> <ClInclude Include="main.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Version.h"> <ClInclude Include="version.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="java.h"> <ClInclude Include="java.h">
@ -47,7 +47,7 @@
<ClCompile Include="main.cpp"> <ClCompile Include="main.cpp">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Version.cpp"> <ClCompile Include="version.cpp">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="java.cpp"> <ClCompile Include="java.cpp">

View File

@ -1,17 +1,16 @@
#include "stdafx.h" #include "stdafx.h"
#include "Version.h" #include "version.h"
using namespace std; Version::Version(const std::wstring &rawString) {
int idx = 0;
Version::Version(const wstring & rawString) ver[0] = ver[1] = ver[2] = ver[3] = 0;
{ for (auto &i : rawString) {
int idx = 0; if (idx >= 4) break;
ver[0] = ver[1] = ver[2] = ver[3] = 0; if (i == '.')
for (auto &i : rawString) ++idx;
{ else if (i == '_')
if (idx >= 4) break; ++idx;
if (i == '.') ++idx; else if (isdigit(i))
else if (i == '_') ++idx; ver[idx] = ver[idx] * 10 + (i - L'0');
else if (isdigit(i)) ver[idx] = ver[idx] * 10 + (i - L'0'); }
}
} }

View File

@ -2,39 +2,30 @@
#include <string> #include <string>
class Version class Version {
{ public:
public: int ver[4];
int ver[4];
Version(const std::wstring &rawString); Version(const std::wstring &rawString);
template <typename T> template <typename T>
Version(std::initializer_list<T> ver_list) Version(std::initializer_list<T> ver_list) {
{ int i = 0;
int i = 0; for (const auto &data : ver_list) {
for (const auto &data : ver_list) if (i >= 4) break;
{ ver[i++] = data;
if (i >= 4) }
break; }
ver[i++] = data;
}
}
bool operator<(const Version &other) const bool operator<(const Version &other) const {
{ for (int i = 0; i < 4; ++i)
for (int i = 0; i < 4; ++i) if (ver[i] != other.ver[i]) return ver[i] < other.ver[i];
if (ver[i] != other.ver[i]) return false;
return ver[i] < other.ver[i]; }
return false;
}
bool operator<=(const Version &other) const bool operator<=(const Version &other) const {
{ for (int i = 0; i < 4; ++i)
for (int i = 0; i < 4; ++i) if (ver[i] != other.ver[i]) return ver[i] < other.ver[i];
if (ver[i] != other.ver[i]) return true;
return ver[i] < other.ver[i]; }
return true;
}
}; };

View File

@ -1,6 +1,7 @@
#include "stdafx.h" #include "stdafx.h"
#include "java.h" #include "java.h"
#include "os.h" #include "os.h"
#include "version.h"
const Version JAVA_8(L"1.8"), JAVA_11(L"11"); const Version JAVA_8(L"1.8"), JAVA_11(L"11");
@ -11,76 +12,72 @@ const LPCWSTR JRE_NEW = L"SOFTWARE\\JavaSoft\\JRE";
bool oldJavaFound = false; bool oldJavaFound = false;
bool FindJavaByRegistryKey(HKEY rootKey, LPCWSTR subKey, std::wstring & path) bool FindJavaByRegistryKey(HKEY rootKey, LPCWSTR subKey, std::wstring& path) {
{ WCHAR javaVer[MAX_KEY_LENGTH]; // buffer for subkey name, special for
WCHAR javaVer[MAX_KEY_LENGTH]; // buffer for subkey name, special for JavaVersion // JavaVersion
DWORD cbName; // size of name string DWORD cbName; // size of name string
DWORD cSubKeys = 0; // number of subkeys DWORD cSubKeys = 0; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size DWORD cbMaxSubKey; // longest subkey size
DWORD cValues; // number of values for key DWORD cValues; // number of values for key
DWORD cchMaxValue; // longest value name DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data DWORD cbMaxValueData; // longest value data
LSTATUS result; LSTATUS result;
HKEY hKey; HKEY hKey;
if (ERROR_SUCCESS != (result = RegOpenKeyEx(rootKey, subKey, 0, KEY_WOW64_64KEY | KEY_READ, &hKey))) if (ERROR_SUCCESS !=
return false; (result =
RegOpenKeyEx(rootKey, subKey, 0, KEY_WOW64_64KEY | KEY_READ, &hKey)))
return false;
RegQueryInfoKey( RegQueryInfoKey(hKey, // key handle
hKey, // key handle NULL, // buffer for class name
NULL, // buffer for class name NULL, // size of class string
NULL, // size of class string NULL, // reserved
NULL, // reserved &cSubKeys, // number of subkeys
&cSubKeys, // number of subkeys &cbMaxSubKey, // longest subkey size
&cbMaxSubKey, // longest subkey size NULL, // longest class string
NULL, // longest class string &cValues, // number of values for this key
&cValues, // number of values for this key &cchMaxValue, // longest value name
&cchMaxValue, // longest value name &cbMaxValueData, // longest value data
&cbMaxValueData, // longest value data NULL, // security descriptor
NULL, // security descriptor NULL); // last write time
NULL); // last write time
if (!cSubKeys) if (!cSubKeys) return false;
return false;
bool flag = false; bool flag = false;
for (DWORD i = 0; i < cSubKeys; ++i) for (DWORD i = 0; i < cSubKeys; ++i) {
{ cbName = MAX_KEY_LENGTH;
cbName = MAX_KEY_LENGTH; if (ERROR_SUCCESS != (result = RegEnumKeyEx(hKey, i, javaVer, &cbName, NULL,
if (ERROR_SUCCESS != (result = RegEnumKeyEx(hKey, i, javaVer, &cbName, NULL, NULL, NULL, NULL))) NULL, NULL, NULL)))
continue; continue;
HKEY javaKey; HKEY javaKey;
if (ERROR_SUCCESS != RegOpenKeyEx(hKey, javaVer, 0, KEY_READ, &javaKey)) if (ERROR_SUCCESS != RegOpenKeyEx(hKey, javaVer, 0, KEY_READ, &javaKey))
continue; continue;
if (ERROR_SUCCESS == MyRegQueryValue(javaKey, L"JavaHome", REG_SZ, path)) if (ERROR_SUCCESS == MyRegQueryValue(javaKey, L"JavaHome", REG_SZ, path)) {
{ if (Version(javaVer) < JAVA_8)
if (Version(javaVer) < JAVA_8) oldJavaFound = true;
oldJavaFound = true; else
else flag = true;
flag = true; }
}
if (flag) if (flag) break;
break; }
}
RegCloseKey(hKey); RegCloseKey(hKey);
return flag; return flag;
} }
bool FindJavaInRegistry(std::wstring & path) bool FindJavaInRegistry(std::wstring& path) {
{ return FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JDK_OLD, path) ||
return FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JDK_OLD, path) || FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JRE_OLD, path) ||
FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JRE_OLD, path) || FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JDK_NEW, path) ||
FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JDK_NEW, path) || FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JRE_NEW, path);
FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JRE_NEW, path);
} }
bool FindJava(std::wstring & path) bool FindJava(std::wstring& path) {
{ return FindJavaInRegistry(path) ||
return FindJavaInRegistry(path) || ERROR_SUCCESS == MyGetEnvironmentVariable(L"JAVA_HOME", path);
ERROR_SUCCESS == MyGetEnvironmentVariable(L"JAVA_HOME", path);
} }

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <windows.h> #include <windows.h>
#include <string> #include <string>
#include "Version.h"
// Find Java installation in system registry // Find Java installation in system registry
bool FindJavaInRegistry(std::wstring &path); bool FindJavaInRegistry(std::wstring &path);

View File

@ -4,96 +4,97 @@
#include "java.h" #include "java.h"
#include "lang.h" #include "lang.h"
using namespace std;
Version J8(TEXT("8")); Version J8(TEXT("8"));
void RawLaunchJVM(const wstring &javaPath, const wstring& workdir, const wstring &jarPath) void RawLaunchJVM(const std::wstring &javaPath, const std::wstring &workdir,
{ const std::wstring &jarPath) {
if (MyCreateProcess(L"\"" + javaPath + L"\" -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=15 -jar \"" + jarPath + L"\"", workdir)) if (MyCreateProcess(
exit(EXIT_SUCCESS); L"\"" + javaPath +
L"\" -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=15 -jar \"" +
jarPath + L"\"",
workdir))
exit(EXIT_SUCCESS);
} }
void LaunchJVM(const wstring &javaPath, const wstring& workdir, const wstring &jarPath) void LaunchJVM(const std::wstring &javaPath, const std::wstring &workdir,
{ const std::wstring &jarPath) {
Version javaVersion(L""); Version javaVersion(L"");
if (!MyGetFileVersionInfo(javaPath, javaVersion)) if (!MyGetFileVersionInfo(javaPath, javaVersion)) return;
return;
if (J8 <= javaVersion) if (J8 <= javaVersion) {
{ RawLaunchJVM(javaPath, workdir, jarPath);
RawLaunchJVM(javaPath, workdir, jarPath); }
}
} }
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
{ LPWSTR lpCmdLine, int nCmdShow) {
wstring path, exeName; std::wstring path, exeName;
// Since Jar file is appended to this executable, we should first get the location of JAR file. // Since Jar file is appended to this executable, we should first get the
if (ERROR_SUCCESS != MyGetModuleFileName(NULL, exeName)) // location of JAR file.
return 1; if (ERROR_SUCCESS != MyGetModuleFileName(NULL, exeName)) return 1;
wstring workdir; std::wstring workdir;
size_t last_slash = exeName.find_last_of(L"/\\"); size_t last_slash = exeName.find_last_of(L"/\\");
if (last_slash != wstring::npos && last_slash + 1 < exeName.size()) { if (last_slash != std::wstring::npos && last_slash + 1 < exeName.size()) {
workdir = exeName.substr(0, last_slash); workdir = exeName.substr(0, last_slash);
exeName = exeName.substr(last_slash + 1); exeName = exeName.substr(last_slash + 1);
} }
// TODO: check whether the bundled JRE is valid. // TODO: check whether the bundled JRE is valid.
// First try the Java packaged together. // First try the Java packaged together.
bool is64Bit = false; bool is64Bit = false;
GetArch(is64Bit); // if failed to determine architecture of operating system, consider 32-bit. GetArch(is64Bit); // if failed to determine architecture of operating system,
// consider 32-bit.
if (is64Bit) if (is64Bit) {
{ RawLaunchJVM(L"jre-x64\\bin\\javaw.exe", workdir, exeName);
RawLaunchJVM(L"jre-x64\\bin\\javaw.exe", workdir, exeName); } else {
} RawLaunchJVM(L"jre-x86\\bin\\javaw.exe", workdir, exeName);
else }
{
RawLaunchJVM(L"jre-x86\\bin\\javaw.exe", workdir, exeName);
}
if (FindJava(path)) if (FindJava(path)) LaunchJVM(path + L"\\bin\\javaw.exe", workdir, exeName);
LaunchJVM(path + L"\\bin\\javaw.exe", workdir, exeName);
// Or we try to search Java in C:\Program Files. // Or we try to search Java in C:\Program Files.
{ {
WIN32_FIND_DATA data; WIN32_FIND_DATA data;
HANDLE hFind = FindFirstFile(L"C:\\Program Files\\Java\\*", &data); // Search all subdirectory HANDLE hFind = FindFirstFile(L"C:\\Program Files\\Java\\*",
&data); // Search all subdirectory
if (hFind != INVALID_HANDLE_VALUE) { if (hFind != INVALID_HANDLE_VALUE) {
do { do {
wstring javaw = wstring(L"C:\\Program Files\\Java\\") + data.cFileName + wstring(L"\\bin\\javaw.exe"); std::wstring javaw = std::wstring(L"C:\\Program Files\\Java\\") +
if (FindFirstFileExists(javaw.c_str(), 0)) { data.cFileName + std::wstring(L"\\bin\\javaw.exe");
LaunchJVM(javaw, workdir, exeName); if (FindFirstFileExists(javaw.c_str(), 0)) {
} LaunchJVM(javaw, workdir, exeName);
} while (FindNextFile(hFind, &data)); }
FindClose(hFind); } while (FindNextFile(hFind, &data));
} FindClose(hFind);
} }
}
// Consider C:\Program Files (x86)\Java // Consider C:\Program Files (x86)\Java
{ {
WIN32_FIND_DATA data; WIN32_FIND_DATA data;
HANDLE hFind = FindFirstFile(L"C:\\Program Files (x86)\\Java\\*", &data); // Search all subdirectory HANDLE hFind = FindFirstFile(L"C:\\Program Files (x86)\\Java\\*",
&data); // Search all subdirectory
if (hFind != INVALID_HANDLE_VALUE) { if (hFind != INVALID_HANDLE_VALUE) {
do { do {
wstring javaw = wstring(L"C:\\Program Files (x86)\\Java\\") + data.cFileName + L"\\bin\\javaw.exe"; std::wstring javaw = std::wstring(L"C:\\Program Files (x86)\\Java\\") +
if (FindFirstFileExists(javaw.c_str(), 0)) { data.cFileName + L"\\bin\\javaw.exe";
LaunchJVM(javaw, workdir, exeName); if (FindFirstFileExists(javaw.c_str(), 0)) {
} LaunchJVM(javaw, workdir, exeName);
} while (FindNextFile(hFind, &data)); }
FindClose(hFind); } while (FindNextFile(hFind, &data));
} FindClose(hFind);
} }
}
// Try java in PATH // Try java in PATH
RawLaunchJVM(L"javaw", workdir, exeName); RawLaunchJVM(L"javaw", workdir, exeName);
MessageBox(NULL, ERROR_PROMPT, L"Error", MB_ICONERROR | MB_OK); MessageBox(NULL, ERROR_PROMPT, L"Error", MB_ICONERROR | MB_OK);
ShellExecute(0, 0, L"https://java.com/", 0, 0, SW_SHOW); ShellExecute(0, 0, L"https://java.com/", 0, 0, SW_SHOW);
return 1; return 1;
} }

View File

@ -1,146 +1,128 @@
#include "stdafx.h" #include "stdafx.h"
#include "os.h" #include "os.h"
using namespace std; LSTATUS MyRegQueryValue(HKEY hKey, LPCWSTR subKey, DWORD dwType,
std::wstring &out) {
LSTATUS MyRegQueryValue(HKEY hKey, LPCWSTR subKey, DWORD dwType, wstring &out) DWORD dwSize = 0;
{ LSTATUS ret = RegQueryValueEx(hKey, subKey, 0, &dwType, NULL, &dwSize);
DWORD dwSize = 0; if (ret != ERROR_SUCCESS) return ret;
LSTATUS ret = RegQueryValueEx(hKey, subKey, 0, &dwType, NULL, &dwSize); WCHAR *buffer = new WCHAR[dwSize];
if (ret != ERROR_SUCCESS) return ret; ret = RegQueryValueEx(hKey, subKey, 0, &dwType, (LPBYTE)buffer, &dwSize);
WCHAR *buffer = new WCHAR[dwSize]; if (ret != ERROR_SUCCESS) return ret;
ret = RegQueryValueEx(hKey, subKey, 0, &dwType, (LPBYTE)buffer, &dwSize); out = buffer;
if (ret != ERROR_SUCCESS) return ret; delete[] buffer;
out = buffer; return ERROR_SUCCESS;
delete[] buffer;
return ERROR_SUCCESS;
} }
LSTATUS MyGetModuleFileName(HMODULE hModule, std::wstring &out) LSTATUS MyGetModuleFileName(HMODULE hModule, std::wstring &out) {
{ DWORD res, size = MAX_PATH;
DWORD res, size = MAX_PATH; out = std::wstring();
out = wstring(); out.resize(size);
out.resize(size); while ((res = GetModuleFileName(hModule, &out[0], size)) == size) {
while ((res = GetModuleFileName(hModule, &out[0], size)) == size) out.resize(size += MAX_PATH);
{ }
out.resize(size += MAX_PATH); if (res == 0)
} return GetLastError();
if (res == 0) else {
return GetLastError(); out.resize(size - MAX_PATH + res);
else return ERROR_SUCCESS;
{ }
out.resize(size - MAX_PATH + res);
return ERROR_SUCCESS;
}
} }
LSTATUS MyGetEnvironmentVariable(LPCWSTR name, std::wstring & out) LSTATUS MyGetEnvironmentVariable(LPCWSTR name, std::wstring &out) {
{ DWORD res, size = MAX_PATH;
DWORD res, size = MAX_PATH; out = std::wstring();
out = wstring(); out.resize(size);
out.resize(size); while ((res = GetEnvironmentVariable(name, &out[0], size)) == size) {
while ((res = GetEnvironmentVariable(name, &out[0], size)) == size) out.resize(size += MAX_PATH);
{ }
out.resize(size += MAX_PATH); if (res == 0)
} return GetLastError();
if (res == 0) else {
return GetLastError(); out.resize(size - MAX_PATH + res);
else return ERROR_SUCCESS;
{ }
out.resize(size - MAX_PATH + res);
return ERROR_SUCCESS;
}
} }
bool MyCreateProcess(const std::wstring &command, const std::wstring &workdir) bool MyCreateProcess(const std::wstring &command, const std::wstring &workdir) {
{ std::wstring writable_command = command;
wstring writable_command = command; STARTUPINFO si;
STARTUPINFO si; PROCESS_INFORMATION pi;
PROCESS_INFORMATION pi; si.cb = sizeof(si);
si.cb = sizeof(si); ZeroMemory(&si, sizeof(si));
ZeroMemory(&si, sizeof(si)); ZeroMemory(&pi, sizeof(pi));
ZeroMemory(&pi, sizeof(pi));
if (workdir.empty()) { if (workdir.empty()) {
return CreateProcess(NULL, &writable_command[0], NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi); return CreateProcess(NULL, &writable_command[0], NULL, NULL, false,
} NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
else { } else {
return CreateProcess(NULL, &writable_command[0], NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, workdir.c_str(), &si, &pi); return CreateProcess(NULL, &writable_command[0], NULL, NULL, false,
} NORMAL_PRIORITY_CLASS, NULL, workdir.c_str(), &si,
&pi);
}
} }
bool FindFirstFileExists(LPCWSTR lpPath, DWORD dwFilter) bool FindFirstFileExists(LPCWSTR lpPath, DWORD dwFilter) {
{ WIN32_FIND_DATA fd;
WIN32_FIND_DATA fd; HANDLE hFind = FindFirstFile(lpPath, &fd);
HANDLE hFind = FindFirstFile(lpPath, &fd); bool bFilter = (false == dwFilter) ? true : fd.dwFileAttributes & dwFilter;
bool bFilter = (false == dwFilter) ? true : fd.dwFileAttributes & dwFilter; bool ret = ((hFind != INVALID_HANDLE_VALUE) && bFilter) ? true : false;
bool ret = ((hFind != INVALID_HANDLE_VALUE) && bFilter) ? true : false; FindClose(hFind);
FindClose(hFind); return ret;
return ret;
} }
bool GetArch(bool & is64Bit) bool GetArch(bool &is64Bit) {
{
#if _WIN64 #if _WIN64
is64Bit = true; is64Bit = true;
return true; return true;
#elif _WIN32 #elif _WIN32
typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); typedef BOOL(WINAPI * LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
BOOL isWow64 = FALSE; BOOL isWow64 = FALSE;
// IsWow64Process is not available on all supported versions of Windows. // IsWow64Process is not available on all supported versions of Windows.
// Use GetModuleHandle to get a handle to the DLL that contains the function // Use GetModuleHandle to get a handle to the DLL that contains the function
// and GetProcAddress to get a pointer to the function if available. // and GetProcAddress to get a pointer to the function if available.
LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS) LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process"); GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
if (fnIsWow64Process) if (fnIsWow64Process) {
{ if (!fnIsWow64Process(GetCurrentProcess(), &isWow64)) return false;
if (!fnIsWow64Process(GetCurrentProcess(), &isWow64))
return false;
is64Bit = isWow64; is64Bit = isWow64;
return true; return true;
} } else // IsWow64Process is not supported, fail to detect.
else // IsWow64Process is not supported, fail to detect. return false;
return false;
#else #else
#error _WIN64 and _WIN32 are both undefined. #error _WIN64 and _WIN32 are both undefined.
#endif #endif
} }
bool MyGetFileVersionInfo(const std::wstring & filePath, Version &version) bool MyGetFileVersionInfo(const std::wstring &filePath, Version &version) {
{ DWORD verHandle = 0;
DWORD verHandle = 0; UINT size = 0;
UINT size = 0; LPBYTE lpBuffer = NULL;
LPBYTE lpBuffer = NULL; VS_FIXEDFILEINFO *pFileInfo;
VS_FIXEDFILEINFO *pFileInfo; DWORD dwSize = GetFileVersionInfoSize(filePath.c_str(), NULL);
DWORD dwSize = GetFileVersionInfoSize(filePath.c_str(), NULL);
if (!dwSize) if (!dwSize) return false;
return false;
LPBYTE data = new BYTE[dwSize]; LPBYTE data = new BYTE[dwSize];
if (!GetFileVersionInfo(filePath.c_str(), 0, dwSize, data)) if (!GetFileVersionInfo(filePath.c_str(), 0, dwSize, data)) {
{ delete[] data;
delete[] data; return false;
return false; }
}
if (!VerQueryValue(data, TEXT("\\"), (LPVOID*)&pFileInfo, &size)) if (!VerQueryValue(data, TEXT("\\"), (LPVOID *)&pFileInfo, &size)) {
{ delete[] data;
delete[] data; return false;
return false; }
}
version = Version{ version = Version{(pFileInfo->dwFileVersionMS >> 16) & 0xFFFF,
(pFileInfo->dwFileVersionMS >> 16) & 0xFFFF, (pFileInfo->dwFileVersionMS >> 0) & 0xFFFF,
(pFileInfo->dwFileVersionMS >> 0) & 0xFFFF, (pFileInfo->dwFileVersionLS >> 16) & 0xFFFF,
(pFileInfo->dwFileVersionLS >> 16) & 0xFFFF, (pFileInfo->dwFileVersionLS >> 0) & 0xFFFF};
(pFileInfo->dwFileVersionLS >> 0) & 0xFFFF return true;
};
return true;
} }

View File

@ -6,17 +6,20 @@
const int MAX_KEY_LENGTH = 255; const int MAX_KEY_LENGTH = 255;
const int MAX_VALUE_NAME = 16383; const int MAX_VALUE_NAME = 16383;
// Query registry value of class root hKey, key path subKey, stores result in parameter out. // Query registry value of class root hKey, key path subKey, stores result in
LSTATUS MyRegQueryValue(HKEY hKey, LPCWSTR subKey, DWORD dwType, std::wstring &out); // parameter out.
LSTATUS MyRegQueryValue(HKEY hKey, LPCWSTR subKey, DWORD dwType,
std::wstring &out);
// Get module file name, stores result in parameter out. // Get module file name, stores result in parameter out.
LSTATUS MyGetModuleFileName(HMODULE hModule, std::wstring &out); LSTATUS MyGetModuleFileName(HMODULE hModule, std::wstring &out);
// Get environment variable by name, C++ style, stores the value in parameter out. // Get environment variable by name, C++ style, stores the value in parameter
// out.
LSTATUS MyGetEnvironmentVariable(LPCWSTR name, std::wstring &out); LSTATUS MyGetEnvironmentVariable(LPCWSTR name, std::wstring &out);
// Create process by invoking CreateProcess, only pass command. // Create process by invoking CreateProcess, only pass command.
bool MyCreateProcess(const std::wstring &command, const std::wstring& workdir); bool MyCreateProcess(const std::wstring &command, const std::wstring &workdir);
// Check if file lpPath exists. // Check if file lpPath exists.
bool FindFirstFileExists(LPCWSTR lpPath, DWORD dwFilter); bool FindFirstFileExists(LPCWSTR lpPath, DWORD dwFilter);