📄 anonpipe.c
字号:
#include <windows.h>
#include <stdio.h>
void pipe_write( HANDLE hWritePipe) {
// loop and write out ten DWORDs...
int i;
BOOL bStatus;
for (i = 0; i < 10; i++) {
DWORD dwData = i;
DWORD dwBytesWritten;
bStatus = WriteFile( hWritePipe, &dwData, sizeof(dwData), &dwBytesWritten, NULL);
if (bStatus)
printf( "Parent successfully wrote %lu into pipe.\n", dwData);
else
printf( "Parent failed writing %lu into pipe.\n", dwData);
}
printf( "Parent finished writing into pipe.\n");
}
void pipe_read( HANDLE hReadPipe) {
// read DWORDs out of pipe until the write handle is closed...
BOOL bStatus;
do {
DWORD dwData;
DWORD dwBytesRead;
bStatus = ReadFile( hReadPipe, &dwData, sizeof(dwData), &dwBytesRead, NULL);
if (bStatus)
printf( "Child successfully read %lu from pipe.\n", dwData);
else
printf( "Child ReadFile returned FALSE, finished reading from pipe.\n");
} while (bStatus);
}
int main( int argc, char *argv[]) {
HANDLE hReadUninheritable, hWriteUninheritable;
HANDLE hReadInheritable;
HANDLE hSaveStdIn;
BOOL bStatus;
STARTUPINFO si;
PROCESS_INFORMATION pi;
////////////////////////////////////////////////////
// Should we create the writing parent
// or the reading child process?
// Passing no arguments on the command line
// will create the child reader, passing any
// arguments will create the parent writer.
// This is useful to demo the use of anonymous
// pipes with one source program, but there is
// no reason that the parent and child couldn't
// be written as separate programs...
//
// Start the program with a command line
// such as "anonpipe parent" to create the parent,
// and the parent will start a copy of itself to
// run as the child...
//
////////////////////////////////////////////////////
if (argc > 1) {
// create the pipe with uninheritable handles...
bStatus = CreatePipe( &hReadUninheritable, &hWriteUninheritable, NULL, 0);
if (!bStatus) {
printf("FATAL ERROR: Unable to create pipe.\n");
exit(-1);
}
// make an inheritable duplicate of the read handle...
bStatus = DuplicateHandle( GetCurrentProcess(), hReadUninheritable, GetCurrentProcess(),
&hReadInheritable, 0, TRUE, DUPLICATE_SAME_ACCESS);
if (!bStatus) {
printf("FATAL ERROR: Unable to duplicate handle.\n");
exit(-1);
}
// close the uninheritable read handle...
CloseHandle(hReadUninheritable);
// make the inheritable read handle the standard input
// handle for easy passing to the child...
hSaveStdIn = GetStdHandle(STD_INPUT_HANDLE);
SetStdHandle( STD_INPUT_HANDLE, hReadInheritable);
// create the child process with handles inheritable...
// the child process will see the read end of the pipe
// as its standard input handle...
ZeroMemory( &si, sizeof(si));
si.cb = sizeof(si);
bStatus = CreateProcess( NULL, argv[0], NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
if (!bStatus) {
printf("FATAL ERROR: Unable to create child process.\n");
exit(-1);
}
// close the handles that we get for the child process since
// we're not going to use them...
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
// reset our standard input handle...
SetStdHandle( STD_INPUT_HANDLE, hSaveStdIn);
// close our copy of the read handle...
CloseHandle(hReadInheritable);
// the parent needs to write some data to the pipe...
pipe_write(hWriteUninheritable);
// we are done close the write end of the pipe...
CloseHandle(hWriteUninheritable);
printf( "Parent done, now exiting.\n");
}
else {
// the child needs to read some data from the pipe
// which has been connected to standard input...
pipe_read(GetStdHandle(STD_INPUT_HANDLE));
printf( "Child done, now exiting.\n");
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -