-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdllmain.cpp
More file actions
137 lines (130 loc) · 4.79 KB
/
dllmain.cpp
File metadata and controls
137 lines (130 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "pch.h"
#include <Windows.h>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <Psapi.h>
#include <tchar.h>
#include <algorithm>
#include <stdio.h>
#pragma comment(lib, "Psapi.lib")
#pragma comment(lib, "Version.lib")
std::string ReadEntireFile(const std::string& filePath) {
std::ifstream file(filePath, std::ios::binary);
if (!file) return "";
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
bool FileExists(const std::string& filePath) {
DWORD attrib = GetFileAttributesA(filePath.c_str());
return (attrib != INVALID_FILE_ATTRIBUTES && !(attrib & FILE_ATTRIBUTE_DIRECTORY));
}
std::string GetVersionFromResource() {
char modulePath[MAX_PATH] = { 0 };
if (!GetModuleFileNameA(NULL, modulePath, MAX_PATH))
return "";
DWORD dummy;
DWORD size = GetFileVersionInfoSizeA(modulePath, &dummy);
if (size == 0)
return "";
std::vector<char> data(size);
if (!GetFileVersionInfoA(modulePath, 0, size, data.data()))
return "";
VS_FIXEDFILEINFO* fileInfo = nullptr;
UINT len = 0;
if (VerQueryValueA(data.data(), "\\", (LPVOID*)&fileInfo, &len) && fileInfo) {
int major = HIWORD(fileInfo->dwFileVersionMS);
int minor = LOWORD(fileInfo->dwFileVersionMS);
int build = HIWORD(fileInfo->dwFileVersionLS);
int revision = LOWORD(fileInfo->dwFileVersionLS);
char versionStr[128];
sprintf_s(versionStr, "%d.%d.%d.%d", major, minor, build, revision);
return versionStr;
}
return "";
}
std::string GetVersionFromFiles() {
char modulePath[MAX_PATH] = { 0 };
if (!GetModuleFileNameA(NULL, modulePath, MAX_PATH))
return "";
std::string exePath(modulePath);
size_t lastSlash = exePath.find_last_of("\\/");
std::string exeDir;
if (lastSlash != std::string::npos)
exeDir = exePath.substr(0, lastSlash);
std::vector<std::string> candidates;
candidates.push_back(exeDir + "\\Engine\\Build\\Build.version");
candidates.push_back(exeDir + "\\UE4Version.txt");
candidates.push_back(exeDir + "\\UE5Version.txt");
size_t parentSlash = exeDir.find_last_of("\\/");
if (parentSlash != std::string::npos) {
std::string parentDir = exeDir.substr(0, parentSlash);
candidates.push_back(parentDir + "\\Engine\\Build\\Build.version");
candidates.push_back(parentDir + "\\UE4Version.txt");
candidates.push_back(parentDir + "\\UE5Version.txt");
}
for (auto& path : candidates) {
if (FileExists(path)) {
std::string content = ReadEntireFile(path);
if (!content.empty()) {
content.erase(std::remove(content.begin(), content.end(), '\r'), content.end());
content.erase(std::remove(content.begin(), content.end(), '\n'), content.end());
return content;
}
}
}
return "";
}
std::string GetVersionFromMemoryScan() {
HMODULE hModule = GetModuleHandleA(NULL);
if (!hModule)
return "";
MODULEINFO modInfo = { 0 };
if (!GetModuleInformation(GetCurrentProcess(), hModule, &modInfo, sizeof(modInfo)))
return "";
char* baseAddr = reinterpret_cast<char*>(modInfo.lpBaseOfDll);
size_t moduleSize = modInfo.SizeOfImage;
if (!baseAddr || moduleSize == 0)
return "";
std::vector<std::string> markers = { "Unreal Engine 4.", "Unreal Engine 5.", "FEngineVersion", "EngineVersion" };
for (const auto& marker : markers) {
size_t markerLen = marker.length();
for (size_t i = 0; i < moduleSize - markerLen; i++) {
if (memcmp(baseAddr + i, marker.c_str(), markerLen) == 0) {
std::string found(marker);
size_t maxExtra = 32;
size_t j = i + markerLen;
while (j < moduleSize && (j - (i + markerLen)) < maxExtra && isprint(baseAddr[j])) {
found.push_back(baseAddr[j]);
j++;
}
return found;
}
}
}
return "";
}
std::string GetUnrealEngineVersion() {
std::string result;
result = GetVersionFromResource();
if (!result.empty())
return result;
result = GetVersionFromFiles();
if (!result.empty())
return result;
result = GetVersionFromMemoryScan();
if (!result.empty())
return result;
return "Unknown";
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
DisableThreadLibraryCalls(hModule);
std::string versionInfo = GetUnrealEngineVersion();
std::string msg = "Unreal Engine Version is: " + versionInfo;
MessageBoxA(nullptr, msg.c_str(), "Unreal Engine Version", MB_OK | MB_ICONINFORMATION);
}
return TRUE;
}