问题描述
- 关于Windows下管道通信的问题
-
我想要通过管道通信控制CMD,能从父进程读数据就是写不了,WriteFile后没反应,szCOMMAND改成随便一个DOS命令,就是没反应,没辙了,请问代码什么地方出了问题呢?
SECURITY_ATTRIBUTES sa;
SECURITY_ATTRIBUTES sa1;
HANDLE hRead_A, hWrite_A;
HANDLE hRead_B, hWrite_B;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
sa1.bInheritHandle = TRUE;
sa1.lpSecurityDescriptor = NULL;
sa1.nLength = sizeof(sa1);
if (!CreatePipe(&hRead_A, &hWrite_A, &sa, 0)) {
printf("ERROR,createpipe_1");
}
if (!CreatePipe(&hRead_B, &hWrite_B, &sa1, 0)){
printf("ERROR,createpipe_2");} STARTUPINFO si; PROCESS_INFORMATION pi; si.cb = sizeof(STARTUPINFO); GetStartupInfo(&si); si.hStdInput = hRead_A; si.hStdError = hWrite_B; si.hStdOutput = hWrite_B; si.wShowWindow = SW_HIDE; si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; if (!CreateProcess(NULL, TEXT("cmd") , NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi)) { printf("createprocess %d",GetLastError()); } char buffer[4096] = { 0 }; DWORD bytesRead; while (true) { DWORD dwdw; WriteFile(hWrite_A,szCOMMAND , sizeof(szCOMMAND), &dwdw, NULL); if (ReadFile(hRead_B, buffer, 4095, &bytesRead, NULL) == NULL) break; printf(buffer); } ![图片说明](http://img.ask.csdn.net/upload/201503/18/1426675258_757052.jpg) 这个是把szCOMMAND改成ping命令的,运行后没反应 ![图片说明](http://img.ask.csdn.net/upload/201503/18/1426675388_229721.jpg)
时间: 2025-01-02 01:36:16