Repository navigation
Expand file tree
/
Copy pathUtils.cpp
More file actions
202 lines (174 loc) · 5.46 KB
/
Copy pathUtils.cpp
File metadata and controls
202 lines (174 loc) · 5.46 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "Utils.h"
BOOL CopyAndPatchFile(std::wstring srcPath, std::wstring dstPath)
{
if (!CopyFileW(srcPath.c_str(), dstPath.c_str(), FALSE))
{
std::wcerr << L"CopyFileW failed with error: " << GetLastError() << std::endl;
return false;
}
HANDLE hFile = CreateFileW(dstPath.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
{
std::wcerr << L"CreateFileW failed with error: " << GetLastError() << std::endl;
return false;
}
DWORD fileSize = GetFileSize(hFile, nullptr);
if (fileSize == INVALID_FILE_SIZE)
{
CloseHandle(hFile);
return false;
}
BYTE* buffer = new BYTE[fileSize];
DWORD bytesRead = 0;
if (!ReadFile(hFile, buffer, fileSize, &bytesRead, nullptr))
{
delete[] buffer;
CloseHandle(hFile);
return false;
}
const char* target = "This program cannot be run in DOS mode";
for (DWORD i = 0; i < fileSize - strlen(target); ++i)
{
if (memcmp(buffer + i, target, strlen(target)) == 0)
{
buffer[i] = 'H'; // Replace 'T' with 'H'
break;
}
}
SetFilePointer(hFile, 0, nullptr, FILE_BEGIN);
DWORD bytesWritten = 0;
if (!WriteFile(hFile, buffer, fileSize, &bytesWritten, nullptr))
{
delete[] buffer;
CloseHandle(hFile);
return false;
}
delete[] buffer;
CloseHandle(hFile);
return true;
}
BOOL IsProcessRunning(std::wstring processName)
{
bool found = false;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) return false;
PROCESSENTRY32W pe32;
pe32.dwSize = sizeof(PROCESSENTRY32W);
if (Process32FirstW(hSnapshot, &pe32))
{
do
{
if (_wcsicmp(pe32.szExeFile, processName.c_str()) == 0)
{
found = true;
break;
}
} while (Process32NextW(hSnapshot, &pe32));
}
CloseHandle(hSnapshot);
return found;
}
BOOL CreateNewService(std::wstring serviceName, std::wstring displayName, std::wstring groupName, std::wstring imagePath)
{
SC_HANDLE hSCM = OpenSCManagerW( nullptr, nullptr, SC_MANAGER_CREATE_SERVICE);
if (!hSCM)
{
std::wcerr << L"OpenSCManager failed, error: " << GetLastError() << std::endl;
return false;
}
SC_HANDLE hService = CreateServiceW(
hSCM,
serviceName.c_str(), // internal service name
displayName.c_str(), // display name
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, // auto start
SERVICE_ERROR_NORMAL,
imagePath.c_str(), // binary path
groupName.c_str(), // load order group
nullptr, // tag ID
nullptr, // dependencies
L"LocalSystem", // run as SYSTEM
nullptr // no password
);
if (!hService)
{
std::wcerr << L"CreateService failed, error: " << GetLastError() << std::endl;
CloseServiceHandle(hSCM);
return false;
}
std::wcout << L"Service created successfully!" << std::endl;
CloseServiceHandle(hService);
CloseServiceHandle(hSCM);
return true;
}
BOOL IsRunningAsService()
{
DWORD sessionId = 0;
if (ProcessIdToSessionId(GetCurrentProcessId(), &sessionId))
{
if (sessionId == 0)
{
return true; // Session 0 → likely a service
}
}
return false;
}
BOOL EnsureDirectoryExists(std::wstring fullFilePath)
{
// Extract directory part from full file path
size_t pos = fullFilePath.find_last_of(L"\\/");
if (pos == std::wstring::npos)
{
std::wcerr << L"Invalid file path: " << fullFilePath << std::endl;
return false;
}
std::wstring dirPath = fullFilePath.substr(0, pos);
// Create all intermediate directories
int result = SHCreateDirectoryExW(nullptr, dirPath.c_str(), nullptr);
if (result == ERROR_SUCCESS || result == ERROR_ALREADY_EXISTS)
{
return true; // directory created or already exists
}
std::wcerr << L"SHCreateDirectoryExW failed, error: " << result << std::endl;
return false;
}
std::wstring GetCurrentProcessPath()
{
wchar_t buffer[MAX_PATH];
DWORD length = GetModuleFileNameW(nullptr, buffer, MAX_PATH);
if (length == 0)
{
std::wcerr << L"GetModuleFileNameW failed, error: " << GetLastError() << std::endl;
return L"";
}
return std::wstring(buffer, length);
}
std::wstring GetCurrentProcessDirectoryOnly()
{
wchar_t buffer[MAX_PATH];
DWORD length = GetModuleFileNameW(nullptr, buffer, MAX_PATH);
if (length == 0) {
std::wcerr << L"GetModuleFileNameW failed, error: " << GetLastError() << std::endl;
return L"";
}
std::wstring fullPath(buffer, length);
// Find last backslash
size_t pos = fullPath.find_last_of(L"\\/");
if (pos == std::wstring::npos) {
return L""; // no directory part
}
return fullPath.substr(0, pos);
}
VOID AppendLog(std::wstring logEntry)
{
std::wofstream logFile;
logFile.open(GetCurrentProcessDirectoryOnly() + L"\\RunLog.txt", std::ios::app);
if (!logFile.is_open())
{
std::wcerr << L"Failed to open RunLog.txt for writing." << std::endl;
return;
}
logFile << logEntry << std::endl;
logFile.close();
}