Skip to content

Instantly share code, notes, and snippets.

@dikarimov
Created March 13, 2025 14:41
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <thread>
#include <windows.h>
// Function to get the current timestamp as a string
std::string getCurrentTimestamp() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
localtime_s(&tstruct, &now);
strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);
return std::string(buf);
}
// Function to log the call and arguments
void logCall(const std::wstring& args) {
std::ofstream logFile("C:\\astc\\astcenc_wrapper.log", std::ios::app); // Open in append mode
if (logFile.is_open()) {
logFile << "[" << getCurrentTimestamp() << "] Called with arguments: " << std::string(args.begin(), args.end()) << std::endl;
logFile.close();
}
else {
std::cerr << "Failed to open log file!" << std::endl;
}
}
int wmain(int argc, wchar_t* argv[]) {
// Log the call and arguments
std::wstring arguments;
for (int i = 1; i < argc; ++i) {
arguments += argv[i];
if (i < argc - 1) {
arguments += L" ";
}
}
auto avail_threads = max(1, std::thread::hardware_concurrency()-2);
int affine = 1;
if (avail_threads > 1) {
affine = 0;
while (avail_threads-- > 0) {
affine <<= 1;
affine |= 1;
}
affine <<= 2;
}
wchar_t affineHex[10]{};
swprintf_s(affineHex, 10, L"%x", affine);
//std::wcout << affineHex << std::endl;
logCall(arguments);
// Prepare the command to call the original astcenc.exe
std::wstring command = L"C:\\windows\\system32\\cmd.exe /C start /wait /b /affinity ";
command += affineHex;
command += L" astcenc_original.exe " + arguments;
//std::wcout << command << std::endl;
// Execute the original astcenc.exe with the same arguments
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
if (CreateProcess(
nullptr, // No module name (use command line)
&command[0], // Command line (wide-character string)
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
nullptr, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure
)) {
// Wait until the process exits
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else {
logCall(L"Failed to start astcenc_original.exe!");
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment