1.正常winform程序中启动 外部程序:
//打开程序的地址
var fileName = path;
//判断文件是否存在,如果不存在,返回 false
Console.WriteLine(File.Exists(fileName));
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = fileName;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
Log.Info("打开程序成功");
2. 在windows服务中需要使用特定的代码才能启动外部程序并显示程序的UI界面:
public static bool StartProcessAndBypassUAC(String applicationName, String command, out PROCESS_INFORMATION procInfo)
{
// 登陆进程的id
uint winlogonPid = 0;
// 用户的访问令牌 登陆进程的访问令牌 登陆进程的句柄
IntPtr hUserTokenDup = IntPtr.Zero, hPToken = IntPtr.Zero, hProcess = IntPtr.Zero;
// 进程信息
procInfo = new PROCESS_INFORMATION();
// obtain the currently active session id; every logged on user in the system has a unique session id
// 获取当前活动会话id;系统中每个登录的用户都有一个唯一的会话id
uint dwSessionId = WTSGetActiveConsoleSessionId();
// obtain the process id of the winlogon process that is running within the currently active session
//获取当前活动会话中运行的winlogon进程(身份认证)的进程id即用户登陆窗口进程id
Process[] processes = Process.GetProcessesByName("winlogon");
foreach (Process p in processes)
{
if ((uint)p.SessionId == dwSessionId)
{
winlogonPid = (uint)p.Id;
}
}
// obtain a handle to the winlogon process
// 获取winlogon进程的句柄
hProcess = OpenProcess(MAXIMUM_ALLOWED, false, winlogonPid);
// obtain a handle to the access token of the winlogon process
// 获取winlogon进程访问令牌的句柄
if (!OpenProcessToken(hProcess, TOKEN_DUPLICATE, ref hPToken))
{
// 使登陆进程句柄无效
CloseHandle(hProcess);
return false;
}
// Security attibute structure used in DuplicateTokenEx and CreateProcessAsUser
// I would prefer to not have to use a security attribute variable and to just
// simply pass null and inherit (by default) the security attributes
// of the existing token. However, in C# structures are value types and therefore
// cannot be assigned the null value.
// DuplicateTokenEx和CreateProcessAsUser中使用的安全属性结构我希望不必使用安全属性变量
//只需传递null并继承(默认情况下)安全属性现有令牌的。然而,在C语言中,结构是值类型,因此无法分配空值。
// 初始化安全属性结构体
SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
sa.Length = Marshal.SizeOf(sa);
// copy the access token of the winlogon process; the newly created token will be a primary token
// 复制winlogon进程的访问令牌;新创建的令牌将是主令牌
if (!DuplicateTokenEx(hPToken, MAXIMUM_ALLOWED, ref sa, (int)SECURITY_IMPERSONATION_LEVEL.SecurityIdentification, (int)TOKEN_TYPE.TokenPrimary, ref hUserTokenDup))
{
// 使登陆进程句柄无效
CloseHandle(hProcess);
// 使登陆进程的令牌句柄无效
CloseHandle(hPToken);
return false;
}
// By default CreateProcessAsUser creates a process on a non-interactive window station, meaning
// the window station has a desktop that is invisible and the process is incapable of receiving
// user input. To remedy this we set the lpDesktop parameter to indicate we want to enable user
// interaction with the new process.
// 默认情况下,CreateProcessAsUser在非交互式窗口站上创建进程,这意味着窗口站有一个不可见的桌面,进程无法接收用户输入。
// 为了解决这个问题,我们设置了lpDesktop参数来表示我们想要启用用户与新流程的互动。
// 用于指定新进程的主视窗特性的结构体
STARTUPINFO si = new STARTUPINFO();
si.cb = (int)Marshal.SizeOf(si);
// 交互式窗口站参数;基本上,这表明创建的进程可以在桌面上显示GUI
si.lpDesktop = @"winsta0\default"; // interactive window station parameter; basically this indicates that the process created can display a GUI on the desktop
// flags that specify the priority and creation method of the process
// 指定进程的优先级和创建方法的标志
int dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE;
// create a new process in the current user's logon session
// 在当前用户的登录会话中创建新进程
bool result = CreateProcessAsUser(hUserTokenDup, // client's access token 用户的访问令牌
applicationName, // file to execute 要执行的文件
command, // command line 命令行
ref sa, // pointer to process SECURITY_ATTRIBUTES 指向进程SECURITY_属性的指针
ref sa, // pointer to thread SECURITY_ATTRIBUTES 指向线程SECURITY_属性的指针
false, // handles are not inheritable 句柄不可继承
dwCreationFlags, // creation flags 创建标志
IntPtr.Zero, // pointer to new environment block 指向新环境块的指针
null, // name of current directory 当前目录的名称
ref si, // pointer to STARTUPINFO structure 指向STARTUPINFO结构的指针
out procInfo // receives information about new process 接收有关新进程的信息
);
// invalidate the handles 使句柄无效
CloseHandle(hProcess);
CloseHandle(hPToken);
CloseHandle(hUserTokenDup);
return result; // return the result
}
接口调用:
StartProcessAndBypassUAC(@"D:\tes.exe", string.Empty, out pInfo);
备注:在我的电脑上能正常启动程序,但放到另外的电脑上不能启动,我怀疑是 si.lpDesktop = @"winsta0\default";这句代码的问题,如果没有这句代码,程序会以当前桌面为基础创建出UI程序进程,我没有测试过,如果你遇到这个问题可以修改下看看;
3. windows服务中关闭程序:
1)通过获取进程列表数据,删除进程:
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
if (p.ProcessName == path)
{
p.Kill();
}
}
2) 根据2中的pInfo结构体中的id删除:
Process p = Process.GetProcessById((int)pInfo.dwProcessId);
p.Kill();