高手救命:用writefile向gdb写命令为什么它不执行?
只是个实验版本:
CString tmp;
tmp = szFilename;//路径加文件名
HANDLE hRead = NULL;
HANDLE hWrite = NULL;
SECURITY_ATTRIBUTES sad;
STARTUPINFO suid;
PROCESS_INFORMATION pid;
sad.bInheritHandle = TRUE;
sad.lpSecurityDescriptor = NULL;
sad.nLength = sizeof(SECURITY_ATTRIBUTES);
if(NULL == hRead || NULL == hWrite )
{
if(!CreatePipe(&hRead,&hWrite,&sad,0))//创建匿名管道
{
AfxMessageBox("创建匿名管道失败");
Complying = 0;
return -1;
}
}
ZeroMemory(&suid,sizeof(STARTUPINFO));
suid.cb = sizeof(STARTUPINFO);
GetStartupInfo(&suid);
suid.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
suid.wShowWindow = SW_SHOW;
suid.hStdInput = /*GetStdHandle(STD_INPUT_HANDLE);//*/hRead;
suid.hStdOutput = /*GetStdHandle(STD_OUTPUT_HANDLE);//*/hWrite;
suid.hStdError = /*GetStdHandle(STD_ERROR_HANDLE);//*/hWrite;
tmp = "D:\\MinGWStudio\\MinGW\\bin\\gdb.exe -x -v " + tmp;
/***************开始调试************************/
if(!CreateProcess(NULL,tmp.GetBuffer(0),NULL,NULL,TRUE,0,NULL,NULL,&suid,&pid))
{
DWORD Error;
Error = GetLastError();
Info.pComplyShow->SetWindowText("调试失败!");
OnDebug = 0;
if(pid.hThread)
{
CloseHandle(pid.hThread);
pid.hThread = NULL;
}
if(pid.hProcess)
{
CloseHandle(pid.hProcess);
pid.hProcess = NULL;
}
if(hRead)
{
CloseHandle(hRead);
hRead = NULL;
}
if(hWrite)
{
CloseHandle(hWrite);
hWrite = NULL;
}
return 1;
}
DWORD dwBytesWritten;
DWORD t;
int s_stdin;
int s_stdout;
int s_stderr;
s_stdin = setmode (fileno(stdin), O_BINARY);
s_stdout = setmode (fileno(stdout), O_BINARY);
s_stderr = setmode (fileno(stderr), O_BINARY);
/* while()
{
}*/
t = WaitForSingleObject(pid.hProcess,INFINITE);
if(!WriteFile(hWrite,"r\n",2,&dwBytesWritten,NULL))
{
DWORD Error;
Error = GetLastError();
Info.pComplyShow->SetWindowText("调试失败!");
OnDebug = 0;
if(pid.hThread)
{
CloseHandle(pid.hThread);
pid.hThread = NULL;
}
if(pid.hProcess)
{
CloseHandle(pid.hProcess);
pid.hProcess = NULL;
}
if(hRead)
{
CloseHandle(hRead);
hRead = NULL;
}
if(hWrite)
{
CloseHandle(hWrite);
hWrite = NULL;
}
return 1;
}
t = dwBytesWritten;
//t = WaitForSingleObject(pid.hProcess,INFINITE);
char buf[4096] = {'\0'};
DWORD dwRead;
if(!ReadFile(hRead,buf,4095,&dwRead,NULL))
{
Complying = 2;
if(pid.hThread)
{
CloseHandle(pid.hThread);
pid.hThread = NULL;
}
if(pid.hProcess)
{
CloseHandle(pid.hProcess);
pid.hProcess = NULL;
}
if(NULL != hRead)
{
CloseHandle(hRead);
hRead = NULL;
}
if(NULL != hWrite)
{
CloseHandle(hWrite);
hWrite = NULL;
}
return 1;
}
strShowCompiler = buf;
Info.pComplyShow->SetWindowText(strShowCompiler.GetBuffer(0));
if(pid.hThread)
{
CloseHandle(pid.hThread);
pid.hThread = NULL;
}
if(pid.hProcess)
{
CloseHandle(pid.hProcess);
pid.hProcess = NULL;
}
if(hRead)
{
CloseHandle(hRead);
hRead = NULL;
}
if(hWrite)
{
CloseHandle(hWrite);
hWrite = NULL;
}
setmode (fileno(stdin), s_stdin); /* 恢复 */
setmode (fileno(stdout), s_stdout); /* 恢复 */
setmode (fileno(stderr), s_stderr); /* 恢复 */