WinEXEC是作不到的 应该用CreateProcess并重定向dos程序的标准输出
这是我以前写的例子,我创建cmd.exe,并用从定向它的标准输出和标准输入在自己建的两个管道, 以后就可以象文件的I/O那样读写. 读的就是cmd.exe的标准输出.
我原来的工程是一个Telnet服务器,下面是建立的过程:
NetSend(hSock,"\15\12\15\12Creating I/O Pipes ...");
if(!CreatePipe(&hReadPipe1,&hWritePipe1,&sa,0))
{
GetError(str);
NetSend(hSock,str);
return ;
}
if(!CreatePipe(&hReadPipe2,&hWritePipe2,&sa,0))
{
GetError(str);
NetSend(hSock,str);
return ;
}
NetSend(hSock,"I/O Pipes created \15\12");
ReadPipe = hReadPipe1;
WritePipe = hWritePipe2;
//创建两个匿名管道。hReadPipe只能用来读管道,hWritePipe1只能用来写管道。STARTUPINFO si;
ZeroMemory(&si,sizeof(si));
si.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdInput = hReadPipe2;
si.hStdOutput = si.hStdError = hWritePipe1;PROCESS_INFORMATION ProcessInformation;
NetSend(hSock,"Creating shell process ...");
if(!CreateProcess(NULL,(LPSTR)Cmd,NULL,NULL,1,CREATE_NEW_CONSOLE,NULL,getenv("windir"),&si,&ProcessInformation))
{
GetError(str);
NetSend(hSock,str);
return ;
}
另外的读线程为:
LoopServerSendThread:
if(PeekNamedPipe(hRP,Buf,BUF_SIZE,&lBytesRead,0,0))
{
if(((CServer *)cs)->WantDel)return;
if(lBytesRead)
{
if(ReadFile(hRP,Buf,lBytesRead,&lBytesRead,0))
{
if(((CServer *)cs)->WantDel)return;
send(sock,Buf,lBytesRead,0);
if(((CServer *)cs)->WantDel)return;
}
}
else
{
Sleep(100);
}
if(((CServer *)cs)->WantDel)return;
}
else
{
Sleep(100);
}if(((CServer *)cs)->WantDel)return;
goto LoopServerSendThread;
这是我以前写的例子,我创建cmd.exe,并用从定向它的标准输出和标准输入在自己建的两个管道, 以后就可以象文件的I/O那样读写. 读的就是cmd.exe的标准输出.
我原来的工程是一个Telnet服务器,下面是建立的过程:
NetSend(hSock,"\15\12\15\12Creating I/O Pipes ...");
if(!CreatePipe(&hReadPipe1,&hWritePipe1,&sa,0))
{
GetError(str);
NetSend(hSock,str);
return ;
}
if(!CreatePipe(&hReadPipe2,&hWritePipe2,&sa,0))
{
GetError(str);
NetSend(hSock,str);
return ;
}
NetSend(hSock,"I/O Pipes created \15\12");
ReadPipe = hReadPipe1;
WritePipe = hWritePipe2;
//创建两个匿名管道。hReadPipe只能用来读管道,hWritePipe1只能用来写管道。STARTUPINFO si;
ZeroMemory(&si,sizeof(si));
si.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdInput = hReadPipe2;
si.hStdOutput = si.hStdError = hWritePipe1;PROCESS_INFORMATION ProcessInformation;
NetSend(hSock,"Creating shell process ...");
if(!CreateProcess(NULL,(LPSTR)Cmd,NULL,NULL,1,CREATE_NEW_CONSOLE,NULL,getenv("windir"),&si,&ProcessInformation))
{
GetError(str);
NetSend(hSock,str);
return ;
}
另外的读线程为:
LoopServerSendThread:
if(PeekNamedPipe(hRP,Buf,BUF_SIZE,&lBytesRead,0,0))
{
if(((CServer *)cs)->WantDel)return;
if(lBytesRead)
{
if(ReadFile(hRP,Buf,lBytesRead,&lBytesRead,0))
{
if(((CServer *)cs)->WantDel)return;
send(sock,Buf,lBytesRead,0);
if(((CServer *)cs)->WantDel)return;
}
}
else
{
Sleep(100);
}
if(((CServer *)cs)->WantDel)return;
}
else
{
Sleep(100);
}if(((CServer *)cs)->WantDel)return;
goto LoopServerSendThread;
解决方案 »
- 急需王育坚的《VC++面向对象编程教程》的源码!
- 谁能帮我讲解下OLE方面的知识,或者给我一些中文的资料。
- 请问下在对话框中如何使用CRichEditView?
- 如何判断一个已知名称是否为目录?
- 数据库添加记录问题.ODBC的
- 串口通信问题
- 本人想购买一些小游戏(娱乐)软件源代码:升级/拖拉机/斗地主/围棋等等--只要实现人与人玩就可以了,价格和付费方式面议。
- 有关利用键盘钩子切换窗口的问题。
- 征集一个控件,可以直接操作shell,效果就是一个资源管理器
- 请问如何该某个文件的图标(EXE文件的)
- 怎么改变视图的背景色,不用Rectangle填充?在线等。。。
- 请问:怎么改变视图的背景色,不用Rectangle填充?在线等。。。
Creating a Child Process with Redirected Input and Output
结合上一位高手的资料,明白了不少。请问各位见过没见过jdk的windows可视化编
译、执行程序?我想试一试。
另:win2K的系统环境变量(即我的电脑--右键--高级---环境变量里的)如何编程添加修改?----这次分发完了,大家不介意吧