首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

windows管道解决思路

2012-12-25 
windows管道本帖最后由 you13fire 于 2012-10-03 10:07:14 编辑我改写的一个管道通信程序,因为子进程有多

windows管道
本帖最后由 you13fire 于 2012-10-03 10:07:14 编辑 我改写的一个管道通信程序,因为子进程有多个输入输出组合语句,如(scanf,printf,再scanf,printf...),父进程先WriteFile(对应第一个scanf),再ReadFile,发现此时ReadFile阻塞;而一次性WriteFile两个参数时,ReadFile不会阻塞。我的本意是想让VC调试程序的界面调试信息写入到一个txt中。符上代码:
//VS2010  主程序


#include "stdafx.h"

#include "stdafx.h"
#include "Windows.h"
#include "stdio.h"
 
int main(int argc, char* argv[])
{
 SECURITY_ATTRIBUTES sa,sa2; 
 HANDLE hInputRead,hInputWrite; 
 HANDLE hOutputRead,hOutputWrite; 
 
 sa.nLength = sizeof(SECURITY_ATTRIBUTES); 
 sa.lpSecurityDescriptor = NULL; 
 sa.bInheritHandle = TRUE; 
 if (!CreatePipe(&hOutputRead,&hOutputWrite,&sa,0)) 
 { 
  printf("Error On CreatePipe1"); 
  return 1; 
 } 
 
 sa2.nLength = sizeof(SECURITY_ATTRIBUTES); 
 sa2.lpSecurityDescriptor = NULL; 
 sa2.bInheritHandle = TRUE; 
 if (!CreatePipe(&hInputRead,&hInputWrite,&sa2,0)) 
 { 
  printf("Error On CreatePipe2"); 
  return 1; 
 } 
 
 STARTUPINFO si; 
 PROCESS_INFORMATION pi; 
 si.cb = sizeof(STARTUPINFO); 
 GetStartupInfo(&si); 
 si.hStdError = hOutputWrite; 
 si.hStdOutput = hOutputWrite; 
 si.hStdInput = hInputRead;
 si.wShowWindow = SW_SHOW; 
 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; 
 
 TCHAR cmd_line[100];
 lstrcpy(cmd_line,TEXT("D:\\test\\Debug\\test.exe"));
 DWORD dwWritten;
 if (!CreateProcess(NULL,cmd_line,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi))
 {
#ifdef UNICODE
  wprintf(TEXT("Error On CreateProcess")); 
#else
  printf(TEXT("Error On CreateProcess")); 
#endif
  return 1; 
 } 
 
 CloseHandle(hInputRead);
 CloseHandle(hOutputWrite);
 
 char  szInPut[]= ("12\r\n");// 输入//34\r\n23\r\n56\r\n

 if(WriteFile(hInputWrite, szInPut, strlen(szInPut), &dwWritten, NULL))
 printf("write success\n");

 char buffer[32] = {0}; 
 DWORD bytesRead; 

 if(ReadFile(hOutputRead,buffer,31,&bytesRead,NULL))
 printf("read success: %s\n",buffer);
 else
 printf("error\n");

  if(WriteFile(hInputWrite, szInPut, strlen(szInPut), &dwWritten, NULL))
 printf("write success\n");

   if(ReadFile(hOutputRead,buffer,31,&bytesRead,NULL))
 printf("read success: %s\n",buffer);
 else
 printf("error\n");

 
 Sleep(5000);
 CloseHandle(hInputWrite);
 CloseHandle(hOutputRead);
 return 0;
}


/*test.cpp   子进程*/

#include "stdafx.h"
#include "stdio.h"
int main(int argc, char* argv[])
{
int x;
scanf( "%d", &x );
printf("x=%d\n",x);
scanf( "%d", &x );
 printf("x=%d\n",x);
return 0;
}


[最优解释]
你用AllocConsole不是更简单
http://blog.csdn.net/visualeleven/article/details/7628564
[其他解释]


#include "stdafx.h"
#include "stdio.h"
int main(int argc, char* argv[])
{
int x;
scanf( "%d", &x );
printf("x=%d\n",x);
flushall();
scanf( "%d", &x );
 printf("x=%d\n",x);
flushall();
return 0;
}

试试
[其他解释]
用异步IO 函数,而不是同步函数
[其他解释]
谢谢回复,但能不能针对我的代码说详细点呀?不太明白
[其他解释]
MSDN上有例子,是同时开两个管道,一个读,一个写。
[其他解释]
管道是内核对象,同时操作一个管道存不存在资源的竞争,所以导致了阻塞。管道不是很熟悉,不过我觉得3#说的比较对。
引用:
MSDN上有例子,是同时开两个管道,一个读,一个写。

[其他解释]
谢谢大家的回答,尤其是4楼,我试了,方案可行。但我想不要修改子程序,因为在我的应用环境中,子程序是别人写的,我不能修改,我就是做一个中介,交互式的把输入传给子程序,再把输出取回来。
[其他解释]
对于3楼和5楼,我这个程序就是同时开两个管道,麻烦仔细看看,不过还是感谢回复
[其他解释]
来个大神啊
[其他解释]
谢谢VisualEleven,虽然还没成功,但给我了一个思路

热点排行