3

In order to avoid tangling with interop services (beyond my understanding) I use

spawnl(P_DETACH, MyPath, "MyProg.exe", 0);

to spawn a VS unmanaged C++ command line project. (It controls an astro-camera via the manufacturer's DLL).

I don't need and don't want a window (I talk to myprog.exe using named pipes from my main GUI program).

Suppressing the window from a GUI is trivial, but in order to avoid tangling with marshalling issues (beyond my comprehension) myprog.exe must be an unmanaged native C++ command line project, not a CLI project.

There is a huge literature on suppressing the window from a batch file or from python, and closing a window in a windows project is trivial, but that is all irrelevant here.

I spawn myprog.exe detached, but that is irrelevant. Closing the console handle executes ok but does not close the window.

Any thoughts on how to either never open the black onscreen DOS box, or how to close it without closing myprog.exe?

3
  • You should probably tag this Windows to be more clear. Also, it sounds like you're more interested in a service. Commented Jul 6, 2013 at 3:08
  • 1
    Drop down a level and use CreateProcess with CREATE_NO_WINDOW in the process creation flags and/or STARTF_USESHOWWINDOW and SW_HIDE in the appropriate slots of STARTUPINFO? (This is not an answer because I don't know if it will work.) Commented Jul 6, 2013 at 3:09
  • @Taywee I tagged it as you suggested. Commented Jul 6, 2013 at 3:10

1 Answer 1

1

Try something like this:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include <stdio.h>

void system_error(char *name) {
// Retrieve, format, and print out a message from the last errror.
// The `name' that's passed should be in the form of a present tense
// noun (phrase) such as "opening file".
//
    char *ptr = NULL;
    FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM,
        0, GetLastError(), 0, (char *)&ptr, 1024, NULL);

    printf("\nError %s: %s\n", name, ptr);
    LocalFree(ptr);
}

PROCESS_INFORMATION p;

BOOL WINAPI die(DWORD reason) {
    TerminateProcess(p.hProcess, 1);
    return TRUE;
}

int main(int argc, char **argv) {

    STARTUPINFO s;

    memset(&s, 0, sizeof s);
    s.cb = sizeof(s);

    if (!CreateProcess(argv[1], argv[2], NULL, NULL, TRUE,
        DETACHED_PROCESS, NULL, NULL, &s, &p))
    {
        system_error("Spawning program");
        return 1;
    }

    SetConsoleCtrlHandler(die, TRUE);

    WaitForSingleObject(p.hProcess, INFINITE);
    return 0;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Out of curiosity, what exactly is the difference between DETACHED_PROCESS and CREATE_NO_WINDOW?
@Zack: To be honest, it's been long enough since I wrote this (or did anything very similar) that I don't remember.
this link to the create process flags on MSDN should help.
@CaptainObvlious: Already looked -- their description doesn't make the difference particularly obvious (though my dim recollection is of the difference actually being fairly substantial).
It sounds to me (from reading MSDN) that CREATE_NO_WINDOW is the one you want. It actually skips creating the window for a console application, whereas DETACHED_PROCESS simply means that the new process does not inherit its parent's console window (and leaves open the possibility for it to create its own new console window). I could be totally wrong, though; I haven't tested my interpretation.
|

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.