关于管道写失败,两者的通信不成功
来了新问题 关于管道的问题 给了绝对的路径还是启动不了进程Child.exe
#include "stdafx.h"
#define _AFXDLL
#include "windows.h"
int WritePipe(HANDLE hWritePipe,char szRecvBuf[100]);
int ReadPipe(HANDLE hReadPipe);
void _tmain(int argc, _TCHAR* argv[])
{
HANDLE hReadPipe;
HANDLE hWritePipe;
HANDLE hEvent;
STARTUPINFO suif;
PROCESS_INFORMATION pi;
memset(&suif,0,sizeof(STARTUPINFO));
suif.cb=sizeof(STARTUPINFO);
suif.hStdError=GetStdHandle(STD_ERROR_HANDLE);
SECURITY_ATTRIBUTES sa;
sa.bInheritHandle=true;
sa.lpSecurityDescriptor=NULL;
sa.nLength=sizeof(SECURITY_ATTRIBUTES);
if(!CreatePipe(&hReadPipe,&hWritePipe,&sa,sizeof(SECURITY_ATTRIBUTES)))
{
printf("Create pipe failed\n");
return ;
}
suif.hStdInput=hReadPipe;
suif.hStdOutput=hWritePipe;
printf("press any key to start child.exe");
getchar();
if(!CreateProcess(_T("C:\\Documents and Settings\\kampu\\My Documents\\Visual Studio 2005\Projects\\Sock\\debug\\Child.exe"),NULL,NULL,NULL,true,CREATE_NEW_CONSOLE,NULL,NULL,&suif,&pi))
{
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
printf("start process failed\n");
return ;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
printf("press any key to write pipe\n");
getchar();
char szSend[]="this is parent";
WritePipe(hWritePipe,szSend);
printf("press any key to read pipe\n");
getchar();
ReadPipe(hReadPipe);
return ;
}
int ReadPipe(HANDLE hReadPipe)
{
char szRecvBuf[100];
DWORD wRecv=0;
if(!ReadFile(hReadPipe,szRecvBuf,100,&wRecv,NULL))
{
printf("Read file failed\n");
return 0;
}
printf("%s\n",szRecvBuf);
return 1;
}
int WritePipe(HANDLE hWritePipe,char szRecvBuf[100])
{
DWORD wRecv=0;
if(!WriteFile(hWritePipe,szRecvBuf,(DWORD)strlen(szRecvBuf)+1,&wRecv,NULL))
{
printf("Write file failed\n");
return 0;
}
printf("Write pipe succeed;");
return 1;
}
子进程程序
// Child.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#define _AFXDLL
#include "windows.h"
int WritePipe(HANDLE hWritePipe,char szRecvBuf[100]);
int ReadPipe(HANDLE hReadPipe);
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hWritePipe;
HANDLE hReadPipe;
hReadPipe=GetStdHandle(STD_INPUT_HANDLE);
hWritePipe=GetStdHandle(STD_OUTPUT_HANDLE);
printf("please press any key to read pipe\n");
getchar();
ReadPipe(hReadPipe);
printf("please press any key to write pipe\n");
getchar();
char szSend[]="this is parent\n";
WritePipe(hWritePipe,szSend);
return 0;
}
int ReadPipe(HANDLE hReadPipe)
{
char szRecvBuf[100];
DWORD wRecv=0;
if(!ReadFile(hReadPipe,szRecvBuf,100,&wRecv,NULL))
{
printf("Read file failed\n");
return 0;
}
printf("%s\n",szRecvBuf);
return 1;
}
int WritePipe(HANDLE hWritePipe,char szRecvBuf[100])
{
DWORD wRecv=0;
if(!WriteFile(hWritePipe,szRecvBuf,(DWORD)strlen(szRecvBuf)+1,&wRecv,NULL))
{
printf("Write file failed\n");
return 0;
}
printf("Write pipe succeed;");
return 1;
}
子进程出现一些乱码,父进程没有反应。谁能帮忙调试一下?