|
1 /* |
|
2 * Copyright (c) 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 * |
|
16 */ |
|
17 using System; |
|
18 using System.Collections.Generic; |
|
19 using System.Text; |
|
20 using System.Threading; |
|
21 using System.Windows.Forms; |
|
22 using System.Runtime.InteropServices; |
|
23 using System.ComponentModel; |
|
24 |
|
25 namespace SymbianUtils.ProcessAndThread |
|
26 { |
|
27 public class ProcessLauncher |
|
28 { |
|
29 #region API |
|
30 public static int Launch( string aFileName, string aCommandLineArguments, string aCurrentDirectory, bool aShowWindow ) |
|
31 { |
|
32 return Launch( aFileName, aCommandLineArguments, aCurrentDirectory, aShowWindow, 0 ); |
|
33 } |
|
34 |
|
35 public static int Launch( string aFileName, string aCommandLineArguments, string aCurrentDirectory, bool aShowWindow, int aWaitTimeInMs ) |
|
36 { |
|
37 // See http://msdn.microsoft.com/en-us/library/ms682425.aspx |
|
38 // See http://msdn2.microsoft.com/en-us/library/ms682425.aspx |
|
39 // See http://www.pinvoke.net/default.aspx/kernel32/CreateProcess.html |
|
40 ProcessInformation pi = new ProcessInformation(); |
|
41 StartupInformation si = new StartupInformation(); |
|
42 if ( !aShowWindow ) |
|
43 { |
|
44 si.dwFlags = STARTF_USESHOWWINDOW; |
|
45 si.wShowWindow = SW_SHOWMINIMIZED; |
|
46 } |
|
47 |
|
48 si.cb = Marshal.SizeOf( si ); |
|
49 SecurityAttributes pSec = new SecurityAttributes(); |
|
50 pSec.nLength = Marshal.SizeOf( pSec ); |
|
51 SecurityAttributes tSec = new SecurityAttributes(); |
|
52 tSec.nLength = Marshal.SizeOf( tSec ); |
|
53 // |
|
54 int error = 0; |
|
55 bool suceeded = CreateProcess( aFileName, aCommandLineArguments, ref pSec, ref tSec, false, 0, IntPtr.Zero, aCurrentDirectory, ref si, out pi ) != false; |
|
56 if ( !suceeded ) |
|
57 { |
|
58 error = Marshal.GetLastWin32Error(); |
|
59 } |
|
60 else |
|
61 { |
|
62 if ( aWaitTimeInMs != 0 ) |
|
63 { |
|
64 // Wait |
|
65 WaitForSingleObject( pi.hProcess, aWaitTimeInMs ); |
|
66 } |
|
67 |
|
68 // Tidy up |
|
69 CloseHandle( pi.hThread ); |
|
70 CloseHandle( pi.hProcess ); |
|
71 |
|
72 int hResult = GetExitCodeProcess( pi.hProcess, ref error ); |
|
73 if ( error != 0 ) |
|
74 { |
|
75 Win32Exception exception = new Win32Exception( error ); |
|
76 throw exception; |
|
77 } |
|
78 } |
|
79 // |
|
80 return error; |
|
81 } |
|
82 #endregion |
|
83 |
|
84 #region Internal constants |
|
85 private const int STARTF_USESHOWWINDOW = 0x00000001; |
|
86 private const int SW_HIDE = 0; |
|
87 private const int SW_SHOWNORMAL = 1; |
|
88 private const int SW_SHOWMINIMIZED = 2; |
|
89 private const int SW_SHOWMAXIMIZED = 3; |
|
90 #endregion |
|
91 |
|
92 #region Internal PInvoke wrappers |
|
93 [DllImport( "kernel32.dll", SetLastError = true )] |
|
94 static extern int GetExitCodeProcess( IntPtr hProcess, ref int lpExitCode ); |
|
95 |
|
96 [DllImport( "kernel32.dll", SetLastError = true )] |
|
97 internal static extern bool CreateProcess( string lpApplicationName, |
|
98 string lpCommandLine, |
|
99 ref SecurityAttributes lpProcessAttributes, |
|
100 ref SecurityAttributes lpThreadAttributes, |
|
101 bool bInheritHandles, |
|
102 uint dwCreationFlags, |
|
103 IntPtr lpEnvironment, |
|
104 string lpCurrentDirectory, |
|
105 ref StartupInformation lpStartupInfo, |
|
106 out ProcessInformation lpProcessInformation ); |
|
107 |
|
108 [DllImport("kernel32", SetLastError=true, ExactSpelling=true)] |
|
109 internal static extern Int32 WaitForSingleObject( IntPtr handle, Int32 milliseconds ); |
|
110 |
|
111 [DllImport( "kernel32.dll", SetLastError = true )] |
|
112 [return: MarshalAs( UnmanagedType.Bool )] |
|
113 internal static extern bool CloseHandle( IntPtr hObject ); |
|
114 #endregion |
|
115 } |
|
116 |
|
117 public static class ProcessLauncherWithBlockingWait |
|
118 { |
|
119 #region API |
|
120 public static int Launch( string aFileName, string aCommandLineArguments, string aCurrentDirectory ) |
|
121 { |
|
122 return Launch( aFileName, aCommandLineArguments, aCurrentDirectory, false ); |
|
123 } |
|
124 |
|
125 public static int Launch( string aFileName, string aCommandLineArguments, string aCurrentDirectory, bool aShowWindow ) |
|
126 { |
|
127 LaunchWorkerThread worker = new LaunchWorkerThread( aFileName, aCommandLineArguments, aCurrentDirectory ); |
|
128 return worker.LaunchAndWait( aShowWindow ); |
|
129 } |
|
130 #endregion |
|
131 } |
|
132 |
|
133 #region Internal class |
|
134 internal class LaunchWorkerThread |
|
135 { |
|
136 #region Constructors |
|
137 public LaunchWorkerThread( string aFileName, string aCommandLineArguments, string aCurrentDirectory ) |
|
138 { |
|
139 iFileName = aFileName; |
|
140 iCommandLineArguments = aCommandLineArguments; |
|
141 iCurrentDirectory = aCurrentDirectory; |
|
142 } |
|
143 #endregion |
|
144 |
|
145 #region API |
|
146 public int LaunchAndWait( bool aShowWindow ) |
|
147 { |
|
148 Thread thread = new Thread( new ParameterizedThreadStart( this.ThreadFunction ) ); |
|
149 thread.Priority = ThreadPriority.BelowNormal; |
|
150 thread.Start( aShowWindow ); |
|
151 // |
|
152 while ( thread.IsAlive ) |
|
153 { |
|
154 Application.DoEvents(); |
|
155 Thread.Sleep( 250 ); |
|
156 } |
|
157 // |
|
158 return iError; |
|
159 } |
|
160 #endregion |
|
161 |
|
162 #region Internal methods |
|
163 private void ThreadFunction( object aData ) |
|
164 { |
|
165 bool showWindow = (bool) aData; |
|
166 iError = ProcessLauncher.Launch( iFileName, iCommandLineArguments, iCurrentDirectory, showWindow, (int) WAIT_INFINITE ); |
|
167 } |
|
168 #endregion |
|
169 |
|
170 #region Internal constants |
|
171 private static uint WAIT_INFINITE = 0xFFFFFFFF; |
|
172 #endregion |
|
173 |
|
174 #region Data members |
|
175 private int iError = -1; |
|
176 private readonly string iFileName; |
|
177 private readonly string iCommandLineArguments; |
|
178 private readonly string iCurrentDirectory; |
|
179 #endregion |
|
180 } |
|
181 #endregion |
|
182 } |