I know how to run compiled C++ programs (exe files) from my console, but it starts only in multiple windows.
So, I would like to know how to run it in my console like in windows cmd. Is it possible ?
I know how to run compiled C++ programs (exe files) from my console, but it starts only in multiple windows.
So, I would like to know how to run it in my console like in windows cmd. Is it possible ?
If you are beginner:
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
system("Name.exe"); //This is for starting program in your programs console.
system("start Name.exe"); //This is for starting program in its own console.
return 0;
}
Note: usually programmers and antiviruses dont like system()
, maybe because:
- It is resource heavy
- It defeats security -- you don't know you it's a valid command or does the same thing on every system, you could even start up programs you didn't intend to start up. The danger is that when you directly execute a program, it gets the same privileges as your program -- meaning that if, for example, you are running as system administrator then the malicious program you just inadvertently executed is also running as system administrator. If that doesn't scare you silly, check your pulse.
- Anti virus programs hate it, your program could get flagged as a virus.
-from here.
..so, you can try instead:
ShellExecute(NULL, "open", "Name.exe", NULL, NULL, SW_SHOWDEFAULT);
or:
VOID startup(LPCTSTR lpApplicationName){
// additional information
STARTUPINFO si;
PROCESS_INFORMATION pi;
// set the size of the structures
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// start the program up
CreateProcess( lpApplicationName, // the path
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 (removed extra parentheses)
);
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
Note: With same #include <iostream>
, #include <windows.h>
, using namespace std;
headers! In 2 last options you can change parameters to get better result.