سؤال

I'm creating a child process using CreateProcess. I've created a pipe in the main application and I want it to be used by the child process which is not a console application.

Is there any way to do this? I can pass it using command line but this might really be a bad solution!

هل كانت مفيدة؟

المحلول

Use named pipes as explained here and pass the pipe name in the lpCcommandLine parameter of CreateProcess.

Or (not sure if this works, I never tried) set the bInheritHandles parameter of CreateProcess to TRUE and pass the handle as a hexadecimal value in the lpCcommandLine parameter.

نصائح أخرى

You don't need special tricks.
Tested: When you start a GUI application with standard io redirected to pipes, they work just like in console application:

test.bat

@echo off
gui.exe | find /v "__nonexist__"
pause

output:

WriteFile
puts

child.c:

#include <stdio.h>
#include <tchar.h>
#include <windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE h;
    TCHAR tstr[100];
    DWORD nb;
    h = GetStdHandle(STD_OUTPUT_HANDLE);
    _stprintf(tstr, _T("0x%08x"), h);
    MessageBox(NULL, tstr, _T("x"), MB_OK);
#define sWriteFile "WriteFile\n"
#define sPuts "puts\n"
    if (h) {
        WriteFile(h, sWriteFile, sizeof(sWriteFile)-1, &nb, NULL);
        fputs(sPuts, stdout);
    }
    return 0;
}

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    return _tmain(0, NULL);
}

When you start a GUI program from console, standard handles are closed, but there's a way to reopen them. This trick is used in netbeans.exe

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top