Question

I am using c++ and i have a program that works with winmain. I do not want the system() call to open and close a window.

example:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    system("dir > nul 2> nul");

    return 0;
}

I want to use system calls but I don't want them to open a new window.

Thanks in advance.

edit:

I found this solution:

To execute cmd commands without opening a window i create a bat file (the program does this) then i use this code:

ShellExecute(NULL, "open", "Directory_Of_Bat_File", NULL, NULL, 0);

This opens the bat file and executes the commands. I also noticed that you don't need system() to delete the bat file but you can delete it by writing:

del Directory_Of_Bat_File

Inside the bat file and this will delete it when you execute the bat file (it will delete itself). This without opening a new window.

Was it helpful?

Solution 3

I found this solution:

To execute cmd commands without opening a window I create a bat file (the program does this) then I use this code:

ShellExecute(NULL, "open", "Directory_Of_Bat_File", NULL, NULL, 0);

This opens the bat file and executes the commands. I also noticed that you don't need system() to delete the bat file but you can delete it by writing:

del Directory_Of_Bat_File

Inside the bat file and this will delete it when you execute the bat file (it will delete itself). This without opening a new window.

OTHER TIPS

Since system, by its definition, creates a new process with a command interpreter, you can't do that.

From MSDN subject system

The system function passes command to the command interpreter, which executes the string as an operating-system command.

If you want to do "dir", since that's a built-in command in the "cmd.exe" or whatever command interpreter you are using, it's pretty difficult to "fix" this issue - even using ShellExecute or CreateProcess will not help a whole lot, since you will get a window either way - it may be minimizes or something like that, but it will still be a window there.

Use CreateProcess or ShellExecute to launch the process, there you can pass options related to windows. what system() executes can normally be found in environment, getenv("ComSpec")

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top