开发者

How to hide console window of subprocess?

I'm trying to write a very simple program to replace an existing executable. It should munge its arguments slightly and exec the original program with the new arguments. It's supposed to be invoked automatically and silently by a third-party library.

It runs fine, but it pops up a console window to show the output of the invoked program. I need that console window to not be there. I do not care about the program's output.

My original attempt was set up as a console application, so I thought I could fix this by writing a new Windows GUI app that did the same thing. But it still p开发者_开发百科ops up the console. I assume that the original command is marked as a console application, and so Windows automatically gives it a console window to run in. I also tried replacing my original call to _exec() with a call to system(), just in case. No help.

Does anyone know how I can make this console window go away?

Here's 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;
    }
}


Use the CreateProcess function instead of execve. For the dwCreationFlags paramter pass the CREATE_NO_WINDOW flag. You will also need to pass the command line as a string as well.

e.g.

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);
}


Aha, I think I found the answer on MSDN, at least if I'm prepared to use .NET. (I don't think I'm really supposed to, but I'll ignore that for now.)

 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();

It's those two lines marked //1 and //2 that are important. Both need to be present.

I really don't understand what's going on here, but it seems to work.


You need to create a non-console application (i.e. a Windows GUI app). If all this app does is some processing of files or whatever, you won't need to have a WinMain, register any windows or have a message loop - just write your code as for a console app. Of course, you won't be able to use printf et al. And when you come to execute it, use the exec() family of functions, not system().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜