在WM5.0中,System.Diagnostics.Process.start()的替代方法
Posted on 2007-03-13 00:08 Paker Liu 阅读(1482) 评论(1) 收藏 举报
使用的操作系统是 Windows Moible 2003 SE,在这个版本的操作系统上,不能使用System.Diagnostics.Process.start()运行程序
1
using System;
2
using System.Runtime.InteropServices;
3
4
class ExecSample
5
{
6
public class ProcessInfo
7
{
8
public IntPtr hProcess;
9
public IntPtr hThread;
10
public Int32 ProcessId;
11
public Int32 ThreadId;
12
}
13
14
[DllImport("CoreDll.DLL", SetLastError=true)]
15
private extern static int CreateProcess( String imageName,
16
String cmdLine,
17
IntPtr lpProcessAttributes,
18
IntPtr lpThreadAttributes,
19
Int32 boolInheritHandles,
20
Int32 dwCreationFlags,
21
IntPtr lpEnvironment,
22
IntPtr lpszCurrentDir,
23
byte [] si,
24
ProcessInfo pi );
25
26
[DllImport("CoreDll.dll")]
27
private extern static Int32 GetLastError();
28
29
public static bool CreateProcess( String ExeName, String CmdLine, ProcessInfo pi )
30
{
31
if ( pi == null )
32
pi = new ProcessInfo();
33
34
byte [] si = new byte[128];
35
return CreateProcess(ExeName, CmdLine, IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, si, pi) != 0;
36
}
37
38
public static void Main()
39
{
40
String progPath = "Calc.exe";
41
Console.WriteLine("Launching \"{0}\"
", progPath);
42
ProcessInfo pi = new ProcessInfo();
43
if ( CreateProcess(progPath, "", pi) )
44
Console.WriteLine("Success! (pid = {0})", pi.ProcessId.ToString());
45
else
46
Console.WriteLine("Failed (system error = {0})", GetLastError().ToString());
47
}
48
}
49
using System; 2
using System.Runtime.InteropServices; 3
4
class ExecSample 5
{ 6
public class ProcessInfo 7
{ 8
public IntPtr hProcess; 9
public IntPtr hThread; 10
public Int32 ProcessId; 11
public Int32 ThreadId; 12
} 13
14
[DllImport("CoreDll.DLL", SetLastError=true)] 15
private extern static int CreateProcess( String imageName, 16
String cmdLine, 17
IntPtr lpProcessAttributes, 18
IntPtr lpThreadAttributes, 19
Int32 boolInheritHandles, 20
Int32 dwCreationFlags, 21
IntPtr lpEnvironment, 22
IntPtr lpszCurrentDir, 23
byte [] si, 24
ProcessInfo pi ); 25
26
[DllImport("CoreDll.dll")] 27
private extern static Int32 GetLastError(); 28
29
public static bool CreateProcess( String ExeName, String CmdLine, ProcessInfo pi ) 30
{ 31
if ( pi == null ) 32
pi = new ProcessInfo(); 33
34
byte [] si = new byte[128]; 35
return CreateProcess(ExeName, CmdLine, IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, si, pi) != 0; 36
} 37
38
public static void Main() 39
{ 40
String progPath = "Calc.exe"; 41
Console.WriteLine("Launching \"{0}\"
", progPath); 42
ProcessInfo pi = new ProcessInfo(); 43
if ( CreateProcess(progPath, "", pi) ) 44
Console.WriteLine("Success! (pid = {0})", pi.ProcessId.ToString()); 45
else 46
Console.WriteLine("Failed (system error = {0})", GetLastError().ToString()); 47
} 48
} 49

但是可惜的是,这个方法运行 exe文件没有问题, 运行程序安装包CAB文件就返回 193 错误
使用 CreateProcess(@"\Windows\wceload.exe", "\"" + save_path + "\"", pi) 可以解决运行其他格式文件(如.cab)的问题
浙公网安备 33010602011771号