C#实现程序开机自启动
随着服务器应用程序越来越多,手动去一个个启动不现实,既费时,也费力。如果使用自带的定时任务和注册表,有些bat无法正常调用。
核心代码如下:
1 /// <summary> 2 /// 进程操作类 3 /// </summary> 4 public class ProcessManage 5 { 6 private static Log4Net log = new Log4Net(); 7 private static string runPath = string.Empty; 8 private static string filename = string.Empty; 9 /// <summary> 10 /// 停止所有程序 11 /// </summary> 12 public static void StopProcess() 13 { 14 try 15 { 16 if (null != AppConfig.ItemConfigList && 0 != AppConfig.ItemConfigList.Count) 17 { 18 //获取所有进程程序 19 Process[] p = Process.GetProcesses(); 20 foreach (Process pro in p) 21 { 22 foreach (ItemConfig itemconfig in AppConfig.ItemConfigList) 23 { 24 if (itemconfig.RunPath.LastIndexOf(pro.ProcessName) > 0) 25 { 26 //关闭程序 27 pro.Kill(); 28 pro.WaitForExit(itemconfig.Time * 1000); 29 log.Log4NetLogger(pro.ProcessName + "关闭," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.sss")); 30 break; 31 } 32 } 33 } 34 } 35 } 36 catch (Exception ex) 37 { 38 log.WriteException(ex, "StopProcess"); 39 } 40 } 41 42 /// <summary> 43 /// 判断进程是否存在 44 /// </summary> 45 /// <param name="appName">程序名</param> 46 /// <returns></returns> 47 public static bool CheckProcessExist(string appName) 48 { 49 bool flag = false; 50 try 51 { 52 if (!string.IsNullOrEmpty(appName)) 53 { 54 //获取所有进程程序 55 Process[] p = Process.GetProcesses(); 56 foreach (Process pro in p) 57 { 58 if (appName.LastIndexOf(pro.ProcessName) > 0) 59 { 60 flag = true; 61 break; 62 } 63 } 64 } 65 return flag; 66 } 67 catch (Exception ex) 68 { 69 log.WriteException(ex, "CheckProcessExist"); 70 return false; 71 } 72 } 73 74 /// <summary> 75 /// 开启所有程序 76 /// </summary> 77 public static void StartProcess() 78 { 79 //停止服务 80 StopProcess(); 81 try 82 { 83 #region 打开程序 84 if (null != AppConfig.ItemConfigList && 0 != AppConfig.ItemConfigList.Count) 85 { 86 foreach (ItemConfig itemconfig in AppConfig.ItemConfigList) 87 { 88 //不考虑win7/8/2008的情况 89 //StartExe(itemconfig); 90 91 lock (itemconfig) 92 { 93 switch (System.IO.Path.GetExtension(itemconfig.RunPath)) 94 { 95 case ".exe": 96 //考虑win7/8/2008的情况 97 StartExeByWin7(itemconfig); 98 break; 99 case ".bat": 100 StartBat(itemconfig); 101 break; 102 } 103 104 } 105 if (itemconfig.Time != 0) 106 Thread.Sleep(itemconfig.Time * 1000); 107 else 108 Thread.Sleep(2000); 109 } 110 } 111 #endregion 112 } 113 catch (Exception ex) 114 { 115 log.WriteException(ex, "StopProcess"); 116 } 117 } 118 119 /// <summary> 120 /// 启动程序 121 /// </summary> 122 /// <param name="itemconfig"></param> 123 /// <returns></returns> 124 private static bool StartExeByWin7(ItemConfig itemconfig) 125 { 126 try 127 { 128 if (string.IsNullOrEmpty(itemconfig.RunPath)) 129 { 130 log.Log4NetLogger("程序启动路径为空," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.sss")); 131 return false; 132 } 133 if (!System.IO.File.Exists(itemconfig.RunPath)) 134 { 135 log.Log4NetLogger(itemconfig.RunPath + " 程序启动程序不存在1," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.sss")); 136 return false; 137 } 138 runPath = itemconfig.RunPath; 139 filename = System.IO.Path.GetFileName(runPath); 140 ProcessManage.CreateProcess(runPath, itemconfig.Argument, runPath.Replace(filename, ""));//调用系统exe程序 141 if (CheckProcessExist(runPath)) 142 { 143 log.Log4NetLogger(itemconfig.RunPath + " " + "程序开始启动," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.sss")); 144 return true; 145 } 146 else 147 { 148 log.Log4NetLogger(itemconfig.RunPath + " " + "程序开始启动失败," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.sss")); 149 return false; 150 } 151 } 152 catch (Exception ex) 153 { 154 log.WriteException(ex, "StartExeByWin7"); 155 return false; 156 } 157 158 } 159 160 private static bool StartBat(ItemConfig itemconfig) 161 { 162 try 163 { 164 if (string.IsNullOrEmpty(itemconfig.RunPath)) 165 { 166 log.Log4NetLogger("bat启动路径为空," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.sss")); 167 return false; 168 } 169 if (!System.IO.File.Exists(itemconfig.RunPath)) 170 { 171 log.Log4NetLogger("bat启动程序不存在," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.sss")); 172 return false; 173 } 174 175 Process proc = new Process(); 176 proc.StartInfo.WorkingDirectory = itemconfig.RunPath.Replace(System.IO.Path.GetFileName(itemconfig.RunPath), ""); 177 proc.StartInfo.FileName = System.IO.Path.GetFileName(itemconfig.RunPath); 178 proc.StartInfo.Arguments = itemconfig.Argument; 179 proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal; 180 proc.Start(); 181 log.Log4NetLogger(System.IO.Path.GetFileName(itemconfig.RunPath) + "bat开始启动," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.sss")); 182 return true; 183 } 184 catch (Exception ex) 185 { 186 log.WriteException(ex, "StartBat"); 187 return false; 188 } 189 } 190 191 /// <summary> 192 /// 启动程序 193 /// </summary> 194 /// <param name="itemconfig"></param> 195 /// <returns></returns> 196 private static bool StartExe(ItemConfig itemconfig) 197 { 198 try 199 { 200 if (string.IsNullOrEmpty(itemconfig.RunPath)) 201 { 202 log.Log4NetLogger("程序启动路径为空," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.sss")); 203 return false; 204 } 205 if (!System.IO.File.Exists(itemconfig.RunPath)) 206 { 207 log.Log4NetLogger("程序启动程序不存在," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.sss")); 208 return false; 209 } 210 211 Process proc = new Process(); 212 proc.StartInfo.FileName = itemconfig.RunPath; 213 proc.StartInfo.UseShellExecute = false; 214 proc.StartInfo.RedirectStandardInput = true; 215 proc.StartInfo.RedirectStandardError = true; 216 proc.StartInfo.CreateNoWindow = true; 217 proc.Start(); 218 if (OneInstance.IsFirst(proc.StartInfo.FileName)) 219 { 220 log.Log4NetLogger(proc.StartInfo.FileName + "程序开始启动," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.sss")); 221 return true; 222 } 223 else 224 { 225 log.Log4NetLogger(proc.StartInfo.FileName + "程序开始启动失败," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.sss")); 226 return false; 227 } 228 } 229 catch (Exception ex) 230 { 231 log.WriteException(ex, "StartExe"); 232 return false; 233 } 234 } 235 236 /// <summary> 237 /// 获取程序是否启动 238 /// </summary> 239 /// <returns></returns> 240 public static bool GetRunState() 241 { 242 bool flag = false; 243 if (null != AppConfig.ItemConfigList && 0 != AppConfig.ItemConfigList.Count) 244 { 245 var list = AppConfig.ItemConfigList.Where(q => System.IO.Path.GetExtension(q.RunPath) != ".bat").ToList(); 246 if (list.Count() == 0) 247 return false; 248 //获取所有进程程序 249 Process[] p = Process.GetProcesses(); 250 foreach (Process pro in p) 251 { 252 foreach (ItemConfig itemconfig in list) 253 { 254 if (itemconfig.RunPath.LastIndexOf(pro.ProcessName) > 0) 255 { 256 return true; 257 } 258 } 259 } 260 } 261 return flag; 262 } 263 264 #region 判断window服务是否存在 265 public static bool ServiceIsExisted(string serviceName) 266 { 267 ServiceController[] services = ServiceController.GetServices(); 268 foreach (ServiceController s in services) 269 { 270 if (s.ServiceName == serviceName) 271 { 272 return true; 273 } 274 } 275 return false; 276 } 277 public static bool ServiceIsExisted(string serviceName, out bool isRun) 278 { 279 isRun = false; 280 ServiceController[] services = ServiceController.GetServices(); 281 foreach (ServiceController s in services) 282 { 283 if (s.ServiceName == serviceName) 284 { 285 if (s.Status == System.ServiceProcess.ServiceControllerStatus.Running) 286 { 287 isRun = true; 288 return true; 289 } 290 else 291 { 292 isRun = false; 293 return true; 294 } 295 } 296 } 297 return false; 298 } 299 #endregion 300 301 #region 安装服务 302 public static bool InstallService(string serviceName, string servicePath, string serviceDes) 303 { 304 string comString = "/c sc create {0} binPath= \"{1}\" type= share start= auto displayname= \"{2}\""; 305 comString = string.Format(comString, serviceName, servicePath, serviceDes); 306 System.Diagnostics.ProcessStartInfo processInfo = new System.Diagnostics.ProcessStartInfo(); 307 processInfo.FileName = "cmd.exe"; 308 processInfo.UseShellExecute = false; 309 processInfo.RedirectStandardError = true; 310 processInfo.RedirectStandardInput = true; 311 processInfo.RedirectStandardOutput = true; 312 processInfo.CreateNoWindow = true; 313 processInfo.Arguments = comString; 314 System.Diagnostics.Process objProcess = System.Diagnostics.Process.Start(processInfo); 315 String stateString = objProcess.StandardOutput.ReadToEnd(); 316 objProcess.Refresh(); 317 if (stateString.IndexOf("成功") > -1) 318 { 319 return SetServiceInteract(serviceName); 320 } 321 else 322 { 323 324 return false; 325 } 326 327 } 328 #endregion 329 330 #region 设置与桌面交互 331 public static bool SetServiceInteract(string serviceName) 332 { 333 string comString = "/c sc config {0} type= interact type= own"; 334 comString = string.Format(comString, serviceName); 335 System.Diagnostics.ProcessStartInfo processInfo = new System.Diagnostics.ProcessStartInfo(); 336 processInfo.FileName = "cmd.exe"; 337 processInfo.UseShellExecute = false; 338 processInfo.RedirectStandardError = true; 339 processInfo.RedirectStandardInput = true; 340 processInfo.RedirectStandardOutput = true; 341 processInfo.CreateNoWindow = true; 342 processInfo.Arguments = comString; 343 System.Diagnostics.Process objProcess = System.Diagnostics.Process.Start(processInfo); 344 String stateString = objProcess.StandardOutput.ReadToEnd(); 345 objProcess.Refresh(); 346 if (stateString.IndexOf("成功") > -1) 347 { 348 return true; 349 } 350 else 351 { 352 353 return false; 354 } 355 356 } 357 #endregion 358 359 #region 卸载windows服务 360 public static bool UnInstallService(string serviceName) 361 { 362 string comString = "/c sc delete {0}"; 363 comString = string.Format(comString, serviceName); 364 System.Diagnostics.ProcessStartInfo processInfo = new System.Diagnostics.ProcessStartInfo(); 365 processInfo.FileName = "cmd.exe"; 366 processInfo.UseShellExecute = false; 367 processInfo.RedirectStandardError = true; 368 processInfo.RedirectStandardInput = true; 369 processInfo.RedirectStandardOutput = true; 370 processInfo.CreateNoWindow = true; 371 processInfo.Arguments = comString; 372 System.Diagnostics.Process objProcess = System.Diagnostics.Process.Start(processInfo); 373 String stateString = objProcess.StandardOutput.ReadToEnd(); 374 375 if (stateString.IndexOf("成功") > -1) 376 { 377 return true; 378 } 379 else 380 { 381 return false; 382 } 383 } 384 #endregion 385 386 #region 启动服务 387 public static bool StartService(string serviceName) 388 { 389 bool flag = true; 390 if (ServiceIsExisted(serviceName)) 391 { 392 System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName); 393 if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending) 394 { 395 service.Start(); 396 service.Refresh(); 397 TimeSpan timeout = TimeSpan.FromSeconds(60); 398 service.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running, timeout); 399 if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running) 400 flag = false; 401 } 402 } 403 return flag; 404 } 405 #endregion 406 407 #region 停止服务 408 public static bool StopService(string serviceName) 409 { 410 bool flag = true; 411 if (ServiceIsExisted(serviceName)) 412 { 413 System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName); 414 if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running) 415 { 416 service.Stop(); 417 service.Refresh(); 418 TimeSpan timeout = TimeSpan.FromSeconds(60); 419 service.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped, timeout); 420 if (service.Status != System.ServiceProcess.ServiceControllerStatus.Stopped) 421 flag = false; 422 } 423 } 424 return flag; 425 } 426 #endregion 427 428 /// <summary> 429 /// 调用批处理文件 430 /// </summary> 431 /// <param name="batName"></param> 432 /// <param name="isCheck"></param> 433 public static void RunBat(string batName, bool isCheck) 434 { 435 try 436 { 437 string filePath = string.Format("{0}\\{1}", Application.StartupPath, batName); 438 if (ProcessManage.ServiceIsExisted("AutoRunService") == isCheck) 439 { 440 if (isCheck) 441 { 442 string temp = string.Format("当前{0}注册服务不存在,请注册", batName); 443 (new Log4Net()).Log4NetLogger(temp); 444 } 445 else 446 { 447 string temp = string.Format("当前{0}注册服务已存在,请检查", batName); 448 (new Log4Net()).Log4NetLogger(temp); 449 } 450 return; 451 } 452 string CurrentDirectory = System.Environment.CurrentDirectory + "\\"; 453 System.Environment.CurrentDirectory = CurrentDirectory; //string.Format("{0}\\", Application.StartupPath); 454 Process process = new Process(); 455 process.StartInfo.UseShellExecute = false; 456 process.StartInfo.FileName = batName; 457 process.StartInfo.CreateNoWindow = true; 458 process.Start(); 459 if (isCheck) 460 { 461 (new Log4Net()).Log4NetLogger("RunBat"); 462 } 463 } 464 catch (Exception ex) 465 { 466 (new Log4Net()).WriteException(ex, "RunBat"); 467 } 468 } 469 470 #region 穿透Session 0 隔离解决win7\8\2008问题 471 public static IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero; 472 473 public static void ShowMessageBox(string message, string title) 474 { 475 int resp = 0; 476 WTSSendMessage( 477 WTS_CURRENT_SERVER_HANDLE, 478 WTSGetActiveConsoleSessionId(), 479 title, title.Length, 480 message, message.Length, 481 0, 0, out resp, false); 482 } 483 484 [DllImport("kernel32.dll", SetLastError = true)] 485 public static extern int WTSGetActiveConsoleSessionId(); 486 487 [DllImport("wtsapi32.dll", SetLastError = true)] 488 public static extern bool WTSSendMessage( 489 IntPtr hServer, 490 int SessionId, 491 String pTitle, 492 int TitleLength, 493 String pMessage, 494 int MessageLength, 495 int Style, 496 int Timeout, 497 out int pResponse, 498 bool bWait); 499 500 public static void CreateProcess(string app, string argument, string path) 501 { 502 IntPtr hToken = WindowsIdentity.GetCurrent().Token; 503 IntPtr hDupedToken = IntPtr.Zero; 504 505 PROCESS_INFORMATION pi = new PROCESS_INFORMATION(); 506 try 507 { 508 bool result; 509 510 511 SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES(); 512 sa.Length = Marshal.SizeOf(sa); 513 514 STARTUPINFO si = new STARTUPINFO(); 515 si.cb = Marshal.SizeOf(si); 516 si.lpDesktop = "winsta0\\default"; 517 si.lpTitle = null; 518 519 int dwSessionID = WTSGetActiveConsoleSessionId(); 520 result = WTSQueryUserToken(dwSessionID, out hToken); 521 522 if (!result) 523 { 524 //ShowMessageBox("WTSQueryUserToken failed", "AlertService Message"); 525 (new Log4Net()).Log4NetLogger("WTSQueryUserToken failed"); 526 } 527 528 result = DuplicateTokenEx( 529 hToken, 530 GENERIC_ALL_ACCESS, 531 ref sa, 532 (int)SECURITY_IMPERSONATION_LEVEL.SecurityIdentification, 533 (int)TOKEN_TYPE.TokenPrimary, 534 ref hDupedToken 535 ); 536 537 if (!result) 538 { 539 //ShowMessageBox("DuplicateTokenEx failed", "AlertService Message"); 540 (new Log4Net()).Log4NetLogger("DuplicateTokenEx failed"); 541 } 542 543 IntPtr lpEnvironment = IntPtr.Zero; 544 result = CreateEnvironmentBlock(out lpEnvironment, hDupedToken, false); 545 546 if (!result) 547 { 548 //ShowMessageBox("CreateEnvironmentBlock failed", "AlertService Message"); 549 (new Log4Net()).Log4NetLogger("CreateEnvironmentBlock failed"); 550 } 551 552 result = CreateProcessAsUser( 553 hDupedToken, 554 null, 555 string.Format("\"{0}\" {1}", app, argument), 556 ref sa, ref sa, 557 false, 0, IntPtr.Zero, 558 null, ref si, ref pi); 559 560 561 562 563 if (!result) 564 { 565 int error = Marshal.GetLastWin32Error(); 566 string message = String.Format("CreateProcessAsUser Error: {0}", error); 567 //ShowMessageBox(message, "AlertService Message"); 568 (new Log4Net()).Log4NetLogger(message); 569 } 570 } 571 catch (Exception ex) 572 { 573 log.WriteException(ex, "CreateProcess"); 574 } 575 finally 576 { 577 if (pi.hProcess != IntPtr.Zero) 578 CloseHandle(pi.hProcess); 579 if (pi.hThread != IntPtr.Zero) 580 CloseHandle(pi.hThread); 581 if (hDupedToken != IntPtr.Zero) 582 CloseHandle(hDupedToken); 583 } 584 } 585 586 [StructLayout(LayoutKind.Sequential)] 587 public struct STARTUPINFO 588 { 589 public Int32 cb; 590 public string lpReserved; 591 public string lpDesktop; 592 public string lpTitle; 593 public Int32 dwX; 594 public Int32 dwY; 595 public Int32 dwXSize; 596 public Int32 dwXCountChars; 597 public Int32 dwYCountChars; 598 public Int32 dwFillAttribute; 599 public Int32 dwFlags; 600 public Int16 wShowWindow; 601 public Int16 cbReserved2; 602 public IntPtr lpReserved2; 603 public IntPtr hStdInput; 604 public IntPtr hStdOutput; 605 public IntPtr hStdError; 606 } 607 608 [StructLayout(LayoutKind.Sequential)] 609 public struct PROCESS_INFORMATION 610 { 611 public IntPtr hProcess; 612 public IntPtr hThread; 613 public Int32 dwProcessID; 614 public Int32 dwThreadID; 615 } 616 617 [StructLayout(LayoutKind.Sequential)] 618 public struct SECURITY_ATTRIBUTES 619 { 620 public Int32 Length; 621 public IntPtr lpSecurityDescriptor; 622 public bool bInheritHandle; 623 } 624 625 public enum SECURITY_IMPERSONATION_LEVEL 626 { 627 SecurityAnonymous, 628 SecurityIdentification, 629 SecurityImpersonation, 630 SecurityDelegation 631 } 632 633 public enum TOKEN_TYPE 634 { 635 TokenPrimary = 1, 636 TokenImpersonation 637 } 638 639 public const int GENERIC_ALL_ACCESS = 0x10000000; 640 641 [DllImport("kernel32.dll", EntryPoint = "CloseHandle", SetLastError = true, 642 CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 643 public static extern bool CloseHandle(IntPtr handle); 644 645 [DllImport("advapi32.dll", SetLastError = true, 646 CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] 647 public static extern bool CreateProcessAsUser( 648 IntPtr hToken, 649 string lpApplicationName, 650 string lpCommandLine, 651 ref SECURITY_ATTRIBUTES lpProcessAttributes, 652 ref SECURITY_ATTRIBUTES lpThreadAttributes, 653 bool bInheritHandle, 654 Int32 dwCreationFlags, 655 IntPtr lpEnvrionment, 656 string lpCurrentDirectory, 657 ref STARTUPINFO lpStartupInfo, 658 ref PROCESS_INFORMATION lpProcessInformation); 659 660 [DllImport("advapi32.dll", EntryPoint = "DuplicateTokenEx", SetLastError = true)] 661 public static extern bool DuplicateTokenEx( 662 IntPtr hExistingToken, 663 Int32 dwDesiredAccess, 664 ref SECURITY_ATTRIBUTES lpThreadAttributes, 665 Int32 ImpersonationLevel, 666 Int32 dwTokenType, 667 ref IntPtr phNewToken); 668 669 [DllImport("wtsapi32.dll", SetLastError = true)] 670 public static extern bool WTSQueryUserToken( 671 Int32 sessionId, 672 out IntPtr Token); 673 674 [DllImport("userenv.dll", SetLastError = true)] 675 static extern bool CreateEnvironmentBlock( 676 out IntPtr lpEnvironment, 677 IntPtr hToken, 678 bool bInherit); 679 680 #endregion 681 }
1 /// <summary> 2 /// 确认只有一个程序实例能够运行 3 /// /// </summary> 4 public abstract class OneInstance 5 { 6 /// <summary> 7 /// 用来判断一个指定的程序是否正在运行 8 /// </summary> 9 /// <param name="appId">程序名称,长一点比较好,防止有重复</param> 10 /// <returns>如果程序是第一次运行返回True,否则返回False</returns> 11 public static bool IsFirst(string appId) 12 { 13 bool ret = false; 14 if (OpenMutex(0x1F0001, 0, appId) == IntPtr.Zero) 15 { 16 CreateMutex(IntPtr.Zero, 0, appId); 17 ret = true; 18 } 19 return ret; 20 } 21 22 /// <summary> 23 /// 尝试调用指定的进程,返回句柄 24 /// </summary> 25 /// <param name="dwDesiredAccess">access</param> 26 /// <param name="bInheritHandle">inheritance option</param> 27 /// <param name = "lpName" > object name</param> 28 ///<returns>相应的句柄</returns> 29 [DllImport("Kernel32.dll", CharSet = CharSet.Auto)] 30 private static extern IntPtr OpenMutex(uint dwDesiredAccess, int bInheritHandle, string lpName); 31 /// <summary> 32 /// 创建进程,返回相应的句柄 33 /// </summary> 34 /// <param name="lpMutexAttributes">SD</param> 35 /// <param name="bInitialOwner">initial owner</param> 36 /// <param name="lpName">object name</param> 37 /// <returns>相应的句柄</returns> 38 [DllImport("Kernel32.dll", CharSet = CharSet.Auto)] 39 private static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, 40 int bInitialOwner, string lpName); 41 [DllImport("user32.dll")] 42 public static extern int GetDesktopWindow(); 43 [DllImport("user32.dll")] 44 public static extern IntPtr GetProcessWindowStation(); 45 [DllImport("kernel32.dll")] 46 public static extern IntPtr GetCurrentThreadId(); 47 [DllImport("user32.dll")] 48 public static extern IntPtr GetThreadDesktop(IntPtr dwThread); 49 [DllImport("user32.dll")] 50 public static extern IntPtr OpenWindowStation(string a, bool b, int c); 51 [DllImport("user32.dll")] 52 public static extern IntPtr OpenDesktop(string lpszDesktop, uint dwFlags, bool fInherit, uint dwDesiredAccess); 53 [DllImport("user32.dll")] 54 public static extern IntPtr CloseDesktop(IntPtr p); 55 [DllImport("rpcrt4.dll", SetLastError = true)] 56 public static extern IntPtr RpcImpersonateClient(int i); 57 [DllImport("rpcrt4.dll", SetLastError = true)] 58 public static extern IntPtr RpcRevertToSelf(); 59 [DllImport("user32.dll")] 60 public static extern IntPtr SetThreadDesktop(IntPtr a); 61 [DllImport("user32.dll")] 62 public static extern IntPtr SetProcessWindowStation(IntPtr a); 63 [DllImport("user32.dll")] 64 public static extern IntPtr CloseWindowStation(IntPtr a); 65 }
源代码下载地址:源代码

浙公网安备 33010602011771号