所以,我不能改变调用一个可执行文件,我需要给它不同的命令行参数比黑盒代码调用。我在想我可以制作一个可执行文件作为代理。 proc.exe
位于黑盒部分所期望的位置,接受命令行参数,修改它们,然后调用原始文件procReal.exe
。CreateProcess失败 - 错误183
不幸的是,CreateProcess似乎无法启动,返回状态183。我查了一切,我无法找到这个。已经尝试翻转事物,将句柄继承设置为true,手动指定工作目录,而不是执行其中任何一项。没有运气。我以为有一些事情在这里的继承调用应用,使得包装作品适当直通的适当的安全上下文,但我想不出该怎么办呢?下面
代码,不相关的部分修剪。
编辑将请求后的完整代码放在这里。这没有任何意义了。它现在将部分工作,但仅限traceFile
的fopen
部分不存在。甚至没有删除fprintfs,特别是整个部分不得不被删除。
我试着回复所有人的评论,我认为我已经排除了大部分这些问题,但是我留下了当前的异常行为。我可以从这个角度再读一点,就是说某些形式的字符串复制可能导致内存溢出,这可能吗?
#include <iostream>
#include <fstream>
#include <windows.h>
#include <string>
#include <vector>
#include <stdio.h>
#include <tchar.h>
#include <algorithm>
#include <string>
using namespace std;
bool caseInsensitiveStringCompare(const std::string& str1, const std::string& str2);
int main(int argc, char* argv[]) {
const string path = "E:\\util\\bin\\";
const string procName = "procReal.exe";
const string argToFilter = "-t";
string origValue;
string passedValue;
for(int i = 1; i < argc; i++)
{
origValue.append(" ").append(argv[i]);
}
for(int i = 1; i < argc; i++)
{
if (!caseInsensitiveStringCompare(argv[i],argToFilter))
{
passedValue.append(" ").append(argv[i]);
}
else
{
i++; // skip over argument and it's value
}
}
const LPCTSTR exeModule = (path+procName).c_str();
std::vector<char> chars(passedValue.c_str(), passedValue.c_str() + passedValue.size() + 1u);
LPTSTR exeArgs = &chars[0];
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
GetStartupInfo(&si);
FILE* traceFile;
traceFile = fopen ((path+"lastRun.txt").c_str(), "w"); // This causes exeModule to change value for unknown reasons???
fprintf(traceFile, "orig: %s%s\n", exeModule, origValue.c_str());
fprintf(traceFile, "%s%s\n", exeModule, exeArgs);
SetLastError(0);
// Start the child process.
if(!CreateProcess(exeModule, // use module name with args for exeArgs
exeArgs, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
TRUE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
FILE* myfile;
myfile = fopen ((path+"error.txt").c_str(), "w");
fprintf(myfile, "CreateProcess failed (%d).\n", int(GetLastError()));
fclose(myfile);
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exit_code;
GetExitCodeProcess(pi.hProcess, &exit_code);
fprintf(traceFile, "Exit Code: %d\n", int(exit_code));
fclose(traceFile);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return exit_code;
}
bool caseInsensitiveStringCompare(const std::string& str1, const std::string& str2) {
std::string str1Cpy(str1);
std::string str2Cpy(str2);
std::transform(str1Cpy.begin(), str1Cpy.end(), str1Cpy.begin(), ::tolower);
std::transform(str2Cpy.begin(), str2Cpy.end(), str2Cpy.begin(), ::tolower);
return (str1Cpy == str2Cpy);
}
错误代码183名的意思是“ERROR_ALREADY_EXISTS” –
是您的计算机上启用UAC?难道procReal.exe需要管理员权限?是否proc.exe有呢?你有没有尝试运行它UAC禁用? – z32a7ul
基本过程可以直接运行而不需要管理员权限leges,是的。 – Vigilant