//====================================================
// Description: A sample to show how to filter cmd.exe
//====================================================
#define WIN32_LEAN_AND_MEAN
#define WINVER 0x0500
// Read from a file and write its contents to a pipe.
for (;;)
{
memset(cBuffer, 0, BUFF_SIZE);
if (! ReadFile(g_hInputHandle, cBuffer, BUFF_SIZE, &dwReadCnt, NULL) ||
dwReadCnt == 0)
break;
// Add check-method here
CheckCmd(cBuffer);
nCmdLen = strlen(cBuffer);
if (! WriteFile(g_hCmdStdinWriteDup, cBuffer, dwReadCnt, &dwWriteCnt, NULL)) break;
//----------------------------------------------------------------------------
DWORD CmdMain()
{
SECURITY_ATTRIBUTES saAttr;
BOOL fSuccess;
// Set the bInheritHandle flag so pipe handles are inherited.
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
// The steps for redirecting child process's STDOUT:
// 1. Save current STDOUT, to be restored later.
// 2. Create anonymous pipe to be STDOUT for child process.
// 3. Set STDOUT of the parent process to be write handle to
// the pipe, so it is inherited by the child process.
// 4. Create a noninheritable duplicate of the read handle and
// close the inheritable read handle.
// Save the handle to the current STDOUT.
g_hBackupStdout = GetStdHandle(STD_OUTPUT_HANDLE); // 得到本进程的当前标准输出
// Create a pipe for the child process's STDOUT.
if (! CreatePipe(&g_hChildStdoutRead, //指向读句柄的指针
&g_hChildStdoutWrite, // 指向写句柄的指针
&saAttr, // 指向安全属性的指针
0) // 管道大小
) ErrorExit("Stdout pipe creation failed\n");
// Set a write handle to the pipe to be STDOUT.
// 设置标准输出到匿名管道
if (! SetStdHandle(STD_OUTPUT_HANDLE, g_hChildStdoutWrite)) ErrorExit("Redirecting STDOUT failed");
// Create noninheritable read handle and close the inheritable read
// handle.
// The steps for redirecting child process's STDIN:
// 1. Save current STDIN, to be restored later.
// 2. Create anonymous pipe to be STDIN for child process.
// 3. Set STDIN of the parent to be the read handle to the
// pipe, so it is inherited by the child process.
// 4. Create a noninheritable duplicate of the write handle,
// and close the inheritable write handle.
// Save the handle to the current STDIN.
g_hBackupStdin = GetStdHandle(STD_INPUT_HANDLE);
// Create a pipe for the child process's STDIN.
if (! CreatePipe(&g_hCmdStdinRead, &g_hCmdStdinWrite, &saAttr, 0))
ErrorExit("Stdin pipe creation failed\n");
// Set a read handle to the pipe to be STDIN.
if (! SetStdHandle(STD_INPUT_HANDLE, g_hCmdStdinRead))
ErrorExit("Redirecting Stdin failed");
// Duplicate the write handle to the pipe so it is not inherited.
fSuccess = DuplicateHandle(GetCurrentProcess(), g_hCmdStdinWrite,
GetCurrentProcess(), &g_hCmdStdinWriteDup, 0,
FALSE, // not inherited
DUPLICATE_SAME_ACCESS);
if (! fSuccess)
ErrorExit("DuplicateHandle failed");
CloseHandle(g_hCmdStdinWrite);
// Now create the child process.
fSuccess = CreateCMDProcess();
if (! fSuccess)
ErrorExit("Create process failed");
// After process creation, restore the saved STDIN and STDOUT.
if (! SetStdHandle(STD_INPUT_HANDLE, g_hBackupStdin))
ErrorExit("Re-redirecting Stdin failed\n");
if (! SetStdHandle(STD_OUTPUT_HANDLE, g_hBackupStdout))
ErrorExit("Re-redirecting Stdout failed\n");
// Get a handle to the parent's input file.
g_hInputHandle = g_hBackupStdin;
if (g_hInputHandle == INVALID_HANDLE_VALUE)
ErrorExit("no input file\n");