I am trying to use CreateProcessand CreatePipeto execute a process from a Windows Forms C ++ / CLR application in Visual Studio 2010.
In my Windows forms application, I want to execute a child process (console application) and return the result in the form std::string, std::wstringor System::String^in my Windows forms application. In addition, I do not want the newly created child process to spawn a window.
The console application has my own creation, so I also control its source.
I saw the following examples, but I donβt understand how to change the code to accomplish what I am trying to do:
MSDN code appears as two console applications, one of which calls the other. The code confuses me. I have been working only in C ++ for about 4 months, so I still don't understand everything. It seems to be referencing a text file that I don't need to do.
Is there an easier way to do this than an MSDN of 200+ lines of code or a kilobyte of more than 300 lines of code?
The answer here was useful, but more simplified. I was hoping to see an example of a basic source (one that doesn't include hundreds of lines of complex code would be preferable). I would use the MFC code, but it was difficult for me to adapt it to my goals (I do not use MFC).
Following is my adaptation of the code from Code Project:
string ExecuteExternalFile(string csExeName, string csArguments)
{
string csExecute;
csExecute=csExeName + " " + csArguments;
SECURITY_ATTRIBUTES secattr;
ZeroMemory(&secattr,sizeof(secattr));
secattr.nLength = sizeof(secattr);
secattr.bInheritHandle = TRUE;
HANDLE rPipe, wPipe;
CreatePipe(&rPipe,&wPipe,&secattr,0);
STARTUPINFO sInfo;
ZeroMemory(&sInfo,sizeof(sInfo));
PROCESS_INFORMATION pInfo;
ZeroMemory(&pInfo,sizeof(pInfo));
sInfo.cb=sizeof(sInfo);
sInfo.dwFlags=STARTF_USESTDHANDLES;
sInfo.hStdInput=NULL;
sInfo.hStdOutput=wPipe;
sInfo.hStdError=wPipe;
CreateProcess(0,(LPWSTR)csExecute.c_str(),0,0,TRUE,NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo);
CloseHandle(wPipe);
char buf[100];
DWORD reDword;
string m_csOutput,csTemp;
BOOL res;
do
{
res=::ReadFile(rPipe,buf,100,&reDword,0);
csTemp=buf;
m_csOutput+=csTemp;
}while(res);
return m_csOutput;
}
Windows Forms, , , . , .
:
std::string ping = ExecuteExternalFile("ping.exe", "127.0.0.1");
, , 3 , .