锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
浠ヤ笅浠g爜婕旂ず浜嗗垱寤篊MD.EXE榪涚▼,騫剁敤2鏍圭閬撻噸瀹氬悜浠栫殑杈撳叆杈撳嚭,騫惰鍙朇MD浜х敓鐨勭粨鏋滄暟鎹?
鍗冧竾涓嶈蹇樿璁劇疆SECURITY_ATTRIBUTES閲岀殑bInheritHandle,姣斿鍒涘緩綆¢亾鐨勬椂鍊欒鎶奲InheritHandle璁劇疆涓篢RUE
鍚屾牱涓嶄竴瀹氳鐢ㄧ閬?姣斿鏂囦歡,SOCKET絳夐兘鍙互閲嶅畾鍚?
濡傛灉鏄敤SOCKET,鏍規(guī)嵁緗戜笂鐨勫悕璇?鍙互鍒涘緩涓涓?闆剁閬撳悗闂?紼嬪簭.姣斿鍦ㄦ湇鍔″櫒绔笂鐨勫悗闂ㄧ▼搴忕洃鍚煇涓鍙?涓鏃︽湁榪炴帴璇鋒眰,鎺ュ彈鍚庡垱寤篠OCKET,灝卞湪榪欎釜鏃跺欏紑鍚疌MD.EXE,騫墮噸瀹氬悜杈撳叆杈撳嚭鍒版湇鍔″櫒涓婄殑榪欎釜SOCKET,榪欐牱渚夸負(fù)榪滅▼鐨勮繖涓繛鎺ヨ姹傚紑浜嗕竴涓湇鍔″櫒涓婄殑鍚庨棬錛屼粠鑰岃繙紼嬭繛鎺ュ彲浠ユ墽琛屾湇鍔″櫒騫惰繑鍥炵粨鏋?
婕旂ず浠g爜濡備笅:
//
// File: Main.cpp
// Purpose: Creates a process( cmd.exe for example ), and redirect its standard input
// by using creating a pipe, then writes some bytes as commands to pipe.
//
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
// Error report
void Error( const char *szErrMsg );
int main( int argc, char **argv )
{
// Create pipe
BOOL bRet;
HANDLE hPipeRead, hPipeWrite;
HANDLE hPipeReadII, hPipeWriteII;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof( SECURITY_ATTRIBUTES );
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
bRet = ::CreatePipe( &hPipeRead, &hPipeWrite, &sa, 512 );
bRet &= ::CreatePipe( &hPipeReadII, &hPipeWriteII, &sa, 512 );
if ( !bRet )
{
Error( "Can't create pipe!" );
return -1;
}
// Spawn a process
STARTUPINFO StartInfo;
PROCESS_INFORMATION ProcessInfo;
// memset( &StartInfo, 0, sizeof(StartInfo) );
// memset( &ProcessInfo, 0, sizeof(ProcessInfo) );
::GetStartupInfo( &StartInfo );
StartInfo.cb = sizeof(StartInfo);
StartInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
StartInfo.wShowWindow = SW_HIDE;
StartInfo.hStdInput = hPipeRead;
StartInfo.hStdOutput = hPipeWriteII; //::GetStdHandle( STD_OUTPUT_HANDLE );
StartInfo.hStdError = hPipeWriteII; //::GetStdHandle( STD_OUTPUT_HANDLE );
bRet = ::CreateProcess( TEXT("c:\\windows\\system32\\cmd.exe"), NULL, NULL, NULL, TRUE, 0, NULL, NULL, &StartInfo, &ProcessInfo );
::Sleep(1000);
printf("wake up...\n");
if ( !bRet )
{
Error( "Can't create process!" );
return -1;
}
// Write commands to pipe
char *szCmd = "netstat\r\n";
DWORD dwDummy;
::WriteFile( hPipeWrite, szCmd, 10, &dwDummy, NULL );
::Sleep(2000);
printf("had written...\n");
// Read from pipe
char szBuf[1024];
memset(szBuf, 0, sizeof(szBuf));
::ReadFile( hPipeReadII, szBuf, sizeof(szBuf), &dwDummy, NULL );
// Don't leave till the spawned process goes end
// ::Sleep(2000);
printf("had read...\n");
// ::WaitForSingleObject( ProcessInfo.hProcess, INFINITE );
printf("%s\n", szBuf);
return 0;
}
void Error( const char *szErrMsg )
{
printf( "Error: %s\n", szErrMsg );
}
#include <windows.h>
#include <stdio.h>
HANDLE ghEvents[2];
DWORD WINAPI ThreadProc( LPVOID );
void main()
{
HANDLE hThread;
DWORD i, dwEvent, dwThreadID;
// Create two event objects
for (i = 0; i < 2; i++)
{
ghEvents[i] = CreateEvent(
NULL, // default security attributes
FALSE, // auto-reset event object
FALSE, // initial state is nonsignaled
NULL); // unnamed object
if (ghEvents[i] == NULL)
{
printf("CreateEvent error: %d\n", GetLastError() );
ExitProcess(0);
}
}
// Create a thread
hThread = CreateThread(
NULL, // default security attributes
0, // default stack size
(LPTHREAD_START_ROUTINE) ThreadProc,
NULL, // no thread function arguments
0, // default creation flags
&dwThreadID); // receive thread identifier
if( hThread == NULL )
{
printf("CreateThread error: %d\n", GetLastError());
return;
}
// Wait for the thread to signal one of the event objects
dwEvent = WaitForMultipleObjects(
2, // number of objects in array
ghEvents, // array of objects
FALSE, // wait for any object
5000); // five-second wait
// The return value indicates which event is signaled
switch (dwEvent)
{
// ghEvents[0] was signaled
case WAIT_OBJECT_0 + 0:
// TODO: Perform tasks required by this event
printf("First event was signaled.\n");
break;
// ghEvents[1] was signaled
case WAIT_OBJECT_0 + 1:
// TODO: Perform tasks required by this event
printf("Second event was signaled.\n");
break;
case WAIT_TIMEOUT:
printf("Wait timed out.\n");
break;
// Return value is invalid.
default:
printf("Wait error: %d\n", GetLastError());
ExitProcess(0);
}
// Close event handles
for (i = 0; i < 2; i++)
CloseHandle(ghEvents[i]);
}
DWORD WINAPI ThreadProc( LPVOID lpParam )
{
// Set one event to the signaled state
if ( !SetEvent(ghEvents[0]) )
{
printf("SetEvent failed (%d)\n", GetLastError());
return -1;
}
return 1;
}