mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-01-30 14:39:56 +08:00
chore: C++ google style
This commit is contained in:
parent
258a6628e4
commit
6d75c4d34f
@ -1,2 +1,4 @@
|
||||
# Use the Google style in this project.
|
||||
BasedOnStyle: Google
|
||||
BasedOnStyle: Google
|
||||
|
||||
SortIncludes: false
|
@ -162,7 +162,7 @@
|
||||
<ClInclude Include="Resource.h" />
|
||||
<ClInclude Include="stdafx.h" />
|
||||
<ClInclude Include="targetver.h" />
|
||||
<ClInclude Include="Version.h" />
|
||||
<ClInclude Include="version.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="java.cpp" />
|
||||
@ -174,7 +174,7 @@
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Version.cpp" />
|
||||
<ClCompile Include="version.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="HMCL.rc" />
|
||||
|
@ -27,7 +27,7 @@
|
||||
<ClInclude Include="main.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Version.h">
|
||||
<ClInclude Include="version.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="java.h">
|
||||
@ -47,7 +47,7 @@
|
||||
<ClCompile Include="main.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Version.cpp">
|
||||
<ClCompile Include="version.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="java.cpp">
|
||||
|
@ -1,17 +1,16 @@
|
||||
#include "stdafx.h"
|
||||
#include "Version.h"
|
||||
#include "version.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
Version::Version(const wstring & rawString)
|
||||
{
|
||||
int idx = 0;
|
||||
ver[0] = ver[1] = ver[2] = ver[3] = 0;
|
||||
for (auto &i : rawString)
|
||||
{
|
||||
if (idx >= 4) break;
|
||||
if (i == '.') ++idx;
|
||||
else if (i == '_') ++idx;
|
||||
else if (isdigit(i)) ver[idx] = ver[idx] * 10 + (i - L'0');
|
||||
}
|
||||
Version::Version(const std::wstring &rawString) {
|
||||
int idx = 0;
|
||||
ver[0] = ver[1] = ver[2] = ver[3] = 0;
|
||||
for (auto &i : rawString) {
|
||||
if (idx >= 4) break;
|
||||
if (i == '.')
|
||||
++idx;
|
||||
else if (i == '_')
|
||||
++idx;
|
||||
else if (isdigit(i))
|
||||
ver[idx] = ver[idx] * 10 + (i - L'0');
|
||||
}
|
||||
}
|
||||
|
@ -2,39 +2,30 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
class Version
|
||||
{
|
||||
public:
|
||||
int ver[4];
|
||||
class Version {
|
||||
public:
|
||||
int ver[4];
|
||||
|
||||
Version(const std::wstring &rawString);
|
||||
Version(const std::wstring &rawString);
|
||||
|
||||
template <typename T>
|
||||
Version(std::initializer_list<T> ver_list)
|
||||
{
|
||||
int i = 0;
|
||||
for (const auto &data : ver_list)
|
||||
{
|
||||
if (i >= 4)
|
||||
break;
|
||||
ver[i++] = data;
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
Version(std::initializer_list<T> ver_list) {
|
||||
int i = 0;
|
||||
for (const auto &data : ver_list) {
|
||||
if (i >= 4) break;
|
||||
ver[i++] = data;
|
||||
}
|
||||
}
|
||||
|
||||
bool operator<(const Version &other) const
|
||||
{
|
||||
for (int i = 0; i < 4; ++i)
|
||||
if (ver[i] != other.ver[i])
|
||||
return ver[i] < other.ver[i];
|
||||
return false;
|
||||
}
|
||||
bool operator<(const Version &other) const {
|
||||
for (int i = 0; i < 4; ++i)
|
||||
if (ver[i] != other.ver[i]) return ver[i] < other.ver[i];
|
||||
return false;
|
||||
}
|
||||
|
||||
bool operator<=(const Version &other) const
|
||||
{
|
||||
for (int i = 0; i < 4; ++i)
|
||||
if (ver[i] != other.ver[i])
|
||||
return ver[i] < other.ver[i];
|
||||
return true;
|
||||
}
|
||||
bool operator<=(const Version &other) const {
|
||||
for (int i = 0; i < 4; ++i)
|
||||
if (ver[i] != other.ver[i]) return ver[i] < other.ver[i];
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "stdafx.h"
|
||||
#include "java.h"
|
||||
#include "os.h"
|
||||
#include "version.h"
|
||||
|
||||
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 FindJavaByRegistryKey(HKEY rootKey, LPCWSTR subKey, std::wstring & path)
|
||||
{
|
||||
WCHAR javaVer[MAX_KEY_LENGTH]; // buffer for subkey name, special for JavaVersion
|
||||
DWORD cbName; // size of name string
|
||||
DWORD cSubKeys = 0; // number of subkeys
|
||||
DWORD cbMaxSubKey; // longest subkey size
|
||||
DWORD cValues; // number of values for key
|
||||
DWORD cchMaxValue; // longest value name
|
||||
DWORD cbMaxValueData; // longest value data
|
||||
LSTATUS result;
|
||||
bool FindJavaByRegistryKey(HKEY rootKey, LPCWSTR subKey, std::wstring& path) {
|
||||
WCHAR javaVer[MAX_KEY_LENGTH]; // buffer for subkey name, special for
|
||||
// JavaVersion
|
||||
DWORD cbName; // size of name string
|
||||
DWORD cSubKeys = 0; // number of subkeys
|
||||
DWORD cbMaxSubKey; // longest subkey size
|
||||
DWORD cValues; // number of values for key
|
||||
DWORD cchMaxValue; // longest value name
|
||||
DWORD cbMaxValueData; // longest value data
|
||||
LSTATUS result;
|
||||
|
||||
HKEY hKey;
|
||||
if (ERROR_SUCCESS != (result = RegOpenKeyEx(rootKey, subKey, 0, KEY_WOW64_64KEY | KEY_READ, &hKey)))
|
||||
return false;
|
||||
HKEY hKey;
|
||||
if (ERROR_SUCCESS !=
|
||||
(result =
|
||||
RegOpenKeyEx(rootKey, subKey, 0, KEY_WOW64_64KEY | KEY_READ, &hKey)))
|
||||
return false;
|
||||
|
||||
RegQueryInfoKey(
|
||||
hKey, // key handle
|
||||
NULL, // buffer for class name
|
||||
NULL, // size of class string
|
||||
NULL, // reserved
|
||||
&cSubKeys, // number of subkeys
|
||||
&cbMaxSubKey, // longest subkey size
|
||||
NULL, // longest class string
|
||||
&cValues, // number of values for this key
|
||||
&cchMaxValue, // longest value name
|
||||
&cbMaxValueData, // longest value data
|
||||
NULL, // security descriptor
|
||||
NULL); // last write time
|
||||
RegQueryInfoKey(hKey, // key handle
|
||||
NULL, // buffer for class name
|
||||
NULL, // size of class string
|
||||
NULL, // reserved
|
||||
&cSubKeys, // number of subkeys
|
||||
&cbMaxSubKey, // longest subkey size
|
||||
NULL, // longest class string
|
||||
&cValues, // number of values for this key
|
||||
&cchMaxValue, // longest value name
|
||||
&cbMaxValueData, // longest value data
|
||||
NULL, // security descriptor
|
||||
NULL); // last write time
|
||||
|
||||
if (!cSubKeys)
|
||||
return false;
|
||||
if (!cSubKeys) return false;
|
||||
|
||||
bool flag = false;
|
||||
for (DWORD i = 0; i < cSubKeys; ++i)
|
||||
{
|
||||
cbName = MAX_KEY_LENGTH;
|
||||
if (ERROR_SUCCESS != (result = RegEnumKeyEx(hKey, i, javaVer, &cbName, NULL, NULL, NULL, NULL)))
|
||||
continue;
|
||||
bool flag = false;
|
||||
for (DWORD i = 0; i < cSubKeys; ++i) {
|
||||
cbName = MAX_KEY_LENGTH;
|
||||
if (ERROR_SUCCESS != (result = RegEnumKeyEx(hKey, i, javaVer, &cbName, NULL,
|
||||
NULL, NULL, NULL)))
|
||||
continue;
|
||||
|
||||
HKEY javaKey;
|
||||
if (ERROR_SUCCESS != RegOpenKeyEx(hKey, javaVer, 0, KEY_READ, &javaKey))
|
||||
continue;
|
||||
HKEY javaKey;
|
||||
if (ERROR_SUCCESS != RegOpenKeyEx(hKey, javaVer, 0, KEY_READ, &javaKey))
|
||||
continue;
|
||||
|
||||
if (ERROR_SUCCESS == MyRegQueryValue(javaKey, L"JavaHome", REG_SZ, path))
|
||||
{
|
||||
if (Version(javaVer) < JAVA_8)
|
||||
oldJavaFound = true;
|
||||
else
|
||||
flag = true;
|
||||
}
|
||||
if (ERROR_SUCCESS == MyRegQueryValue(javaKey, L"JavaHome", REG_SZ, path)) {
|
||||
if (Version(javaVer) < JAVA_8)
|
||||
oldJavaFound = true;
|
||||
else
|
||||
flag = true;
|
||||
}
|
||||
|
||||
if (flag)
|
||||
break;
|
||||
}
|
||||
if (flag) break;
|
||||
}
|
||||
|
||||
RegCloseKey(hKey);
|
||||
RegCloseKey(hKey);
|
||||
|
||||
return flag;
|
||||
return flag;
|
||||
}
|
||||
|
||||
bool FindJavaInRegistry(std::wstring & path)
|
||||
{
|
||||
return FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JDK_OLD, path) ||
|
||||
FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JRE_OLD, path) ||
|
||||
FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JDK_NEW, path) ||
|
||||
FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JRE_NEW, path);
|
||||
bool FindJavaInRegistry(std::wstring& path) {
|
||||
return FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JDK_OLD, path) ||
|
||||
FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JRE_OLD, path) ||
|
||||
FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JDK_NEW, path) ||
|
||||
FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JRE_NEW, path);
|
||||
}
|
||||
|
||||
bool FindJava(std::wstring & path)
|
||||
{
|
||||
return FindJavaInRegistry(path) ||
|
||||
ERROR_SUCCESS == MyGetEnvironmentVariable(L"JAVA_HOME", path);
|
||||
bool FindJava(std::wstring& path) {
|
||||
return FindJavaInRegistry(path) ||
|
||||
ERROR_SUCCESS == MyGetEnvironmentVariable(L"JAVA_HOME", path);
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
#include <windows.h>
|
||||
#include <string>
|
||||
#include "Version.h"
|
||||
|
||||
// Find Java installation in system registry
|
||||
bool FindJavaInRegistry(std::wstring &path);
|
||||
|
@ -4,96 +4,97 @@
|
||||
#include "java.h"
|
||||
#include "lang.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
Version J8(TEXT("8"));
|
||||
|
||||
void RawLaunchJVM(const wstring &javaPath, const wstring& workdir, const wstring &jarPath)
|
||||
{
|
||||
if (MyCreateProcess(L"\"" + javaPath + L"\" -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=15 -jar \"" + jarPath + L"\"", workdir))
|
||||
exit(EXIT_SUCCESS);
|
||||
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))
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
void LaunchJVM(const wstring &javaPath, const wstring& workdir, const wstring &jarPath)
|
||||
{
|
||||
Version javaVersion(L"");
|
||||
if (!MyGetFileVersionInfo(javaPath, javaVersion))
|
||||
return;
|
||||
void LaunchJVM(const std::wstring &javaPath, const std::wstring &workdir,
|
||||
const std::wstring &jarPath) {
|
||||
Version javaVersion(L"");
|
||||
if (!MyGetFileVersionInfo(javaPath, javaVersion)) return;
|
||||
|
||||
if (J8 <= javaVersion)
|
||||
{
|
||||
RawLaunchJVM(javaPath, workdir, jarPath);
|
||||
}
|
||||
if (J8 <= javaVersion) {
|
||||
RawLaunchJVM(javaPath, workdir, jarPath);
|
||||
}
|
||||
}
|
||||
|
||||
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
|
||||
{
|
||||
wstring path, exeName;
|
||||
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
||||
LPWSTR lpCmdLine, int nCmdShow) {
|
||||
std::wstring path, exeName;
|
||||
|
||||
// Since Jar file is appended to this executable, we should first get the location of JAR file.
|
||||
if (ERROR_SUCCESS != MyGetModuleFileName(NULL, exeName))
|
||||
return 1;
|
||||
// Since Jar file is appended to this executable, we should first get the
|
||||
// location of JAR file.
|
||||
if (ERROR_SUCCESS != MyGetModuleFileName(NULL, exeName)) return 1;
|
||||
|
||||
wstring workdir;
|
||||
size_t last_slash = exeName.find_last_of(L"/\\");
|
||||
if (last_slash != wstring::npos && last_slash + 1 < exeName.size()) {
|
||||
workdir = exeName.substr(0, last_slash);
|
||||
exeName = exeName.substr(last_slash + 1);
|
||||
}
|
||||
std::wstring workdir;
|
||||
size_t last_slash = exeName.find_last_of(L"/\\");
|
||||
if (last_slash != std::wstring::npos && last_slash + 1 < exeName.size()) {
|
||||
workdir = exeName.substr(0, last_slash);
|
||||
exeName = exeName.substr(last_slash + 1);
|
||||
}
|
||||
|
||||
// TODO: check whether the bundled JRE is valid.
|
||||
// First try the Java packaged together.
|
||||
bool is64Bit = false;
|
||||
GetArch(is64Bit); // if failed to determine architecture of operating system, consider 32-bit.
|
||||
// TODO: check whether the bundled JRE is valid.
|
||||
// First try the Java packaged together.
|
||||
bool is64Bit = false;
|
||||
GetArch(is64Bit); // if failed to determine architecture of operating system,
|
||||
// consider 32-bit.
|
||||
|
||||
if (is64Bit)
|
||||
{
|
||||
RawLaunchJVM(L"jre-x64\\bin\\javaw.exe", workdir, exeName);
|
||||
}
|
||||
else
|
||||
{
|
||||
RawLaunchJVM(L"jre-x86\\bin\\javaw.exe", workdir, exeName);
|
||||
}
|
||||
if (is64Bit) {
|
||||
RawLaunchJVM(L"jre-x64\\bin\\javaw.exe", workdir, exeName);
|
||||
} else {
|
||||
RawLaunchJVM(L"jre-x86\\bin\\javaw.exe", workdir, exeName);
|
||||
}
|
||||
|
||||
if (FindJava(path))
|
||||
LaunchJVM(path + L"\\bin\\javaw.exe", workdir, exeName);
|
||||
if (FindJava(path)) LaunchJVM(path + L"\\bin\\javaw.exe", workdir, exeName);
|
||||
|
||||
// Or we try to search Java in C:\Program Files.
|
||||
{
|
||||
WIN32_FIND_DATA data;
|
||||
HANDLE hFind = FindFirstFile(L"C:\\Program Files\\Java\\*", &data); // Search all subdirectory
|
||||
// Or we try to search Java in C:\Program Files.
|
||||
{
|
||||
WIN32_FIND_DATA data;
|
||||
HANDLE hFind = FindFirstFile(L"C:\\Program Files\\Java\\*",
|
||||
&data); // Search all subdirectory
|
||||
|
||||
if (hFind != INVALID_HANDLE_VALUE) {
|
||||
do {
|
||||
wstring javaw = wstring(L"C:\\Program Files\\Java\\") + data.cFileName + wstring(L"\\bin\\javaw.exe");
|
||||
if (FindFirstFileExists(javaw.c_str(), 0)) {
|
||||
LaunchJVM(javaw, workdir, exeName);
|
||||
}
|
||||
} while (FindNextFile(hFind, &data));
|
||||
FindClose(hFind);
|
||||
}
|
||||
}
|
||||
if (hFind != INVALID_HANDLE_VALUE) {
|
||||
do {
|
||||
std::wstring javaw = std::wstring(L"C:\\Program Files\\Java\\") +
|
||||
data.cFileName + std::wstring(L"\\bin\\javaw.exe");
|
||||
if (FindFirstFileExists(javaw.c_str(), 0)) {
|
||||
LaunchJVM(javaw, workdir, exeName);
|
||||
}
|
||||
} while (FindNextFile(hFind, &data));
|
||||
FindClose(hFind);
|
||||
}
|
||||
}
|
||||
|
||||
// Consider C:\Program Files (x86)\Java
|
||||
{
|
||||
WIN32_FIND_DATA data;
|
||||
HANDLE hFind = FindFirstFile(L"C:\\Program Files (x86)\\Java\\*", &data); // Search all subdirectory
|
||||
// Consider C:\Program Files (x86)\Java
|
||||
{
|
||||
WIN32_FIND_DATA data;
|
||||
HANDLE hFind = FindFirstFile(L"C:\\Program Files (x86)\\Java\\*",
|
||||
&data); // Search all subdirectory
|
||||
|
||||
if (hFind != INVALID_HANDLE_VALUE) {
|
||||
do {
|
||||
wstring javaw = wstring(L"C:\\Program Files (x86)\\Java\\") + data.cFileName + L"\\bin\\javaw.exe";
|
||||
if (FindFirstFileExists(javaw.c_str(), 0)) {
|
||||
LaunchJVM(javaw, workdir, exeName);
|
||||
}
|
||||
} while (FindNextFile(hFind, &data));
|
||||
FindClose(hFind);
|
||||
}
|
||||
}
|
||||
if (hFind != INVALID_HANDLE_VALUE) {
|
||||
do {
|
||||
std::wstring javaw = std::wstring(L"C:\\Program Files (x86)\\Java\\") +
|
||||
data.cFileName + L"\\bin\\javaw.exe";
|
||||
if (FindFirstFileExists(javaw.c_str(), 0)) {
|
||||
LaunchJVM(javaw, workdir, exeName);
|
||||
}
|
||||
} while (FindNextFile(hFind, &data));
|
||||
FindClose(hFind);
|
||||
}
|
||||
}
|
||||
|
||||
// Try java in PATH
|
||||
RawLaunchJVM(L"javaw", workdir, exeName);
|
||||
// Try java in PATH
|
||||
RawLaunchJVM(L"javaw", workdir, exeName);
|
||||
|
||||
MessageBox(NULL, ERROR_PROMPT, L"Error", MB_ICONERROR | MB_OK);
|
||||
ShellExecute(0, 0, L"https://java.com/", 0, 0, SW_SHOW);
|
||||
return 1;
|
||||
MessageBox(NULL, ERROR_PROMPT, L"Error", MB_ICONERROR | MB_OK);
|
||||
ShellExecute(0, 0, L"https://java.com/", 0, 0, SW_SHOW);
|
||||
return 1;
|
||||
}
|
||||
|
@ -1,146 +1,128 @@
|
||||
#include "stdafx.h"
|
||||
#include "os.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
LSTATUS MyRegQueryValue(HKEY hKey, LPCWSTR subKey, DWORD dwType, wstring &out)
|
||||
{
|
||||
DWORD dwSize = 0;
|
||||
LSTATUS ret = RegQueryValueEx(hKey, subKey, 0, &dwType, NULL, &dwSize);
|
||||
if (ret != ERROR_SUCCESS) return ret;
|
||||
WCHAR *buffer = new WCHAR[dwSize];
|
||||
ret = RegQueryValueEx(hKey, subKey, 0, &dwType, (LPBYTE)buffer, &dwSize);
|
||||
if (ret != ERROR_SUCCESS) return ret;
|
||||
out = buffer;
|
||||
delete[] buffer;
|
||||
return ERROR_SUCCESS;
|
||||
LSTATUS MyRegQueryValue(HKEY hKey, LPCWSTR subKey, DWORD dwType,
|
||||
std::wstring &out) {
|
||||
DWORD dwSize = 0;
|
||||
LSTATUS ret = RegQueryValueEx(hKey, subKey, 0, &dwType, NULL, &dwSize);
|
||||
if (ret != ERROR_SUCCESS) return ret;
|
||||
WCHAR *buffer = new WCHAR[dwSize];
|
||||
ret = RegQueryValueEx(hKey, subKey, 0, &dwType, (LPBYTE)buffer, &dwSize);
|
||||
if (ret != ERROR_SUCCESS) return ret;
|
||||
out = buffer;
|
||||
delete[] buffer;
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
LSTATUS MyGetModuleFileName(HMODULE hModule, std::wstring &out)
|
||||
{
|
||||
DWORD res, size = MAX_PATH;
|
||||
out = wstring();
|
||||
out.resize(size);
|
||||
while ((res = GetModuleFileName(hModule, &out[0], size)) == size)
|
||||
{
|
||||
out.resize(size += MAX_PATH);
|
||||
}
|
||||
if (res == 0)
|
||||
return GetLastError();
|
||||
else
|
||||
{
|
||||
out.resize(size - MAX_PATH + res);
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
LSTATUS MyGetModuleFileName(HMODULE hModule, std::wstring &out) {
|
||||
DWORD res, size = MAX_PATH;
|
||||
out = std::wstring();
|
||||
out.resize(size);
|
||||
while ((res = GetModuleFileName(hModule, &out[0], size)) == size) {
|
||||
out.resize(size += MAX_PATH);
|
||||
}
|
||||
if (res == 0)
|
||||
return GetLastError();
|
||||
else {
|
||||
out.resize(size - MAX_PATH + res);
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
LSTATUS MyGetEnvironmentVariable(LPCWSTR name, std::wstring & out)
|
||||
{
|
||||
DWORD res, size = MAX_PATH;
|
||||
out = wstring();
|
||||
out.resize(size);
|
||||
while ((res = GetEnvironmentVariable(name, &out[0], size)) == size)
|
||||
{
|
||||
out.resize(size += MAX_PATH);
|
||||
}
|
||||
if (res == 0)
|
||||
return GetLastError();
|
||||
else
|
||||
{
|
||||
out.resize(size - MAX_PATH + res);
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
LSTATUS MyGetEnvironmentVariable(LPCWSTR name, std::wstring &out) {
|
||||
DWORD res, size = MAX_PATH;
|
||||
out = std::wstring();
|
||||
out.resize(size);
|
||||
while ((res = GetEnvironmentVariable(name, &out[0], size)) == size) {
|
||||
out.resize(size += MAX_PATH);
|
||||
}
|
||||
if (res == 0)
|
||||
return GetLastError();
|
||||
else {
|
||||
out.resize(size - MAX_PATH + res);
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
bool MyCreateProcess(const std::wstring &command, const std::wstring &workdir)
|
||||
{
|
||||
wstring writable_command = command;
|
||||
STARTUPINFO si;
|
||||
PROCESS_INFORMATION pi;
|
||||
si.cb = sizeof(si);
|
||||
ZeroMemory(&si, sizeof(si));
|
||||
ZeroMemory(&pi, sizeof(pi));
|
||||
bool MyCreateProcess(const std::wstring &command, const std::wstring &workdir) {
|
||||
std::wstring writable_command = command;
|
||||
STARTUPINFO si;
|
||||
PROCESS_INFORMATION pi;
|
||||
si.cb = sizeof(si);
|
||||
ZeroMemory(&si, sizeof(si));
|
||||
ZeroMemory(&pi, sizeof(pi));
|
||||
|
||||
if (workdir.empty()) {
|
||||
return CreateProcess(NULL, &writable_command[0], NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
|
||||
}
|
||||
else {
|
||||
return CreateProcess(NULL, &writable_command[0], NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, workdir.c_str(), &si, &pi);
|
||||
}
|
||||
if (workdir.empty()) {
|
||||
return CreateProcess(NULL, &writable_command[0], NULL, NULL, false,
|
||||
NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
|
||||
} else {
|
||||
return CreateProcess(NULL, &writable_command[0], NULL, NULL, false,
|
||||
NORMAL_PRIORITY_CLASS, NULL, workdir.c_str(), &si,
|
||||
&pi);
|
||||
}
|
||||
}
|
||||
|
||||
bool FindFirstFileExists(LPCWSTR lpPath, DWORD dwFilter)
|
||||
{
|
||||
WIN32_FIND_DATA fd;
|
||||
HANDLE hFind = FindFirstFile(lpPath, &fd);
|
||||
bool bFilter = (false == dwFilter) ? true : fd.dwFileAttributes & dwFilter;
|
||||
bool ret = ((hFind != INVALID_HANDLE_VALUE) && bFilter) ? true : false;
|
||||
FindClose(hFind);
|
||||
return ret;
|
||||
bool FindFirstFileExists(LPCWSTR lpPath, DWORD dwFilter) {
|
||||
WIN32_FIND_DATA fd;
|
||||
HANDLE hFind = FindFirstFile(lpPath, &fd);
|
||||
bool bFilter = (false == dwFilter) ? true : fd.dwFileAttributes & dwFilter;
|
||||
bool ret = ((hFind != INVALID_HANDLE_VALUE) && bFilter) ? true : false;
|
||||
FindClose(hFind);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool GetArch(bool & is64Bit)
|
||||
{
|
||||
bool GetArch(bool &is64Bit) {
|
||||
#if _WIN64
|
||||
is64Bit = true;
|
||||
return true;
|
||||
is64Bit = true;
|
||||
return true;
|
||||
#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.
|
||||
// Use GetModuleHandle to get a handle to the DLL that contains the function
|
||||
// and GetProcAddress to get a pointer to the function if available.
|
||||
// IsWow64Process is not available on all supported versions of Windows.
|
||||
// Use GetModuleHandle to get a handle to the DLL that contains the function
|
||||
// and GetProcAddress to get a pointer to the function if available.
|
||||
|
||||
LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)
|
||||
GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
|
||||
LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
|
||||
GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
|
||||
|
||||
if (fnIsWow64Process)
|
||||
{
|
||||
if (!fnIsWow64Process(GetCurrentProcess(), &isWow64))
|
||||
return false;
|
||||
if (fnIsWow64Process) {
|
||||
if (!fnIsWow64Process(GetCurrentProcess(), &isWow64)) return false;
|
||||
|
||||
is64Bit = isWow64;
|
||||
return true;
|
||||
}
|
||||
else // IsWow64Process is not supported, fail to detect.
|
||||
return false;
|
||||
is64Bit = isWow64;
|
||||
return true;
|
||||
} else // IsWow64Process is not supported, fail to detect.
|
||||
return false;
|
||||
|
||||
#else
|
||||
#error _WIN64 and _WIN32 are both undefined.
|
||||
#endif
|
||||
}
|
||||
|
||||
bool MyGetFileVersionInfo(const std::wstring & filePath, Version &version)
|
||||
{
|
||||
DWORD verHandle = 0;
|
||||
UINT size = 0;
|
||||
LPBYTE lpBuffer = NULL;
|
||||
VS_FIXEDFILEINFO *pFileInfo;
|
||||
DWORD dwSize = GetFileVersionInfoSize(filePath.c_str(), NULL);
|
||||
bool MyGetFileVersionInfo(const std::wstring &filePath, Version &version) {
|
||||
DWORD verHandle = 0;
|
||||
UINT size = 0;
|
||||
LPBYTE lpBuffer = NULL;
|
||||
VS_FIXEDFILEINFO *pFileInfo;
|
||||
DWORD dwSize = GetFileVersionInfoSize(filePath.c_str(), NULL);
|
||||
|
||||
if (!dwSize)
|
||||
return false;
|
||||
if (!dwSize) return false;
|
||||
|
||||
LPBYTE data = new BYTE[dwSize];
|
||||
if (!GetFileVersionInfo(filePath.c_str(), 0, dwSize, data))
|
||||
{
|
||||
delete[] data;
|
||||
return false;
|
||||
}
|
||||
LPBYTE data = new BYTE[dwSize];
|
||||
if (!GetFileVersionInfo(filePath.c_str(), 0, dwSize, data)) {
|
||||
delete[] data;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!VerQueryValue(data, TEXT("\\"), (LPVOID*)&pFileInfo, &size))
|
||||
{
|
||||
delete[] data;
|
||||
return false;
|
||||
}
|
||||
if (!VerQueryValue(data, TEXT("\\"), (LPVOID *)&pFileInfo, &size)) {
|
||||
delete[] data;
|
||||
return false;
|
||||
}
|
||||
|
||||
version = Version{
|
||||
(pFileInfo->dwFileVersionMS >> 16) & 0xFFFF,
|
||||
(pFileInfo->dwFileVersionMS >> 0) & 0xFFFF,
|
||||
(pFileInfo->dwFileVersionLS >> 16) & 0xFFFF,
|
||||
(pFileInfo->dwFileVersionLS >> 0) & 0xFFFF
|
||||
};
|
||||
return true;
|
||||
version = Version{(pFileInfo->dwFileVersionMS >> 16) & 0xFFFF,
|
||||
(pFileInfo->dwFileVersionMS >> 0) & 0xFFFF,
|
||||
(pFileInfo->dwFileVersionLS >> 16) & 0xFFFF,
|
||||
(pFileInfo->dwFileVersionLS >> 0) & 0xFFFF};
|
||||
return true;
|
||||
}
|
||||
|
@ -6,17 +6,20 @@
|
||||
const int MAX_KEY_LENGTH = 255;
|
||||
const int MAX_VALUE_NAME = 16383;
|
||||
|
||||
// Query registry value of class root hKey, key path subKey, stores result in parameter out.
|
||||
LSTATUS MyRegQueryValue(HKEY hKey, LPCWSTR subKey, DWORD dwType, std::wstring &out);
|
||||
// Query registry value of class root hKey, key path subKey, stores result in
|
||||
// parameter out.
|
||||
LSTATUS MyRegQueryValue(HKEY hKey, LPCWSTR subKey, DWORD dwType,
|
||||
std::wstring &out);
|
||||
|
||||
// Get module file name, stores result in parameter 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);
|
||||
|
||||
// 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.
|
||||
bool FindFirstFileExists(LPCWSTR lpPath, DWORD dwFilter);
|
||||
|
Loading…
Reference in New Issue
Block a user