在window service中调用外部exe或.bat等

本文介绍了一种使用C#在Windows环境中创建应用启动器的方法,该启动器能够以管理员权限运行指定应用程序,并且能够绕过Vista系统的用户账户控制(UAC)提示,确保了程序的执行无需用户交互即可获得所需权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在您的服务程序中直接调用:UserProcess.StartProcessAndBypassUAC("your appPath","parameters",processInfo)


[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6.   
  7. using System.Security;  
  8. using System.Diagnostics;  
  9. using System.Runtime.InteropServices;  
  10.   
  11.   
  12. namespace Test  
  13. {  
  14.     class UserProcess  
  15.     {  
  16.         #region Structures  
  17.   
  18.   
  19.         [StructLayout(LayoutKind.Sequential)]  
  20.         public struct SECURITY_ATTRIBUTES  
  21.         {  
  22.             public int Length;  
  23.             public IntPtr lpSecurityDescriptor;  
  24.             public bool bInheritHandle;  
  25.         }  
  26.   
  27.   
  28.         [StructLayout(LayoutKind.Sequential)]  
  29.         public struct STARTUPINFO  
  30.         {  
  31.             public int cb;  
  32.             public String lpReserved;  
  33.             public String lpDesktop;  
  34.             public String lpTitle;  
  35.             public uint dwX;  
  36.             public uint dwY;  
  37.             public uint dwXSize;  
  38.             public uint dwYSize;  
  39.             public uint dwXCountChars;  
  40.             public uint dwYCountChars;  
  41.             public uint dwFillAttribute;  
  42.             public uint dwFlags;  
  43.             public short wShowWindow;  
  44.             public short cbReserved2;  
  45.             public IntPtr lpReserved2;  
  46.             public IntPtr hStdInput;  
  47.             public IntPtr hStdOutput;  
  48.             public IntPtr hStdError;  
  49.         }  
  50.   
  51.   
  52.         [StructLayout(LayoutKind.Sequential)]  
  53.         public struct PROCESS_INFORMATION  
  54.         {  
  55.             public IntPtr hProcess;  
  56.             public IntPtr hThread;  
  57.             public uint dwProcessId;  
  58.             public uint dwThreadId;  
  59.         }  
  60.  
  61.  
  62.         #endregion  
  63.  
  64.  
  65.         #region Enumerations  
  66.   
  67.   
  68.         enum TOKEN_TYPE : int  
  69.         {  
  70.             TokenPrimary = 1,  
  71.             TokenImpersonation = 2  
  72.         }  
  73.   
  74.   
  75.         enum SECURITY_IMPERSONATION_LEVEL : int  
  76.         {  
  77.             SecurityAnonymous = 0,  
  78.             SecurityIdentification = 1,  
  79.             SecurityImpersonation = 2,  
  80.             SecurityDelegation = 3,  
  81.         }  
  82.   
  83.   
  84.         enum WTSInfoClass  
  85.         {  
  86.             InitialProgram,  
  87.             ApplicationName,  
  88.             WorkingDirectory,  
  89.             OEMId,  
  90.             SessionId,  
  91.             UserName,  
  92.             WinStationName,  
  93.             DomainName,  
  94.             ConnectState,  
  95.             ClientBuildNumber,  
  96.             ClientName,  
  97.             ClientDirectory,  
  98.             ClientProductId,  
  99.             ClientHardwareId,  
  100.             ClientAddress,  
  101.             ClientDisplay,  
  102.             ClientProtocolType  
  103.         }  
  104.  
  105.  
  106.         #endregion  
  107.  
  108.  
  109.         #region Constants  
  110.   
  111.   
  112.         public const int TOKEN_DUPLICATE = 0x0002;  
  113.         public const uint MAXIMUM_ALLOWED = 0x2000000;  
  114.         public const int CREATE_NEW_CONSOLE = 0x00000010;  
  115.   
  116.   
  117.         public const int IDLE_PRIORITY_CLASS = 0x40;  
  118.         public const int NORMAL_PRIORITY_CLASS = 0x20;  
  119.         public const int HIGH_PRIORITY_CLASS = 0x80;  
  120.         public const int REALTIME_PRIORITY_CLASS = 0x100;  
  121.  
  122.  
  123.         #endregion  
  124.  
  125.  
  126.         #region Win32 API Imports  
  127.   
  128.   
  129.         [DllImport("kernel32.dll", SetLastError = true)]  
  130.         private static extern bool CloseHandle(IntPtr hSnapshot);  
  131.   
  132.   
  133.         [DllImport("kernel32.dll")]  
  134.         static extern uint WTSGetActiveConsoleSessionId();  
  135.   
  136.   
  137.         [DllImport("wtsapi32.dll", CharSet = CharSet.Unicode, SetLastError = true), SuppressUnmanagedCodeSecurityAttribute]  
  138.         static extern bool WTSQuerySessionInformation(System.IntPtr hServer, int sessionId, WTSInfoClass wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned);  
  139.   
  140.   
  141.         [DllImport("advapi32.dll", EntryPoint = "CreateProcessAsUser", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]  
  142.         public extern static bool CreateProcessAsUser(IntPtr hToken, String lpApplicationName, String lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,  
  143.             ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandle, int dwCreationFlags, IntPtr lpEnvironment,  
  144.             String lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);  
  145.   
  146.   
  147.         [DllImport("kernel32.dll")]  
  148.         static extern bool ProcessIdToSessionId(uint dwProcessId, ref uint pSessionId);  
  149.   
  150.   
  151.         [DllImport("advapi32.dll", EntryPoint = "DuplicateTokenEx")]  
  152.         public extern static bool DuplicateTokenEx(IntPtr ExistingTokenHandle, uint dwDesiredAccess,  
  153.             ref SECURITY_ATTRIBUTES lpThreadAttributes, int TokenType,  
  154.             int ImpersonationLevel, ref IntPtr DuplicateTokenHandle);  
  155.   
  156.   
  157.         [DllImport("kernel32.dll")]  
  158.         static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);  
  159.   
  160.   
  161.         [DllImport("advapi32", SetLastError = true), SuppressUnmanagedCodeSecurityAttribute]  
  162.         static extern bool OpenProcessToken(IntPtr ProcessHandle, int DesiredAccess, ref IntPtr TokenHandle);  
  163.  
  164.  
  165.         #endregion  
  166.   
  167.   
  168.         public static string GetCurrentActiveUser()  
  169.         {  
  170.             IntPtr hServer = IntPtr.Zero, state = IntPtr.Zero;  
  171.             uint bCount = 0;  
  172.   
  173.   
  174.             // obtain the currently active session id; every logged on user in the system has a unique session id  
  175.             uint dwSessionId = WTSGetActiveConsoleSessionId();  
  176.             string domain = string.Empty, userName = string.Empty;  
  177.   
  178.   
  179.             if (WTSQuerySessionInformation(hServer, (int)dwSessionId, WTSInfoClass.DomainName, out state, out bCount))  
  180.             {  
  181.                 domain = Marshal.PtrToStringAuto(state);  
  182.             }  
  183.   
  184.   
  185.             if (WTSQuerySessionInformation(hServer, (int)dwSessionId, WTSInfoClass.UserName, out state, out bCount))  
  186.             {  
  187.                 userName = Marshal.PtrToStringAuto(state);  
  188.             }  
  189.   
  190.   
  191.             return string.Format("{0}\\{1}", domain, userName);  
  192.         }  
  193.   
  194.   
  195.         /// <summary>  
  196.         /// Launches the given application with full admin rights, and in addition bypasses the Vista UAC prompt  
  197.         /// </summary>  
  198.         /// <param name="applicationName">The name of the application to launch</param>  
  199.         /// <param name="procInfo">Process information regarding the launched application that gets returned to the caller</param>  
  200.         /// <returns></returns>  
  201.         public static bool StartProcessAndBypassUAC(String applicationName, String command, out PROCESS_INFORMATION procInfo)  
  202.         {  
  203.             uint winlogonPid = 0;  
  204.             IntPtr hUserTokenDup = IntPtr.Zero, hPToken = IntPtr.Zero, hProcess = IntPtr.Zero;  
  205.             procInfo = new PROCESS_INFORMATION();  
  206.   
  207.   
  208.             // obtain the currently active session id; every logged on user in the system has a unique session id  
  209.             uint dwSessionId = WTSGetActiveConsoleSessionId();  
  210.   
  211.   
  212.             // obtain the process id of the winlogon process that is running within the currently active session  
  213.             Process[] processes = Process.GetProcessesByName("winlogon");  
  214.             foreach (Process p in processes)  
  215.             {  
  216.                 if ((uint)p.SessionId == dwSessionId)  
  217.                 {  
  218.                     winlogonPid = (uint)p.Id;  
  219.                 }  
  220.             }  
  221.   
  222.   
  223.             // obtain a handle to the winlogon process  
  224.             hProcess = OpenProcess(MAXIMUM_ALLOWED, false, winlogonPid);  
  225.   
  226.   
  227.             // obtain a handle to the access token of the winlogon process  
  228.             if (!OpenProcessToken(hProcess, TOKEN_DUPLICATE, ref hPToken))  
  229.             {  
  230.                 CloseHandle(hProcess);  
  231.                 return false;  
  232.             }  
  233.   
  234.   
  235.             // Security attibute structure used in DuplicateTokenEx and CreateProcessAsUser  
  236.             // I would prefer to not have to use a security attribute variable and to just   
  237.             // simply pass null and inherit (by default) the security attributes  
  238.             // of the existing token. However, in C# structures are value types and therefore  
  239.             // cannot be assigned the null value.  
  240.             SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();  
  241.             sa.Length = Marshal.SizeOf(sa);  
  242.   
  243.   
  244.             // copy the access token of the winlogon process; the newly created token will be a primary token  
  245.             if (!DuplicateTokenEx(hPToken, MAXIMUM_ALLOWED, ref sa, (int)SECURITY_IMPERSONATION_LEVEL.SecurityIdentification, (int)TOKEN_TYPE.TokenPrimary, ref hUserTokenDup))  
  246.             {  
  247.                 CloseHandle(hProcess);  
  248.                 CloseHandle(hPToken);  
  249.                 return false;  
  250.             }  
  251.   
  252.   
  253.             // By default CreateProcessAsUser creates a process on a non-interactive window station, meaning  
  254.             // the window station has a desktop that is invisible and the process is incapable of receiving  
  255.             // user input. To remedy this we set the lpDesktop parameter to indicate we want to enable user   
  256.             // interaction with the new process.  
  257.             STARTUPINFO si = new STARTUPINFO();  
  258.             si.cb = (int)Marshal.SizeOf(si);  
  259.             si.lpDesktop = @"winsta0\default"// interactive window station parameter; basically this indicates that the process created can display a GUI on the desktop  
  260.   
  261.   
  262.             // flags that specify the priority and creation method of the process  
  263.             int dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE;  
  264.   
  265.   
  266.             // create a new process in the current user's logon session  
  267.             bool result = CreateProcessAsUser(hUserTokenDup,        // client's access token  
  268.                                             applicationName,        // file to execute  
  269.                                             command,                // command line  
  270.                                             ref sa,                 // pointer to process SECURITY_ATTRIBUTES  
  271.                                             ref sa,                 // pointer to thread SECURITY_ATTRIBUTES  
  272.                                             false,                  // handles are not inheritable  
  273.                                             dwCreationFlags,        // creation flags  
  274.                                             IntPtr.Zero,            // pointer to new environment block   
  275.                                             null,                   // name of current directory   
  276.                                             ref si,                 // pointer to STARTUPINFO structure  
  277.                                             out procInfo            // receives information about new process  
  278.                                             );  
  279.   
  280.   
  281.             // invalidate the handles  
  282.             CloseHandle(hProcess);  
  283.             CloseHandle(hPToken);  
  284.             CloseHandle(hUserTokenDup);  
  285.   
  286.   
  287.             return result; // return the result  
  288.         }  
  289.     }  
  290. }  
<think>首先,用户的问题是:“请问如何在新应用程序中运行打开.bat文件 Windows 解决方案”。这似乎是在询问如何在Windows系统中运行打开一个.bat文件,特别是涉及到新应用程序。 我需要参考系统级指令: - 所有行内数学表达式用$...$格式。 - 独立公式用$$...$$格式。 - 使用中文回答。 - 生成相关问题。 - 回答中引用的段落末尾添加引用标识。 - 回答结构清晰,帮助用户逐步解决问题。 - 参考提供的引用内容。 提供的引用: - [^1]: 关于创建批处理文件来控制应用程序启动和窗口位置。 - [^2]: 关于.hlp文件和winhlp32.exe的问题,但似乎不直接相关。 - [^3]: 关于IDEA无法打开,通过重新下载Visual C++解决。 用户的需求是“在新应用程序中运行打开.bat文件”。这可能意味着用户想在另一个应用程序中运行.bat文件者更一般地,如何在Windows中运行.bat文件。引用[^1]提到了批处理文件,所以我可以参考那个。 关键点:用户说“在新应用程序中”,这有点模糊。可能用户想从一个新应用程序中执行.bat文件.bat文件本身启动新应用程序。从引用[^1]看,它展示了如何在批处理文件启动应用程序。 所以,我应该解释如何运行.bat文件,以及如何将.bat文件集成到其他应用程序中。 步骤: 1. **理解.bat文件**:批处理文件是包含一系列命令的文本文件,扩展名为.bat,在Windows命令提示符中运行。 2. **运行.bat文件**: - 双击.bat文件文件资源管理器中运行。 - 使用命令提示符PowerShell运行。 3. **在应用程序中运行.bat文件**:用户可能想从另一个程序(如Python脚本、Java应用等)执行.bat文件。这可以通过编程方式调用命令提示符来实现。 4. **解决方案**:基于引用[^1],我可以展示如何创建和使用批处理文件来控制应用程序启动。 结构化回答: - 介绍.bat文件。 - 解释如何运行.bat文件。 - 提供在应用程序中运行.bat文件的示例。 - 生成相关问题。 使用中文回答。 添加引用:在引用的段落末尾添加[^1]等。 生成相关问题。 参考引用[^1]的内容:它展示了批处理文件示例。 所以,我的回答应该包括: - 什么是.bat文件。 - 如何运行.bat文件。 - 如何在其他应用程序中运行.bat文件(例如,从Python调用)。 - 示例代码,如引用[^1]中的批处理文件。 - 生成相关问题。 确保回答真实可靠。</think>### 如何在新应用程序中运行打开.bat文件Windows解决方案) 在Windows系统中,.bat文件(批处理文件)是一种包含一系列命令的脚本文件,通常用于自动化任务启动应用程序。您的问题可能涉及两种场景:(1) 直接运行.bat文件启动新应用程序;(2) 从另一个应用程序(如Python、JavaC#程序)中调用.bat文件。下面我将分步骤解释解决方案,确保内容真实可靠,并基于最佳实践。 #### 步骤1: 理解.bat文件的基本运行方式 .bat文件Windows中可以直接执行,它会启动命令提示符(cmd.exe)并运行文件中的命令。如果您只想运行.bat文件启动一个新应用程序(例如,启动一个游戏工具),操作很简单: - **双击运行**:在文件资源管理器中找到.bat文件,双击即可执行。如果文件包含启动应用程序的命令(如`start "C:\Path\To\App.exe"`),它会自动打开新应用程序。 - **使用命令提示符**:打开命令提示符(按Win+R,输入`cmd`),然后导航到.bat文件所在目录,输入文件名(例如`mybatch.bat`)并按Enter。 如果.bat文件需要参数来控制应用程序行为(如窗口位置),可以参考引用[^1]中的示例,创建一个批处理文件来指定参数。例如: ```batch @echo off start "" "C:\Program Files\MyApp\app.exe" --window-position=1920,0 ``` 这段代码会启动`app.exe`,并将窗口定位在屏幕坐标(1920,0)处[^1]。 #### 步骤2: 在新应用程序中运行.bat文件(编程方式) 如果您想在另一个应用程序(如Python脚本、Java程序C#应用)中执行.bat文件,这需要调用Windows的系统命令。以下是常见编程语言的实现方法,确保使用完整路径以避免权限问题。 - **Python示例**:使用`subprocess`模块调用.bat文件。这适用于自动化任务集成到Python应用中。 ```python import subprocess # 指定.bat文件的完整路径 bat_path = r"C:\Path\To\Your\batchfile.bat" # 运行.bat文件,并等待完成(使用shell=True以处理路径空格) result = subprocess.run(bat_path, shell=True, capture_output=True, text=True) print("输出:", result.stdout) # 打印命令输出 ``` 这会在Python环境中启动.bat文件,并执行其命令,包括启动新应用程序[^1]。 - **Java示例**:使用`Runtime``ProcessBuilder`类执行.bat文件。 ```java import java.io.IOException; public class RunBat { public static void main(String[] args) { try { // 指定.bat文件路径 String batPath = "C:\\Path\\To\\Your\\batchfile.bat"; // 创建进程并执行 Process process = Runtime.getRuntime().exec(batPath); process.waitFor(); // 等待执行完成 System.out.println("批处理文件执行完毕"); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } ``` 这会在Java应用中调用.bat文件,适合桌面应用后台服务。 - **C#示例**:在.NET应用中,使用`Process.Start`方法。 ```csharp using System; using System.Diagnostics; class Program { static void Main() { // 指定.bat文件路径 string batPath = @"C:\Path\To\Your\batchfile.bat"; // 启动进程 Process.Start(new ProcessStartInfo { FileName = batPath, UseShellExecute = true // 使用系统shell执行 }); Console.WriteLine("批处理文件启动"); } } ``` 这适用于Windows窗体控制台应用,能无缝集成.bat文件[^1]。 #### 注意事项和常见问题 - **权限问题**:如果.bat文件无法运行,确保以管理员身份运行命令提示符应用程序(右键单击 > “以管理员身份运行”)。 - **路径处理**:路径中包含空格时,使用双引号包裹(如`"C:\Program Files\App\app.exe"`),避免错误。 - **错误排查**:如果.bat文件启动失败,检查命令语法依赖项(如某些应用程序需要Visual C++库,类似引用[^3]中提到的问题)。 - **安全提示**:.bat文件可能包含恶意命令,只运行来源可信的文件。在开发中,建议测试在沙箱环境中。 通过以上方法,您可以轻松在Windows中运行.bat文件,并集成到各种应用程序中。如果您遇到具体错误(如文件路径无效权限不足),请提供更多细节以便进一步帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值