45

I am using CreateProcess to create a cmd.exe process that is passed a parameter that it executes and quits, this makes command prompt flash up on the screen.

I tried to avoid this by setting STARTUPINFO struct wShowWindow to SW_HIDE but this parameter seems to affect the calling window, not the window for the process that gets executed.

Is there anyway that you can use createprocess to launch a program that is hidden from view?

Also what is the proper winapi standard way to get enviroment variables?

1
  • 4
    Have you set the STARTF_USESHOWWINDOW in dwFlags? Commented Apr 23, 2009 at 6:15

4 Answers 4

78

If its just a console app you can also use the CREATE_NO_WINDOW flag as part of the CreateProcess call itself, e.g.

CreateProcess(NULL, lpszCommandLine, NULL, NULL, FALSE, 
              CREATE_NO_WINDOW, NULL, NULL, &si, &pi);

Also, see this page for information about environment variables.

Sign up to request clarification or add additional context in comments.

3 Comments

+1: This is the correct way to do that. Useful when you need to call cscript from the session 0 but don't want any console to popup.
For future references: CREATE_NO_WINDOW has value &H8000000
How to launch UI application (win32/wpf) in hidden mode. I want to show it later, not at the time of launch.
17

The following link here describes how to create the window silently:

DWORD RunSilent(char* strFunct, char* strstrParams)
{
    STARTUPINFO StartupInfo;
    PROCESS_INFORMATION ProcessInfo;
    char Args[4096];
    char *pEnvCMD = NULL;
    char *pDefaultCMD = "CMD.EXE";
    ULONG rc;

    memset(&StartupInfo, 0, sizeof(StartupInfo));
    StartupInfo.cb = sizeof(STARTUPINFO);
    StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
    StartupInfo.wShowWindow = SW_HIDE;

    Args[0] = 0;

    pEnvCMD = getenv("COMSPEC");

    if(pEnvCMD){

        strcpy(Args, pEnvCMD);
    }
    else{
        strcpy(Args, pDefaultCMD);
    }

    // "/c" option - Do the command then terminate the command window
    strcat(Args, " /c "); 
    //the application you would like to run from the command window
    strcat(Args, strFunct);  
    strcat(Args, " "); 
    //the parameters passed to the application being run from the command window.
    strcat(Args, strstrParams); 

    if (!CreateProcess( NULL, Args, NULL, NULL, FALSE,
        CREATE_NEW_CONSOLE, 
        NULL, 
        NULL,
        &StartupInfo,
        &ProcessInfo))
    {
        return GetLastError();      
    }

    WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
    if(!GetExitCodeProcess(ProcessInfo.hProcess, &rc))
        rc = 0;

    CloseHandle(ProcessInfo.hThread);
    CloseHandle(ProcessInfo.hProcess);

    return rc;

}

I think getenv and setenv are all okay? I am not sure what you are asking about in that respect.

1 Comment

Not bad, but strcat is unsafe, and getenv can return way more than 4096 characters. wiki.sei.cmu.edu/confluence/display/c/… Use strcat_s instead.
14

set the STARTF_USESHOWWINDOW in dwFlags

by sharptooth

1 Comment

For me it doesn't work. I still see a flashing CMD window: ghostbin.com/paste/xr7eo (same when using CREATE_NEW_CONSOLE in CreateProcess)
0

This might be an overkill for your needs, but you can hook the ShowWindow API and never show any windows for that process

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.