using System;
using System
namespace FileWatchService
{
public class modAPI
{
[DllImport(
public static extern int LockServiceDatabase(int hSCManager);
[DllImport(
public static extern bool UnlockServiceDatabase(int hSCManager);
[DllImport(
public static extern void CopyMemory(IntPtr pDst
[DllImport(
public static extern bool ChangeServiceConfigA(
int hService
int dwErrorControl
int lpdwTagId
string lpPassword
[DllImport(
public static extern bool ChangeServiceConfig
int hService
[MarshalAs(UnmanagedType
[DllImport(
public static extern bool ChangeServiceConfig
int hService
[MarshalAs(UnmanagedType
[DllImport(
public static extern int OpenServiceA(
int hSCManager
[DllImport(
public static extern int OpenSCManagerA(
string lpMachineName
[DllImport(
public static extern bool CloseServiceHandle(
int hSCObject);
[DllImport(
public static extern bool QueryServiceConfigA(
int hService
ref QUERY_SERVICE_CONFIG lpServiceConfig
int pcbBytesNeeded);
[DllImport(
public static extern int StartService(int SVHANDLE
public const int STANDARD_RIGHTS_REQUIRED =
public const int GENERIC_READ =
public const int ERROR_INSUFFICIENT_BUFFER =
public const int SERVICE_NO_CHANGE =
//public const int SERVICE_NO_CHANGE =
public enum ServiceType
{
SERVICE_KERNEL_DRIVER =
SERVICE_FILE_SYSTEM_DRIVER =
SERVICE_WIN
SERVICE_WIN
SERVICE_INTERACTIVE_PROCESS =
SERVICETYPE_NO_CHANGE = SERVICE_NO_CHANGE
}
public enum ServiceStartType:int
{
SERVICE_BOOT_START =
SERVICE_SYSTEM_START =
SERVICE_AUTO_START =
SERVICE_DEMAND_START =
SERVICE_DISABLED =
SERVICESTARTTYPE_NO_CHANGE = SERVICE_NO_CHANGE
}
public enum ServiceErrorControl:int
{
SERVICE_ERROR_IGNORE =
SERVICE_ERROR_NORMAL =
SERVICE_ERROR_SEVERE =
SERVICE_ERROR_CRITICAL =
msidbServiceInstallErrorControlVital =
SERVICEERRORCONTROL_NO_CHANGE = SERVICE_NO_CHANGE
}
public enum ServiceStateRequest:int
{
SERVICE_ACTIVE =
SERVICE_INACTIVE =
SERVICE_STATE_ALL = (SERVICE_ACTIVE + SERVICE_INACTIVE)
}
public enum ServiceControlType:int
{
SERVICE_CONTROL_STOP =
SERVICE_CONTROL_PAUSE =
SERVICE_CONTROL_CONTINUE =
SERVICE_CONTROL_INTERROGATE =
SERVICE_CONTROL_SHUTDOWN =
SERVICE_CONTROL_PARAMCHANGE =
SERVICE_CONTROL_NETBINDADD =
SERVICE_CONTROL_NETBINDREMOVE =
SERVICE_CONTROL_NETBINDENABLE =
SERVICE_CONTROL_NETBINDDISABLE =
SERVICE_CONTROL_DEVICEEVENT =
SERVICE_CONTROL_HARDWAREPROFILECHANGE =
SERVICE_CONTROL_POWEREVENT =
SERVICE_CONTROL_SESSIONCHANGE =
}
public enum ServiceState:int
{
SERVICE_STOPPED =
SERVICE_START_PENDING =
SERVICE_STOP_PENDING =
SERVICE_RUNNING =
SERVICE_CONTINUE_PENDING =
SERVICE_PAUSE_PENDING =
SERVICE_PAUSED =
}
public enum ServiceControlAccepted:int
{
SERVICE_ACCEPT_STOP =
SERVICE_ACCEPT_PAUSE_CONTINUE =
SERVICE_ACCEPT_SHUTDOWN =
SERVICE_ACCEPT_PARAMCHANGE =
SERVICE_ACCEPT_NETBINDCHANGE =
SERVICE_ACCEPT_HARDWAREPROFILECHANGE =
SERVICE_ACCEPT_POWEREVENT =
SERVICE_ACCEPT_SESSIONCHANGE =
}
public enum ServiceControlManagerType:int
{
SC_MANAGER_CONNECT =
SC_MANAGER_CREATE_SERVICE =
SC_MANAGER_ENUMERATE_SERVICE =
SC_MANAGER_LOCK =
SC_MANAGER_QUERY_LOCK_STATUS =
SC_MANAGER_MODIFY_BOOT_CONFIG =
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
}
public enum ACCESS_TYPE:int
{
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 =
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
}
[StructLayout(LayoutKind
public struct SERVICE_STATUS
{
public int dwServiceType;
public int dwCurrentState;
public int dwControlsAccepted;
public int dwWin
public int dwServiceSpecificExitCode;
public int dwCheckPoint;
public int dwWaitHint;
}
[StructLayout(LayoutKind
public struct QUERY_SERVICE_CONFIG
{
public int dwServiceType;
public int dwStartType;
public int dwErrorControl;
public string lpBinaryPathName;
public string lpLoadOrderGroup;
public int dwTagId;
public string lpDependencies;
public string lpServiceStartName;
public string lpDisplayName;
}
public enum SC_ACTION_TYPE:int
{
SC_ACTION_NONE =
SC_ACTION_RESTART =
SC_ACTION_REBOOT =
SC_ACTION_RUN_COMMAND =
}
[StructLayout(LayoutKind
public struct SC_ACTION
{
public SC_ACTION_TYPE SCActionType;
public int Delay;
}
public enum InfoLevel:int
{
SERVICE_CONFIG_DESCRIPTION =
SERVICE_CONFIG_FAILURE_ACTIONS =
}
[StructLayout(LayoutKind
public struct SERVICE_DESCRIPTION
{
public string lpDescription;
}
[StructLayout(LayoutKind
public struct SERVICE_FAILURE_ACTIONS
{
public int dwResetPeriod;
public string lpRebootMsg;
public string lpCommand;
public int cActions;
public int lpsaActions;
}
}
}
當我們給服務增加安裝包時
private void InitializeComponent()
{
//這裡要增加代碼
this
}
private void ProjectInstaller_AfterInstall(object sender
{
int iSCManagerHandle =
int iSCManagerLockHandle =
int iServiceHandle =
bool bChangeServiceConfig = false;
bool bChangeServiceConfig
modAPI
modAPI
modAPI
bool bCloseService = false;
bool bUnlockSCManager = false;
bool bCloseSCManager = false;
IntPtr iScActionsPointer = new IntPtr();
try
{
//打開服務控制台
iSCManagerHandle = modAPI
modAPI
if (iSCManagerHandle <
{
throw new Exception(
}
iSCManagerLockHandle = modAPI
if (iSCManagerLockHandle <
{
throw new Exception(
}
//服務名
iServiceHandle = modAPI
modAPI
if (iServiceHandle <
{
throw new Exception(
}
bChangeServiceConfig = modAPI
modAPI
modAPI
if (bChangeServiceConfig==false)
{
throw new Exception(
}
ServiceDescription
bChangeServiceConfig
modAPI
if (bChangeServiceConfig
{
throw new Exception(
}
ServiceFailureActions
ServiceFailureActions
// ServiceFailureActions
ServiceFailureActions
ServiceFailureActions
//故障恢復設置
ScActions[
ScActions[
ScActions[
ScActions[
ScActions[
ScActions[
iScActionsPointer = Marshal
modAPI
ServiceFailureActions
bChangeServiceConfig
modAPI
if (bChangeServiceConfig
{
throw new Exception(
}
}
catch(Exception ex)
{
throw new Exception(ex
}
finally
{
Marshal
if (iServiceHandle >
{
bCloseService = modAPI
}
if (iSCManagerLockHandle >
{
bUnlockSCManager = modAPI
}
if (iSCManagerHandle !=
{
bCloseSCManager = modAPI
}
}
}
在安裝完成後
如果你在安裝時
this
this
這二個事件
committed事件
beforeuninstall事件
From:http://tw.wingwit.com/Article/program/net/201311/13252.html