1 : /**********************************************************************
2 : * $Id: cpl_spawn.cpp 25133 2012-10-15 20:40:26Z rouault $
3 : *
4 : * Project: CPL - Common Portability Library
5 : * Purpose: Implement CPLSystem().
6 : * Author: Even Rouault, <even dot rouault at mines dash paris dot org>
7 : *
8 : **********************************************************************
9 : * Copyright (c) 2012,Even Rouault
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "cpl_conv.h"
31 :
32 : CPL_CVSID("$Id: cpl_spawn.cpp 25133 2012-10-15 20:40:26Z rouault $");
33 :
34 : #if defined(WIN32)
35 :
36 : #include <windows.h>
37 :
38 : /************************************************************************/
39 : /* CPLSystem() */
40 : /************************************************************************/
41 :
42 : /**
43 : * Runs an executable in another process.
44 : *
45 : * This function runs an executable, wait for it to finish and returns
46 : * its exit code.
47 : *
48 : * It is implemented as CreateProcess() on Windows platforms, and system()
49 : * on other platforms.
50 : *
51 : * @param pszApplicationName the lpApplicationName for Windows (might be NULL),
52 : * or ignored on other platforms.
53 : * @param pszCommandLine the command line, starting with the executable name
54 : *
55 : * @return the exit code of the spawned process, or -1 in case of error.
56 : */
57 :
58 : int CPLSystem( const char* pszApplicationName, const char* pszCommandLine )
59 : {
60 : int nRet = -1;
61 : PROCESS_INFORMATION processInfo;
62 : STARTUPINFO startupInfo;
63 : ZeroMemory( &processInfo, sizeof(PROCESS_INFORMATION) );
64 : ZeroMemory( &startupInfo, sizeof(STARTUPINFO) );
65 : startupInfo.cb = sizeof(STARTUPINFO);
66 :
67 : char* pszDupedCommandLine = (pszCommandLine) ? CPLStrdup(pszCommandLine) : NULL;
68 :
69 : if( !CreateProcess( pszApplicationName,
70 : pszDupedCommandLine,
71 : NULL,
72 : NULL,
73 : FALSE,
74 : CREATE_NO_WINDOW|NORMAL_PRIORITY_CLASS,
75 : NULL,
76 : NULL,
77 : &startupInfo,
78 : &processInfo) )
79 : {
80 : DWORD err = GetLastError();
81 : CPLDebug("CPL", "'%s' failed : err = %d", pszCommandLine, err);
82 : nRet = -1;
83 : }
84 : else
85 : {
86 : WaitForSingleObject( processInfo.hProcess, INFINITE );
87 :
88 : DWORD exitCode;
89 :
90 : // Get the exit code.
91 : int err = GetExitCodeProcess(processInfo.hProcess, &exitCode);
92 :
93 : CloseHandle(processInfo.hProcess);
94 : CloseHandle(processInfo.hThread);
95 :
96 : if( !err )
97 : {
98 : CPLDebug("CPL", "GetExitCodeProcess() failed : err = %d", err);
99 : }
100 : else
101 : nRet = exitCode;
102 : }
103 :
104 : CPLFree(pszDupedCommandLine);
105 :
106 : return nRet;
107 : }
108 :
109 : #else
110 :
111 : /************************************************************************/
112 : /* CPLSystem() */
113 : /************************************************************************/
114 :
115 4 : int CPLSystem( const char* pszApplicationName, const char* pszCommandLine )
116 : {
117 4 : return system(pszCommandLine);
118 : }
119 :
120 : #endif
|