所以,我不能改變調用一個可執行文件,我需要給它不同的命令行參數比黑盒代碼調用。我在想我可以製作一個可執行文件作爲代理。 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