|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * Switches |
|
16 * System Includes |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 |
|
22 #include <stdio.h> |
|
23 #include <stdlib.h> |
|
24 #include <string.h> |
|
25 #include <assert.h> |
|
26 #include <windows.h> |
|
27 |
|
28 /******************************************************************************* |
|
29 * |
|
30 * Local Includes |
|
31 * |
|
32 ******************************************************************************/ |
|
33 #include "proclib.h" |
|
34 |
|
35 /******************************************************************************* |
|
36 * |
|
37 * Definitions |
|
38 * |
|
39 ******************************************************************************/ |
|
40 |
|
41 /******************************************************************************* |
|
42 * |
|
43 * CAProcess |
|
44 * |
|
45 ******************************************************************************/ |
|
46 CAProcess::CAProcess() |
|
47 { |
|
48 memset( &iProcessInfo, 0, sizeof(iProcessInfo) ); |
|
49 iProcessState = PS_INIT; |
|
50 } |
|
51 |
|
52 CAProcess::~CAProcess() |
|
53 { |
|
54 // assert( (iProcessState == PS_INIT) || (iProcessState == PS_REMOVED) ); |
|
55 } |
|
56 |
|
57 |
|
58 /******************************************************************************* |
|
59 * |
|
60 * StartProcess() |
|
61 * |
|
62 ******************************************************************************/ |
|
63 int CAProcess::StartProcess( char *aCommandLine ) |
|
64 { |
|
65 int err; |
|
66 STARTUPINFO info; |
|
67 |
|
68 // check that the commandline is valid |
|
69 assert( aCommandLine != NULL ); |
|
70 |
|
71 // verify the state |
|
72 assert( iProcessState == PS_INIT ); |
|
73 |
|
74 // set the startup info to zero (required even though empty) |
|
75 memset( &info, 0, sizeof(info) ); |
|
76 info.cb = sizeof(info); |
|
77 |
|
78 // create the process |
|
79 err = CreateProcess( NULL, aCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &info, &iProcessInfo ); |
|
80 if( err == 0 ) { |
|
81 assert( iProcessInfo.hProcess == NULL ); |
|
82 return GetLastError(); |
|
83 } |
|
84 |
|
85 // update the state |
|
86 iProcessState = PS_STARTED; |
|
87 |
|
88 // done |
|
89 return 0; |
|
90 } |
|
91 |
|
92 /******************************************************************************* |
|
93 * |
|
94 * StopProcess() |
|
95 * |
|
96 ******************************************************************************/ |
|
97 int CAProcess::StopProcess( ) |
|
98 { |
|
99 int err, rv = 0; |
|
100 |
|
101 // check the state |
|
102 assert( iProcessState == PS_STARTED ); |
|
103 |
|
104 // terminate the process |
|
105 err = TerminateProcess( iProcessInfo.hProcess, -1 ); |
|
106 if( err == 0 ) { |
|
107 rv = GetLastError(); |
|
108 } |
|
109 |
|
110 // update the state |
|
111 iProcessState = PS_STOPPED; |
|
112 return rv; |
|
113 } |
|
114 |
|
115 /******************************************************************************* |
|
116 * |
|
117 * WaitForProcess() |
|
118 * |
|
119 ******************************************************************************/ |
|
120 int CAProcess::WaitForProcess() |
|
121 { |
|
122 int err, rv = 0; |
|
123 |
|
124 // check the state |
|
125 assert( (iProcessState == PS_STOPPED) || (iProcessState == PS_STARTED) ); |
|
126 |
|
127 // now wait |
|
128 err = WaitForSingleObject( iProcessInfo.hProcess, INFINITE ); |
|
129 if( err != WAIT_OBJECT_0 ) { |
|
130 rv = err; |
|
131 } |
|
132 |
|
133 // update the state |
|
134 iProcessState = PS_REMOVED; |
|
135 return rv; |
|
136 } |
|
137 |
|
138 |
|
139 /******************************************************************************* |
|
140 * |
|
141 * GetProcessStatus() |
|
142 * |
|
143 ******************************************************************************/ |
|
144 TProcessStatus CAProcess::GetProcessStatus() |
|
145 { |
|
146 int err; |
|
147 |
|
148 // if the state is not started then return |
|
149 if( iProcessState != PS_STARTED ) { |
|
150 return iProcessState; |
|
151 } |
|
152 |
|
153 // otherwise see if the process is still running or has stopped itself |
|
154 err = WaitForSingleObject( iProcessInfo.hProcess, 0 ); |
|
155 if( err != WAIT_TIMEOUT ) { |
|
156 iProcessState = PS_STOPPED; |
|
157 } |
|
158 |
|
159 // return the status |
|
160 return iProcessState; |
|
161 } |