// hidecon.cpp : released to public domain by author, Dave Hart // davehart@pobox.com // // hidecon runs the program specified on its command line // forcing it to start up minimized and without GUI activation. // This is particularly useful with text-mode (console or "DOS" // programs) being launched by Windows' Task Scheduler which // you would rather not have appear in front of whatever you // happen to be doing at the time. Written to wrap perl.exe, // a win32 console app, so it can run mrtg script every 5 minutes // and I can keep my sanity. This wouldn't be needed if the // Task Scheduler itself offered options on how to display the // program in question. // // N.B. This program goes away immediately after launching // the specified child program. If you change the code to // wait for the child to terminate, you should take into // consideration that this code runs as a win32 GUI process, // so if you will be blocking to wait for the child to // complete, don't use the primary thread to do that. Instead // enter a GetMessage loop in the primary thread after starting // another thread to do the wait and then PostThreadMessage to // let the original thread know to exit the GetMessage loop // and exit WinMain to finish the process. Or you could get // fancy and do it all with one thread and something like // WaitForMsgAndEvent paired with a PeekMessage loop. If // you simply block the initial thread of this GUI process, // things can hang up as our creator does WaitForInputIdle() // inside WinExec() or system(), expecting us to be a GUI // program even though we don't actually create any windows // or have any capability to receive messages except those // sent using PostThreadMessage -- nonetheless we need to let // the GUI system know we are input idle by waiting for the // (likely nonexistent) incoming messages. // // change it (or not!) and copyright (or left!!) it // to your heart's content :p // #include #include #include #include STARTUPINFO si; PROCESS_INFORMATION pi; DWORD dwExitCode; int CALLBACK WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR pszCommandLine, int nShow) { // for win9x SetProcessShutdownParameters(0x101, SHUTDOWN_NORETRY); ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = SW_SHOWMINNOACTIVE; // the whole point if (! CreateProcess( NULL, pszCommandLine, NULL, // process security attributes NULL, // thread security attributes TRUE, // bInherhitHandles CREATE_DEFAULT_ERROR_MODE, NULL, // environment NULL, // curdir &si, &pi )) { return GetLastError(); } CloseHandle(pi.hThread); CloseHandle(pi.hProcess); return 0; }