-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWindowsFunctions.h
414 lines (349 loc) · 11.3 KB
/
WindowsFunctions.h
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#pragma once
#include <string>
#include <windows.h>
#include <wininet.h>
#include <fstream>
#include <TlHelp32.h>
#include "deps/minizip/unzip.h"
#pragma comment(lib, "wininet.lib")
namespace WindowsFunctions
{
static void ForceForegroundWindow(HWND focusOnWindowHandle)
{
int style = GetWindowLong(focusOnWindowHandle, GWL_STYLE);
// Minimize and restore to be able to make it active.
if ((style & WS_MINIMIZE) == WS_MINIMIZE)
ShowWindow(focusOnWindowHandle, SW_RESTORE);
auto currentlyFocusedWindowProcessId = GetWindowThreadProcessId(GetForegroundWindow(), LPDWORD(0));
auto appThread = GetCurrentThreadId();
if (currentlyFocusedWindowProcessId != appThread)
{
AttachThreadInput(currentlyFocusedWindowProcessId, appThread, true);
BringWindowToTop(focusOnWindowHandle);
AttachThreadInput(currentlyFocusedWindowProcessId, appThread, false);
}
else
{
BringWindowToTop(focusOnWindowHandle);
}
}
static bool Unzip(const std::string &filepath, std::vector<std::string> &output)
{
unzFile zipFile = unzOpen(filepath.c_str());
if (!zipFile)
{
// Failed to open the zip file
return false;
}
unz_global_info globalInfo;
if (unzGetGlobalInfo(zipFile, &globalInfo) != UNZ_OK)
{
unzClose(zipFile);
return false;
}
char buffer[4096]; // Adjust buffer size as needed
for (uLong i = 0; i < globalInfo.number_entry; ++i)
{
char filename[256];
unz_file_info fileInfo;
if (unzGetCurrentFileInfo(zipFile, &fileInfo, filename, sizeof(filename), NULL, 0, NULL, 0) != UNZ_OK)
{
unzClose(zipFile);
return false;
}
std::filesystem::path fullOutputPath = std::filesystem::path(filepath).parent_path() / filename;
if (filename[strlen(filename) - 1] == '/')
{
// It's a directory
std::filesystem::create_directories(fullOutputPath);
}
else
{
// It's a file
if (unzOpenCurrentFile(zipFile) != UNZ_OK)
{
unzClose(zipFile);
return false;
}
std::filesystem::create_directories(fullOutputPath.parent_path());
std::ofstream outFile(fullOutputPath, std::ios::binary);
int error = UNZ_OK;
do
{
error = unzReadCurrentFile(zipFile, buffer, sizeof(buffer));
if (error < 0)
{
unzCloseCurrentFile(zipFile);
unzClose(zipFile);
return false;
}
if (error > 0)
{
outFile.write(buffer, error);
}
} while (error > 0);
outFile.close();
auto fpstr = fullOutputPath.string();
// Use '\' for all just because
for (size_t i = 0; i < fpstr.size(); ++i)
{
if (fpstr[i] == '/')
fpstr[i] = '\\';
}
output.push_back(fpstr);
if (unzCloseCurrentFile(zipFile) != UNZ_OK)
{
unzClose(zipFile);
return false;
}
}
if ((i + 1) < globalInfo.number_entry)
{
if (unzGoToNextFile(zipFile) != UNZ_OK)
{
unzClose(zipFile);
return false;
}
}
}
unzClose(zipFile);
return true;
}
static bool DownloadFile(const std::string &url, const std::string &filename)
{
HINTERNET connect = InternetOpenA("Streamlabs", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!connect)
return false;
HINTERNET hOpenAddress = InternetOpenUrlA(connect, url.c_str(), NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
if (!hOpenAddress)
{
InternetCloseHandle(connect);
return false;
}
DWORD contentLength;
DWORD contentLengthSize = sizeof(contentLength);
if (!HttpQueryInfo(hOpenAddress, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &contentLength, &contentLengthSize, NULL))
{
//blog(LOG_ERROR, "DownloadFile: HttpQueryInfo failed, last error = %d\n", GetLastError());
InternetCloseHandle(hOpenAddress);
InternetCloseHandle(connect);
return false;
}
char dataReceived[4096];
DWORD numberOfBytesRead = 0;
std::ofstream outFile(filename, std::ios::binary);
if (!outFile.is_open())
{
//blog(LOG_ERROR, "DownloadFile: Could not open std::ofstream outFile, last error = %d\n", GetLastError());
InternetCloseHandle(hOpenAddress);
InternetCloseHandle(connect);
return false;
}
DWORD totalBytesRead = 0;
while (InternetReadFile(hOpenAddress, dataReceived, 4096, &numberOfBytesRead) && numberOfBytesRead > 0)
{
outFile.write(dataReceived, numberOfBytesRead);
totalBytesRead += numberOfBytesRead;
}
outFile.close();
InternetCloseHandle(hOpenAddress);
InternetCloseHandle(connect);
if (totalBytesRead != contentLength)
{
//blog(LOG_ERROR, "DownloadFile: Incomplete download, last error = %d\n", GetLastError());
std::remove(filename.c_str());
return false;
}
return true;
};
static int HTTPRequest(const std::string &a_URI, const std::string &a_method, const std::string &a_headers, DWORD *a_pOutHttpCode, DWORD a_nTimeoutMS, std::string &response, const std::string &a_body, const std::string &a_headerAcceptType)
{
DWORD nValue = 128;
InternetSetOption(NULL, INTERNET_OPTION_MAX_CONNS_PER_SERVER, (LPVOID)&nValue, sizeof(nValue));
DWORD retVal = ERROR_SUCCESS;
// Parse URI
URL_COMPONENTSA uri;
uri.dwStructSize = sizeof(uri);
uri.dwSchemeLength = 1;
uri.dwHostNameLength = 1;
uri.dwUserNameLength = 1;
uri.dwPasswordLength = 1;
uri.dwUrlPathLength = 1;
uri.dwExtraInfoLength = 1;
uri.lpszScheme = NULL;
uri.lpszHostName = NULL;
uri.lpszUserName = NULL;
uri.lpszPassword = NULL;
uri.lpszUrlPath = NULL;
uri.lpszExtraInfo = NULL;
if (BOOL isProperURI = ::InternetCrackUrlA(a_URI.c_str(), (DWORD)a_URI.length(), 0, &uri))
{
std::string scheme(uri.lpszScheme, uri.dwSchemeLength);
std::string serverName(uri.lpszHostName, uri.dwHostNameLength);
std::string object(uri.lpszUrlPath, uri.dwUrlPathLength);
std::string extraInfo(uri.lpszExtraInfo, uri.dwExtraInfoLength);
std::string username(uri.lpszUserName, uri.dwUserNameLength);
std::string password(uri.lpszPassword, uri.dwPasswordLength);
// Open HTTP resource
if (HINTERNET handle = ::InternetOpenA("HTTP", INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0))
{
// Setup timeout
if (::InternetSetOptionA(handle, INTERNET_OPTION_CONNECT_TIMEOUT, &a_nTimeoutMS, sizeof(DWORD)))
{
// Connect to server
if (HINTERNET session = ::InternetConnectA(handle, serverName.c_str(), uri.nPort, username.c_str(), password.c_str(), INTERNET_SERVICE_HTTP, 0, 0))
{
// Request object
DWORD dwFlags = INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI |
INTERNET_FLAG_RELOAD;
if (scheme == "https")
{
dwFlags |= INTERNET_FLAG_SECURE;
// dwFlags |= INTERNET_FLAG_KEEP_CONNECTION; Not needed for independent calls
}
// Define the Accept Types
std::string sHeaders = a_headers;
LPCSTR rgpszAcceptTypes[] = {sHeaders.c_str(), NULL};
if (HINTERNET h_istream = ::HttpOpenRequestA(session, a_method.c_str(), (object + extraInfo).c_str(), NULL, NULL, rgpszAcceptTypes, dwFlags, 0))
{
// Send attached data, if any
BOOL requestOk = TRUE;
// Append input headers
if (a_headers.length() > 0)
requestOk = ::HttpAddRequestHeadersA(h_istream, a_headers.c_str(), (DWORD)a_headers.length(), HTTP_ADDREQ_FLAG_ADD);
// Ignore certificate verification
DWORD nFlags;
DWORD nBuffLen = sizeof(nFlags);
::InternetQueryOptionA(h_istream, INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&nFlags, &nBuffLen);
nFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
::InternetSetOptionA(h_istream, INTERNET_OPTION_SECURITY_FLAGS, &nFlags, sizeof(nFlags));
// Send
if (a_body.size() > 0)
{
if (requestOk)
requestOk = ::HttpSendRequestA(h_istream, NULL, 0, (void *)a_body.data(), (DWORD)a_body.size());
}
else if (requestOk)
{
requestOk = ::HttpSendRequestA(h_istream, NULL, 0, NULL, 0);
}
// Validate
if (requestOk)
{
// Query HTTP status code
if (a_pOutHttpCode != nullptr)
{
DWORD dwStatusCode = 0;
DWORD dwSize = sizeof(dwStatusCode);
*a_pOutHttpCode = 0;
if (HttpQueryInfo(h_istream, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatusCode, &dwSize, NULL))
*a_pOutHttpCode = dwStatusCode;
}
// Read response
static const DWORD SIZE = 4 * 1024;
BYTE data[SIZE];
DWORD size = 0;
// Download
do
{
// Read chunk of bytes
BOOL result = ::InternetReadFile(h_istream, data, SIZE, &size);
// Error check
if (result)
{
// Write data read
if (size > 0)
response.append((char *)data, size);
}
else
{
retVal = GetLastError();
}
} while (retVal == ERROR_SUCCESS && size > 0);
}
else
{
retVal = GetLastError();
}
// Close handles
::InternetCloseHandle(h_istream);
}
else
{
retVal = GetLastError();
}
::InternetCloseHandle(session);
}
else
{
retVal = GetLastError();
}
}
else
{
retVal = GetLastError();
}
// Close internet handle
::InternetCloseHandle(handle);
}
else
{
retVal = GetLastError();
}
}
else
{
retVal = GetLastError();
retVal = ERROR_INVALID_DATA;
}
return retVal;
}
static bool InstallFont(const char *fontPath)
{
if (AddFontResourceA(fontPath))
{
PostMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
return true;
}
return false;
}
static bool KillProcess(DWORD processId)
{
// Then terminate the main process
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, processId);
if (hProcess == NULL)
return false;
BOOL result = TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
return (result != FALSE);
}
static bool LaunchProcess(const std::wstring &filePath)
{
// Extract directory from filePath
std::wstring directory = filePath.substr(0, filePath.find_last_of(L"\\/"));
// Initialize STARTUPINFO and PROCESS_INFORMATION
STARTUPINFOW si = {sizeof(si)};
PROCESS_INFORMATION pi = {};
// Create a modifiable copy of filePath for CreateProcessW
std::wstring modifiableFilePath = filePath;
// Launch the process
if (!CreateProcessW(nullptr, // Application name
&modifiableFilePath[0], // Command line (modifiable)
nullptr, // Process handle not inheritable
nullptr, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
nullptr, // Use parent's environment block
directory.empty() ? nullptr : directory.c_str(), // Set working directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure
))
{
return false;
}
// Close the process and thread handles
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return true;
}
}