关于远程CMD交互的菜鸟问题
编译连接都成功了,只是运行没有结果,恩,求大侠指教
- C/C++ code
#include "stdafx.h"int _tmain(int argc, _TCHAR* argv[]){ return 0;}#include<iostream>#include"winsock2.h"#pragma comment(lib,"ws2_32.lib")using namespace std;HANDLE hReadFile,hWriteFile,hReadPipe,hWritePipe;DWORD WINAPI ThreadReadFunc();void Error_Report(DWORD code,char* lp);int mark_1=0,mark_2=0;DWORD WINAPI ThreadWriteFunc();DWORD WINAPI ThreadReadFunc();int main(){ DWORD Thread_ID1,Thread_ID2; HANDLE hThread1,hThread2; hThread2=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadReadFunc,NULL,0,&Thread_ID2); if(hThread2==NULL) { Error_Report(GetLastError(),"CreateThread2"); return 1; } hThread1=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadWriteFunc,NULL,0,&Thread_ID1); if(hThread1==NULL) { Error_Report(GetLastError(),"CreateThread1"); return 1; } STARTUPINFO starinfo; GetStartupInfo(&starinfo); starinfo.dwFlags =STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES; //向进程输入 starinfo.hStdInput =hReadPipe; //从进程输出 starinfo.hStdOutput =hWritePipe; starinfo.hStdError =hWritePipe; starinfo.wShowWindow =SW_HIDE; char path[MAX_PATH]; ::GetSystemDirectoryA (path,MAX_PATH); LPCWSTR lp=_T("C:\\Windows\\System32\\cmd.exe"); PROCESS_INFORMATION pro_info; while(1) { if(mark_1==1&&mark_2==1) { goto dz; break; } else continue; }dz: if(CreateProcess(lp,NULL,NULL,NULL,true,0,NULL,NULL,&starinfo,&pro_info)) { cout<<"成功"; } else { Error_Report(GetLastError(),"CreateProcess"); } while(1) { ; }}DWORD WINAPI ThreadReadFunc(){ DWORD ByteWritten,nErrorCode; HANDLE hWriteFile; SECURITY_ATTRIBUTES pipe_sec; pipe_sec.nLength =sizeof(SECURITY_ATTRIBUTES); pipe_sec.bInheritHandle =true; pipe_sec.lpSecurityDescriptor =NULL; nErrorCode=CreatePipe(&hReadPipe,&hWriteFile,&pipe_sec,0); if(nErrorCode) { //创建pipe成功 mark_1=1; while(1) { //不断向进程中写入数据 Sleep(5000); nErrorCode=WriteFile(hWriteFile,"ipconfig -all \r\n",256,&ByteWritten,NULL); } } else { Error_Report(GetLastError(),"CreatePipe"); } return 0;}DWORD WINAPI ThreadWriteFunc(){ HANDLE hReadFile; SECURITY_ATTRIBUTES thread_sec; thread_sec.bInheritHandle =true; thread_sec.lpSecurityDescriptor =NULL; thread_sec.nLength =sizeof(SECURITY_ATTRIBUTES); if(CreatePipe(&hReadFile,&hWritePipe,&thread_sec,0)) { //不断从里面读出数据 mark_2=1; while(1) { char buf[2600]; DWORD size; ReadFile(hReadFile,buf,2600,&size,NULL); if(size!=0) { cout<<buf<<endl; } else { cout<<"没有数据 "; } } } else { cout<<"CreatePipe失败"; } return 0;}void Error_Report(DWORD code,char* lp){ cout<<lp<<"出错,代码是"<<code<<endl;}
[解决办法]
去我的blog看看