C语言创建管道实现进程间通信
来源:互联网 发布:我的世界网络错误 编辑:程序博客网 时间:2024/06/04 00:36
#include <windows.h>#include <stdio.h>#include <stdlib.h>int runcmd( char* lpCmd ){ char buf[2048] = {0}; //缓冲区 DWORD len; HANDLE hRead, hWrite; // 管道读写句柄 STARTUPINFO si; PROCESS_INFORMATION pi; SECURITY_ATTRIBUTES sa; //ZeroMemory( buf, 2047 ); sa.nLength = sizeof( sa ); sa.bInheritHandle = TRUE; // 管道句柄是可被继承的 sa.lpSecurityDescriptor = NULL; // 创建匿名管道,管道句柄是可被继承的 if( !CreatePipe( &hRead, &hWrite, &sa, 2048 ) ) { printf( "管道创建失败!(%#X)\n", (unsigned int)GetLastError() ); return 1; } ZeroMemory( &si, sizeof( si ) ); si.cb = sizeof( si ); si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE; si.hStdError = hWrite; si.hStdOutput = hWrite; // 创建子进程,运行命令,子进程是可继承的 if ( !CreateProcess( NULL, lpCmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi ) ) { printf( "创建进程失败!(%#x)\n", (unsigned int)GetLastError() ); CloseHandle( hRead ); CloseHandle( hWrite ); return 1; } // 写端句柄已被继承,本地需要关闭,不然读管道时将被阻塞 CloseHandle( hWrite ); // 读管道内容,并显示 while ( ReadFile( hRead, buf, 2047, &len, NULL ) ) { printf( buf ); ZeroMemory( buf, 2047 ); } CloseHandle( hRead ); return 0;}int main( int argc, char** argv ){ char cmd[256]; printf( "输入命令行:" ); gets( cmd ); runcmd( cmd ); system( "pause" ); return 0;}
阅读全文
0 0
- C语言创建管道实现进程间通信
- C语言无名管道实现父子进程间通信
- 进程间通信-创建管道实现机制
- 【C语言】【unix c】如何使用管道实现两个进程间的通信
- 管道实现进程间通信
- 命名管道实现跨语言进程通信
- copy_process.c(采用进程间管道通信方式实现复制)
- linux c之通过管道实现兄弟间进程通信:
- Linux C命名管道实现进程间通信
- Lunix下用C语言实现管道间通信
- 创建一个进程和两个管道,实现与进程通信
- Linux C 进程间的管道通信
- 使用命名管道实现进程间通信
- 使用命名管道实现进程间通信
- 使用命名管道实现进程间通信
- 进程间通信 - 命名管道实现
- 通过匿名管道实现进程间通信
- 进程间通信 - 匿名管道实现
- 使用Kotlin开发Android基础介绍
- 指针和引用的区别
- Pycharm中如何加载多个项目?
- 「57万片段+10万动作+21万标签」谷歌发布人类动作识别数据集AVA
- HTML5中的WebSocket
- C语言创建管道实现进程间通信
- 东北大学科学家利用CNN识别贫血患者红细胞
- 创建一个带有SQLserver数据库模型的MVC项目
- 世界最大色情网站Pornhub使用AI识别AV,鉴黄师要下岗了!
- 阿联酋任命第一位人工智能部长:谢赫穆罕默德·本·拉希德·阿勒马克图姆
- opencv通过鼠标获取训练图像
- leetcode Median of Two Sorted Arrays
- C++笔记——标准模板库STL
- github