我需要啓動一個進程,然後立即將其分解,以便可以使用Visual Studio進行調試。 我在網上搜索了一些小部分後,寫了這段代碼。代碼不起作用。這個過程被啓動,但不會中斷,調用代碼會無限期地等待。如果我不以暫停模式啓動進程,它會立即運行並退出。以編程方式打破已啓動的進程
我無法修改我啓動的.exe代碼。我只有.exe和符號文件。
代碼:
#include<iostream>
#include<Windows.h>
using namespace std;
int main(int argc, char **argv)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
LPWSTR commandLine;
int commandLength;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (argc != 2)
{
cout << "Usage: Launcher <commandline>" << endl;
return 1;
}
commandLength = MultiByteToWideChar(CP_UTF8, 0, argv[1], -1, NULL, 0);
commandLine = new WCHAR[commandLength];
MultiByteToWideChar(CP_UTF8, 0, argv[1], -1, commandLine, commandLength);
if (!CreateProcess(NULL, commandLine, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi))
{
cout << "CreateProcess failed (" << GetLastError() << ")." << endl;
delete[] commandLine;
return 1;
}
cout << pi.dwProcessId << " " << pi.dwThreadId << endl;
delete[] commandLine;
DebugBreakProcess(pi.hProcess);
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
我在做什麼錯在這裏?
編輯:這是泰森建議後的代碼。
#include<iostream>
#include<Windows.h>
using namespace std;
int main(int argc, char **argv)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
LPWSTR commandLine;
int commandLength;
HANDLE processHandle;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (argc != 2)
{
cout << "Usage: Launcher <commandline>" << endl;
return 1;
}
commandLength = MultiByteToWideChar(CP_UTF8, 0, argv[1], -1, NULL, 0);
commandLine = new WCHAR[commandLength];
MultiByteToWideChar(CP_UTF8, 0, argv[1], -1, commandLine, commandLength);
if (!CreateProcess(NULL, commandLine, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi))
{
cout << "CreateProcess failed (" << GetLastError() << ")." << endl;
delete[] commandLine;
return 1;
}
delete[] commandLine;
processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pi.dwProcessId);
if (processHandle == NULL)
{
cout << "Could not obtain handle (" << GetLastError() << ")." << endl;
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 2;
}
DebugActiveProcess(pi.dwProcessId);
//ResumeThread(pi.hThread);
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(processHandle);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
這個問題呢?你能從IInspectable的建議中獲得有用的信息嗎?其實我同意他的建議,自動啓動調試器將是一個更好的解決方法。 –