winapi: CreateProcess but hide the process' window?

前端 未结 4 2045
一向
一向 2020-12-01 07:24

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 trie

4条回答
  •  青春惊慌失措
    2020-12-01 08:03

    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.

提交回复
热议问题