How to Create a process in c++ to execute exe? - c++

I have to execute an .exe which is available on some drive. How can I do this using C++?
I am doing it like this:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
void main()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if(!CreateProcess(L"c:\\DOTNET.exe",NULL,NULL, NULL,FALSE, 0,NULL,NULL,&si,&pi ) )
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
}
else
{
printf("Prcess Creation Success");
}
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
getch();
}
But every time, it is showing this error:
process creation failed with error code 2 (i.e can not find the path specified)
But I place the DOTNET.exe at c:\DOTNET.exe only.
What is wrong in this code?

I've just tested your code and it's working here with :
if(!CreateProcess(L"C:\\Program Files\\Mozilla Firefox\\firefox.exe",NULL,NULL, NULL,FALSE, 0,NULL,NULL,&si,&pi ) )
A C++/Win32 solution for your C/Win32 code :)
void ExecuteAndWait (wstring toto)
{
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
vector<TCHAR> V( toto.length() + 1);
for (int i=0;i< (int) toto.length();i++)
V[i] = toto[i];
CreateProcess(NULL, &V[0],0, 0, FALSE, 0, 0, 0, &si, &pi);
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}

I think you need to call CreateProcess() differently.
Try this;
if (!CreateProcess( NULL, L"C:\\DOTNET.exe", NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi))

Are you building a Unicode executable? Try _T("C:\\DOTNET.exe") instead.
Or does your DOTNET.exe have some dependent DLLs which are not being found?

Related

How to execute CMD command with CreateProcess

I wanted to execute cmd command like "wmic logicaldisk get name > file.log". I have written the following function but it doesn't work and my program will crash.
bool Information::ExecuteConsoleCommand(QString arg_console_command)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if(!CreateProcess(NULL,
(WCHAR*)arg_console_command.toStdWString().c_str(),
NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
NULL, NULL, &si, &pi))
{
return false;
}
else
{
return true;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
How should I fix this function in the order it can execute cmd command? Also, I didn't want to use system() to run my commands because it show console window.
I see tag Qt and parameter's type QString in your question. So, you can use QProcess and QFile, here is example:
#include <QProcess>
#include <QDebug>
#include <QFile>
int main(int /*argc*/, char* /*argv*/[])
{
QProcess process;
process.start("wmic logicaldisk get name");
process.waitForFinished();
auto output = process.readAll();
QFile outputFile("file.log");
outputFile.open(QIODevice::WriteOnly);
outputFile.write(output);
outputFile.close();
return 0;
}
If you want to use CreateProcess, you can do it like this (based on your source):
#include <QString>
#include <Windows.h>
bool ExecuteConsoleCommand(QString arg_console_command)
{
arg_console_command.prepend("/C ");
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
// Start the child process.
if(!CreateProcessW(L"C:\\Windows\\System32\\cmd.exe",
(WCHAR*)arg_console_command.toStdWString().c_str(),
NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
NULL, NULL, &si, &pi))
{
return false;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return true;
}
int main(int /*argc*/, char* /*argv*/[])
{
ExecuteConsoleCommand("wmic logicaldisk get name > file.log");
return 0;
}

C++ CreateProcess fails when triggered from Task Scheduler

I have a C++ program which uses CreateProcess. This works fine when I manually trigger the application. However, when the application is triggered from the task scheduler it returns met the error code 6, what does this error-code refer to?
On the net I can't find any message corresponding to the error code?
The code I use:
int StartCommand(char* Command) {
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
bool CommandStatus = CreateProcess(NULL, Command, NULL, NULL, FALSE, CREATE_NO_WINDOW | CREATE_SUSPENDED | CREATE_BREAKAWAY_FROM_JOB, NULL, NULL, &si, &pi);
AssignProcessToJobObject(FilerHandle, pi.hProcess); // Does not work if without CREATE_BREAKAWAY_FROM_JOB
ResumeThread(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
if (CommandStatus) {
return pi.dwProcessId;
}
cout << GetLastError() << endl; //RETURNS ME 6
Message = "Couldn't start command!";
Exit(Message);
return 0;
}
Note that when I trigger it manually, the same command get's executed successfully

How to execute external programm in the parent C++ programm in Windows?

int system(const char *str);
declared in stdlib.h
As I know, this code will spawn a new process not within parent process and in Task Manager will be two processes - parent and child.
How to spawn child process in the parent process? ie in Task Manager must be only one - parent - process.
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
}
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
Source: MSDN

CreateProcess() compiling error?

i'm trying to figure out how to use the CreateProcess() function, and i'm not too proficient in C++. i've tried a few things to try making the error go away, but then it appears that the application doesn't do what i expect it to do thereafter.
what I want to do is pass "cmd.exe /c ipconfig > C:\test.txt" to it and have it execute that as expected.
the errors that i'm getting (in dev C++) are:
6 C:\Dev-Cpp\project\test\Untitled1.cpp main' must returnint'
C:\Dev-Cpp\project\test\Untitled1.cpp In function `int main(...)':
28 C:\Dev-Cpp\project\test\Untitled1.cpp return-statement with no value, in function returning 'int'
any help would be greatly appreciated.
here's the code i'm using (taken from microsoft's example):
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
Change
void _tmain( int argc, TCHAR *argv[] )
to
int _tmain( int argc, TCHAR *argv[] )
and return an exit code in your program, that is, change
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
to
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return 1;
}

how to save executed output cmd line in file in c++

I have this code for execute CMD line in c++
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
}
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
I want save executed output in file.but how?
In CreateProcess you pass a STARTUPINFO-structure. You can set the STARTF_USESTDHANDLES in si.dwFlags and then fill out the hStdInput, hStdOutput, and hStdError-fields with valid file-descriptors, especially hStdOutput should be a handle to a previously opened file (returned by successful CreateFile) which will then receive the std-output of the started process.
Edit:
This was kind of mean answer, because it needs more work to make that thing work: You need to create that file with the right SECURITY_ATTRIBUTES and have to set the Set handle inheritance to TRUE in CreateProcess. So it's also kind of purists nightmare to do it that way.
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
si.dwFlags |=STARTF_USESTDHANDLES ;
si.hStdInput=GetStdHandle(STD_INPUT_HANDLE);
si.hStdError=GetStdHandle(STD_ERROR_HANDLE);
SECURITY_ATTRIBUTES sa;
ZeroMemory( &sa, sizeof(sa) );
sa.nLength=sizeof(sa);
sa.bInheritHandle=TRUE;
si.hStdOutput=CreateFile ("log.txt", GENERIC_READ|GENERIC_WRITE, 0, &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
ZeroMemory( &pi, sizeof(pi) );
if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
}
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
TRUE, // Set handle inheritance to TRUE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
CloseHandle (si.hStdOutput);
}
Either redirect stdout to a file with
freopen("file.txt", "w", stdout);
Or, pipe the output to a file with windows
cmd> prg.exe > file.txt