/* from: http://xiaoou2002.blog.163.com/blog/static/21586669201081634323740/ */
注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;
}
}
}
}