Last active
October 29, 2020 15:28
-
-
Save qis/5e4c4fc5467ee37a2becd698957ff05b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <filesystem> | |
#include <iostream> | |
#include <sstream> | |
#include <string> | |
int main(int argc, char* argv[]) { | |
// Report command and parameter lengths. | |
if (argc > 1) { | |
std::cout | |
<< std::string_view{ argv[0] }.size() << " + " | |
<< std::string_view{ argv[1] }.size() << std::endl; | |
return 0; | |
} | |
// Get module file name as UTF-8 string. | |
DWORD size = 0; | |
std::string str; | |
do { | |
str.resize(str.size() + MAX_PATH); | |
size = GetModuleFileName(nullptr, &str[0], static_cast<DWORD>(str.size())); | |
} while (GetLastError() == ERROR_INSUFFICIENT_BUFFER); | |
str.resize(size); | |
const auto m = std::filesystem::canonical(str).string(); | |
// Create quoted command. | |
std::ostringstream oss; | |
oss << std::quoted(m) << ' '; | |
auto c = oss.str(); | |
// Generate maximum length parameter. | |
c.append(std::string(32'767 - c.size() - 1, 'Q')); | |
// Execute command with generated parameter.. | |
STARTUPINFO si = {}; | |
si.cb = sizeof(si); | |
PROCESS_INFORMATION pi = {}; | |
CreateProcess(m.data(), c.data(), nullptr, nullptr, TRUE, 0, nullptr, nullptr, &si, &pi); | |
WaitForSingleObject(pi.hProcess, INFINITE); | |
CloseHandle(pi.hProcess); | |
CloseHandle(pi.hThread); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment