|
Redirection, give you an example:
char cmdbuffer [1024];
HANDLE hReadPipe;
HANDLE hReadPipe2;
HANDLE hWritePipe;
HANDLE hWritePipe2;
DWORD __stdcall ThreadFun (void * pVoid)
{
HWND hwnd = * ((HWND *) pVoid);
Ranch
SECURITY_ATTRIBUTES sat;
STARTUPINFO startupinfo;
PROCESS_INFORMATION pinfo;
BYTE buffer [1024];
DWORD byteRead;
CString rString;
Ranch
Ranch
sat.nLength = sizeof (SECURITY_ATTRIBUTES);
sat.bInheritHandle = true;
sat.lpSecurityDescriptor = NULL;
if (! CreatePipe (&hReadPipe,&hWritePipe,&sat, NULL))
{
MessageBox (NULL, "Create Pipe Error!", "Error!", MB_OK);
return 0;
}
if (! CreatePipe (&hReadPipe2,&hWritePipe2,&sat, NULL))
{
MessageBox (NULL, "Create Pipe2 Error!", "Error!", MB_OK);
return 0;
}
startupinfo.cb = sizeof (STARTUPINFO);
GetStartupInfo (&startupinfo);
startupinfo.hStdError = hWritePipe;
startupinfo.hStdOutput = hWritePipe;
startupinfo.hStdInput = hReadPipe2;
startupinfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
startupinfo.wShowWindow = SW_HIDE;
if (! CreateProcess (NULL, "c:\\winnt\\system32\\cmd.exe", NULL, NULL, TRUE, NULL, NULL, NULL,&startupinfo,&pinfo))
{
MessageBox (NULL, "create process error!", "Error!", MB_OK);
return 0;
}
CloseHandle (hWritePipe);
CloseHandle (hReadPipe2);
CloseHandle (pinfo.hThread);
CloseHandle (pinfo.hProcess);
Ranch
while (true)
{
RtlZeroMemory (buffer, 1024);
if (ReadFile (hReadPipe, buffer, 1023,&byteRead, NULL) == NULL)
break;
// buffer is the content displayed on the screen
}
CloseHandle (hReadPipe);
CloseHandle (hWritePipe2);
return 0;
}
// Create a thread:
HANDLE hThread = NULL;
hThread = CreateThread (NULL, 0, ThreadFun,&m_hWnd, 0,&dwThreadId); // m_hWnd This parameter is determined by your needs
if (hThread == NULL)
{
MessageBox ("CreateThread failed.", "Main", MB_OK);
}
else
{
CloseHandle (hThread);
}
// Write a command to the consel program:
strcpy (cmdbuffer, "dir");
strcat (cmdbuffer, "\r\n"); // There must be a carriage return and line feed
WriteFile (hWritePipe2, cmdbuffer, strlen (cmdbuffer),&byteRead, NULL); |
|