--- q_a/samples/startp/startp.c 2018/08/09 18:29:38 1.1.1.2 +++ q_a/samples/startp/startp.c 2018/08/09 18:30:04 1.1.1.3 @@ -1,3 +1,14 @@ + +/******************************************************************************\ +* This is a part of the Microsoft Source Code Samples. +* Copyright (C) 1993 Microsoft Corporation. +* All rights reserved. +* This source code is only intended as a supplement to +* Microsoft Development Tools and/or WinHelp documentation. +* See these sources for detailed information regarding the +* Microsoft samples programs. +\******************************************************************************/ + /******************************************************************** * This program demonstrates various CreateProcess parameters, * * including starting processes in a given priority class. This is * @@ -13,38 +24,38 @@ * see the help() function or run the program without any * * parameters to see detailed execution info. * * * -* Future enhancements: * +* Possible enhancements: * * Handle cmd.exe internal commands * ********************************************************************/ -/* Microsoft Developer Support - Copyright (c) 1992 Microsoft Corporation */ #include - +#include #include #include #include /* Standard error macro for reporting API errors */ -#define PERR(api) printf("%s: Error %d from %s on line %d\n", \ - __FILE__, GetLastError(), api, __LINE__); +#define PERR(bSuccess, api) {if (!(bSuccess)) printf("%s: Error %d from %s \ + on line %d\n", __FILE__, GetLastError(), api, __LINE__);} void help() { puts("Starts a specified program, batch, or command file."); - puts("STARTP [/Ttitle] [/Dpath] [/h] [/l] [/min] [/max] [/w]"); - puts(" [/c] [program] [parameters]"); + puts("STARTP [/Ttitle] [/Dpath] [/l] [/h] [/r] [/min] [/max] [/w]"); + puts(" [/c] [/b] [program] [parameters]"); puts("\n title Title to display in window title bar. Quote the"); puts(" entire paramter to include spaces in the title,"); puts(" i.e. startp \"/Ttest job\""); puts(" path Starting directory"); - puts(" h Set default to high priority"); puts(" l Set default to low priority"); + puts(" h Set default to high priority"); + puts(" r Set default to realtime priority"); puts(" min Start window minimized"); puts(" max Start window maximized"); puts(" w Wait for started process to end before returning"); puts(" control to the command processor. This option starts"); puts(" the process synchronously"); puts(" c Use current console instead of creating a new console"); + puts(" b Start detached with no console at all"); puts(" program A batch file or program to run as either a GUI"); puts(" application or a console application"); puts(" parameters These are the parameters passed to the program"); @@ -69,6 +80,14 @@ int main(int argc, char *argv[]) int i; char *aExt[] = { ".exe", ".com", ".bat", ".cmd" }; + /* Check to make sure we are running on Windows NT */ + if( GetVersion() & 0x80000000 ) + { + MessageBox(NULL, "Sorry, this application requires Windows NT.\n" + "This application will now terminate.", + "Error: Windows NT Required to Run", MB_OK ); + return(1); + } /* process all command-line parameters */ if (argc < 2) help(); @@ -99,6 +118,9 @@ int main(int argc, char *argv[]) case 'l': dwCreate |= IDLE_PRIORITY_CLASS; /* /l */ break; + case 'r': + dwCreate |= REALTIME_PRIORITY_CLASS; /* /r */ + break; case 'm': switch ((*argv)[2]) { @@ -120,6 +142,9 @@ int main(int argc, char *argv[]) case 'w': bWait = TRUE; /* don't end until new process ends as well */ break; + case 'b': + dwCreate |= DETACHED_PROCESS; /* start detached */ + /* detached implies no CREATE_NEW_CONSOLE so fall through */ case 'c': dwCreate &= ~CREATE_NEW_CONSOLE; /* turn off this bit */ break; @@ -172,8 +197,11 @@ int main(int argc, char *argv[]) case ERROR_DIRECTORY: puts("Error: bad starting directory"); exit(1); + case ERROR_PATH_NOT_FOUND: + puts("Error: bad path"); + exit(1); default: - PERR("CreateProcess"); + PERR(0, "CreateProcess"); exit(1); } /* switch */ } while (!fSuccess); @@ -183,8 +211,7 @@ int main(int argc, char *argv[]) { dwResult = WaitForSingleObject(pi.hProcess, /* object to wait for */ (DWORD) -1); /* timeout time */ - if (dwResult == -1) - PERR("WaitForSingleObject"); + PERR(dwResult != -1, "WaitForSingleObject"); } CloseHandle(pi.hProcess); /* close process handle or it won't die */ return(0);