使用advapi32.dll来启动、停止服务(c#)
2011-10-28 11:26
671 查看
/* from: http://xiaoou2002.blog.163.com/blog/static/21586669201081634323740/ */
注1:代码中的IntPtr可以改为int
注2:ServiceName需要取service name,而不是display name
注1:代码中的IntPtr可以改为int
注2:ServiceName需要取service name,而不是display name
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace WindowsService2 { public class InstallUtil { #region DLLImport [DllImport("advapi32.dll")] public static extern IntPtr OpenSCManager(string lpMachineName, string lpSCDB, int scParameter); [DllImport("Advapi32.dll")] public static extern IntPtr CreateService(IntPtr SC_HANDLE, string lpSvcName, string lpDisplayName, int dwDesiredAccess, int dwServiceType, int dwStartType, int dwErrorControl, string lpPathName, string lpLoadOrderGroup, int lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword); [DllImport("advapi32.dll")] public static extern void CloseServiceHandle(IntPtr SCHANDLE); [DllImport("advapi32.dll")] public static extern int StartService(IntPtr SVHANDLE, int dwNumServiceArgs, string lpServiceArgVectors); [DllImport("advapi32.dll", SetLastError = true)] public static extern IntPtr OpenService(IntPtr SCHANDLE, string lpSvcName, int dwNumServiceArgs); [DllImport("advapi32.dll")] public static extern int DeleteService(IntPtr SVHANDLE); [DllImport("advapi32.dll")] public static extern int ControlService(IntPtr SCHANDLE, int dwControl, ref SERVICE_STATUS lpStatus); [DllImport("kernel32.dll")] public static extern int GetLastError(); #endregion DLLImport #region 常量定义 [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct SERVICE_STATUS { public int dwServiceType; public int dwCurrentState; public int dwControlsAccepted; public int dwWin32ExitCode; public int dwServiceSpecificExitCode; public int dwCheckPoint; public int dwWaitHint; } static int STANDARD_RIGHTS_REQUIRED = 0xF0000; static int SC_MANAGER_CONNECT = 0x0001; static int SC_MANAGER_CREATE_SERVICE = 0x0002; static int SC_MANAGER_ENUMERATE_SERVICE = 0x0004; static int SC_MANAGER_LOCK = 0x0008; static int SC_MANAGER_QUERY_LOCK_STATUS = 0x0010; static int SC_MANAGER_MODIFY_BOOT_CONFIG = 0x0020; static int SC_MANAGER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE | SC_MANAGER_ENUMERATE_SERVICE | SC_MANAGER_LOCK | SC_MANAGER_QUERY_LOCK_STATUS | SC_MANAGER_MODIFY_BOOT_CONFIG); static int SERVICE_WIN32_OWN_PROCESS = 0x00000010; //static int SERVICE_DEMAND_START = 0x00000003; static int SERVICE_ERROR_NORMAL = 0x00000001; static int SERVICE_QUERY_CONFIG = 0x0001; static int SERVICE_CHANGE_CONFIG = 0x0002; static int SERVICE_QUERY_STATUS = 0x0004; static int SERVICE_ENUMERATE_DEPENDENTS = 0x0008; static int SERVICE_START = 0x0010; static int SERVICE_STOP = 0x0020; static int SERVICE_PAUSE_CONTINUE = 0x0040; static int SERVICE_INTERROGATE = 0x0080; static int SERVICE_USER_DEFINED_CONTROL = 0x0100; static int SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG | SERVICE_QUERY_STATUS | SERVICE_ENUMERATE_DEPENDENTS | SERVICE_START | SERVICE_STOP | SERVICE_PAUSE_CONTINUE | SERVICE_INTERROGATE | SERVICE_USER_DEFINED_CONTROL); static int SERVICE_AUTO_START = 0x00000002; static int SERVICE_CONTROL_STOP = 0x00000001; #endregion 常量定义. /// <summary> /// 安装(并启动) /// </summary> /// <param name="svcPath">程序路径.</param> /// <param name="svcName">服务名</param> /// <param name="svcDispName">服务显示名称.</param> /// <returns>服务安装是否成功.</returns> static public bool InstallService(string svcPath, string svcName, string svcDispName) { try { UnInstallService(svcName); IntPtr sc_handle = OpenSCManager(null, null, SC_MANAGER_CREATE_SERVICE); if (sc_handle.ToInt32() != 0) { IntPtr sv_handle = CreateService(sc_handle, svcName, svcDispName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, svcPath, null, 0, null, null, null); if (sv_handle.ToInt32() == 0) { CloseServiceHandle(sc_handle); return false; } //else //{ // //试尝启动服务 // int i = StartService(sv_handle, 0, null); // if (i == 0) // { // CloseServiceHandle(sc_handle); // return false; // } //} return true; } else return false; } catch { return false; } } /// <summary> /// 服务是否已经安装 /// </summary> /// <param name="svcName"></param> /// <returns></returns> static public bool IsInstalled(string svcName) { bool bResult = false; IntPtr sc_handle = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS); if (sc_handle.ToInt32() != 0) { IntPtr sv_handle = OpenService(sc_handle, svcName, SERVICE_QUERY_CONFIG); if (sv_handle.ToInt32() != 0) { bResult = true; CloseServiceHandle(sv_handle); } CloseServiceHandle(sc_handle); } return bResult; } /// <summary> /// 反安装服务. /// </summary> /// <param name="svcName">服务名.</param> static public bool UnInstallService(string svcName) { if (!IsInstalled(svcName)) return true; try { IntPtr sc_hndl = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS); if (sc_hndl.ToInt32() == 0) return false; int DELETE = 0x10000; IntPtr sv_hndl = OpenService(sc_hndl, svcName, SERVICE_STOP | DELETE); if (sv_hndl.ToInt32() == 0) { CloseServiceHandle(sc_hndl); return false; } SERVICE_STATUS status = new SERVICE_STATUS(); ControlService(sv_hndl, SERVICE_CONTROL_STOP, ref status); int nRtn = DeleteService(sv_hndl); CloseServiceHandle(sv_hndl); CloseServiceHandle(sc_hndl); if (nRtn == 0) return false; return true; } catch { return false; } } /// <summary> /// 停止服务 /// </summary> /// <param name="svcName"></param> /// <returns></returns> static public bool StopService(string svcName) { if (!IsInstalled(svcName)) return true; try { IntPtr sc_hndl = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS); if (sc_hndl.ToInt32() == 0) return false; IntPtr sv_hndl = OpenService(sc_hndl, svcName, SERVICE_STOP); if (sv_hndl.ToInt32() == 0) { CloseServiceHandle(sc_hndl); return false; } SERVICE_STATUS status = new SERVICE_STATUS(); int nRtn = ControlService(sv_hndl, SERVICE_CONTROL_STOP, ref status); CloseServiceHandle(sv_hndl); CloseServiceHandle(sc_hndl); if (nRtn == 0) { int nErr = GetLastError(); return false; } return true; } catch { return false; } } } }
相关文章推荐
- 使用advapi32.dll来启动、停止服务(c#)
- C#对Windows服务操作(注册安装服务,卸载服务,启动停止服务,判断服务存在)
- C#Windows服务:一些方法(启动、停止等)
- 基于C#实现Windows服务状态启动和停止服务的方法
- 如何远程连接Linux主机并启动和停止服务-Xshell的基本使用
- C#操作注册服务卸载服务启动服务停止服务.. .
- 本地计算机上的SQLServer(MSSQLSERVER)服务启动后停止,某些服务在未由其他服务或程序使用时将自动停止
- X86_64平台下Oracle 11g R2 X64 for Linux的软件安装、数据库安装、服务配置、数据库启动和停止、SQLPLus使用
- 使用SC在C#中取得windows服务的启动类型
- OracleOraDb11g_home1TNSListener服务启动后停止,某些服务在未由其它服务或程序使用时将自动停止
- OracleOraDb11g_home1TNSListener服务启动后停止,某些服务在未由其它服务或程序使用时将自动停止
- 使用批处理设置、启动和停止服务
- C#Windows服务:一些方法(启动、停止等)
- 基于C#实现Windows服务状态启动和停止服务的方法
- 本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止?
- Redis教程3(Redis服务启动停止,命令客户端的使用)
- C#Windows服务:一些方法(启动、停止等)
- 使用C#代码安装、启动、停止、卸载Windows service(不使用InstallUtil.exe)
- 本地计算机的oracleoradb11g_home1tnslistener服务启动后停止,某些服务在未由其他服务或程序使用时将自动停止
- C#Windows服务:一些方法(启动、停止等)