Mar 30, 2010 at 1:35am UTC
hi , in this code i am trying to write in the pipe and read from it but it s not working the child read only the first caracter can anyone tell me why :
main process :
// This is the main project file for VC++ application project
// generated using an Application Wizard.
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
#define BUFSIZE 4096
HANDLE FD[2];
int _tmain( )
{
// Créer un tube anonyme
// Initialisation de SECURITY_ATTRIBUTES du pipe à créer.
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE; // à faire hériter pour les fils
saAttr.lpSecurityDescriptor = NULL;
if (! CreatePipe(&FD[0], &FD[1], &saAttr, 0))
{ printf("Stdout tube creation failed\n");
return -1;
} else printf("FD[0]= %d, FD[1]=%d\n", FD[0], FD[1]);
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
//convert the handle FD[0] to char
char buf[10];
sprintf(buf, "pipeChild %d", FD[0]);
// Start the child process.
if( !CreateProcess( "..\\Debug\\Poursuite_fils.exe", //NULL, // No module name (use command line).
buf, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
TRUE, // Set handle inheritance to TRUE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return -1;
}
CloseHandle(FD[0]);
DWORD dwRead=BUFSIZE, dwWritten;
CHAR chBuf[BUFSIZE]="Bonjour du Pere au Fils\0" ;
if (! WriteFile(FD[1], chBuf, strlen(chBuf)+1, &dwWritten, NULL))
{
printf( "WriteFile pipe failed (%d).\n", GetLastError() );
return -1;
} else printf( "WriteFile pipe succeeded (%d).\n", dwWritten );
CloseHandle(FD[1]);
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
ExitProcess(0);
}
child process :
// This is the main project file for VC++ application project
// generated using an Application Wizard.
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
int _tmain(int argc, char * argv[])
{ DWORD dwRead;
char chBuf[4096];
printf("I am the child. My Pid and Tid: (%d, %d).\n", GetCurrentProcessId(), GetCurrentThreadId());
HANDLE fd0= (HANDLE) atoi (argv[1]);
printf("argc = %d, argv[0]=%s, argv[1]=%s, fd0=%d\n", argc, argv[0],argv[1], fd0);
if (! ReadFile(fd0, chBuf, 4096, &dwRead, NULL) || dwRead == 0)
{ printf("Error reading pipe\n");
CloseHandle(fd0);
return -1;
}
printf("Message recu : %s , de taille %d \n", chBuf, dwRead);
CloseHandle(fd0);
ExitProcess(10); // exit code for all threads
return 0;
}
Last edited on Mar 30, 2010 at 1:36am UTC