How to hide the subprocess console window?

I am trying to write a very simple program to replace an existing executable. He should slightly reject his arguments and execute the original program with new arguments. It should be automatically and quietly launched by a third-party library.

It works fine, but a console window appears to display the output of the called program. I need the console window not to be. I do not care about the program exit.

My initial attempt was configured as a console application, so I decided to fix it by writing a new Windows GUI application that did the same. But it still pops up on the console. I assume that the original command is marked as a console application, and therefore Windows automatically provides it with a console window to run. I also tried replacing my initial call to _exec () with a call to system (), just in case. No help.

Does anyone know how I can remove this console window?

Here is my code:

int APIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       char*    lpCmdLine,
                       int       nCmdShow)
{
    char *argString, *executable;
    // argString and executable are retrieved here

    std::vector< std::string > newArgs;
    // newArgs gets set up with the intended arguments here

    char const ** newArgsP = new char const*[newArgs.size() + 1];
    for (unsigned int i = 0; i < newArgs.size(); ++i)
    {
        newArgsP[i] = newArgs[i].c_str();
    }
    newArgsP[newArgs.size()] = NULL;

    int rv = _execv(executable, newArgsP);
    if (rv)
    {
        return -1;
    }
}
+3
source share
3 answers

CreateProcess execve. dwCreationFlags CREATE_NO_WINDOW. .

.

STARTUPINFO startInfo = {0};
PROCESS_INFORMATION procInfo;
TCHAR cmdline[] = _T("\"path\\to\\app.exe\" \"arg1\" \"arg2\"");
startInfo.cb = sizeof(startInfo);
if(CreateProcess(_T("path\\to\\app.exe"), cmdline, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &startInfo, &procInfo))
{ 
   CloseHandle(procInfo.hProcess);
   CloseHandle(procInfo.hThread);
}
+4

, , MSDN, , .NET. ( , , .)

 System::String^ command = gcnew System::String(executable);
 System::Diagnostics::Process^ myProcess = gcnew Process;
 myProcess->StartInfor->FileName = command;
 myProcess->StartInfo->UseShellExecute = false; //1
 myProcess->StartInfo->CreateNowindow = true;   //2
 myProcess->Start();

, // 1 //2, . .

, , , , .

+1

(.. Windows). - - , WinMain, - . , printf . , exec(), system().

0
source

Source: https://habr.com/ru/post/1726227/


All Articles