I have a fairly simple question, which may or may not have a simple solution. How can I run the system(); command, in a win32 (not console) application, without the black command prompt window flashing on the screen?

If relevant, I am using Dev-C++.

Recommended Answers

All 5 Replies

The short answer is that you can't. The system() function (it's not a command) supported by win32 compilers/libraries starts up a command shell, and that command shell shows the "black command prompt window".

If you actually need a command shell (eg you're running an inbuilt shell command or executing a .BAT file), then you have no choice. However, you might be able to find a way to implement your required functionality without using a command shell.

If you want to run a third-party program (eg another win32 application) with more control than is possible with the system() command, look up the CreateProcess() function in the win32 API.

I agree with grumpier, if you have to use the system() call, you're stuck with the "black window". But unless you're calling batch files, you can probably avoid the system() call.

I know, system() isn't a command, I was in a hurry when I wrote that.

Well, basically what I'm trying to do is make a program that automatically starts the user in the SYSTEM account (if you've seen guides on how to do that). It's not a particularly "useful" program, but it's good practice. I am trying to use the "at" command using /interactive to start explorer.exe as SYSTEM.

Example:

std::string cmd = "at " + calcTime(1) + " /interactive explorer.exe";
system(cmd.c_str());

Perhaps this is was a stupid question, I suppose I could use CreateProcess() to run at.exe. Am I correct?

at.exe is in my system32 directory, so I suspect you could use CreateProcess on it.

#include <Windows.h>
void my_cmd()
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));
    // CMD command here
    char arg[] = "cmd.exe /c E:/Softwares/program.exe";
    // Convert char string to required LPWSTR string
    wchar_t text[500];
    mbstowcs(text, arg, strlen(arg) + 1);
    LPWSTR command = text;
    // Run process
    CreateProcess (NULL, command, NULL, NULL, 0, 
    CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
}

Using CreateProcess instead of system works for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.