Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #r "System.ServiceProcess.dll"
- #r "System.Configuration.Install.dll"
- open System
- open System.ServiceProcess
- open System.Threading
- open System.Runtime.InteropServices
- module advapi32 =
- module ACCESS_MASK =
- let DELETE = uint32 0x00010000
- let READ_CONTROL = uint32 0x00020000
- let WRITE_DAC = uint32 0x00040000
- let WRITE_OWNER = uint32 0x00080000
- let SYNCHRONIZE = uint32 0x00100000
- let STANDARD_RIGHTS_REQUIRED = uint32 0x000F0000
- let STANDARD_RIGHTS_READ = uint32 0x00020000
- let STANDARD_RIGHTS_WRITE = uint32 0x00020000
- let STANDARD_RIGHTS_EXECUTE = uint32 0x00020000
- let STANDARD_RIGHTS_ALL = uint32 0x001F0000
- let SPECIFIC_RIGHTS_ALL = uint32 0x0000FFFF
- let ACCESS_SYSTEM_SECURITY = uint32 0x01000000
- let MAXIMUM_ALLOWED = uint32 0x02000000
- let GENERIC_READ = uint32 0x80000000
- let GENERIC_WRITE = uint32 0x40000000
- let GENERIC_EXECUTE = uint32 0x20000000
- let GENERIC_ALL = uint32 0x10000000
- let DESKTOP_READOBJECTS = uint32 0x00000001
- let DESKTOP_CREATEWINDOW = uint32 0x00000002
- let DESKTOP_CREATEMENU = uint32 0x00000004
- let DESKTOP_HOOKCONTROL = uint32 0x00000008
- let DESKTOP_JOURNALRECORD = uint32 0x00000010
- let DESKTOP_JOURNALPLAYBACK = uint32 0x00000020
- let DESKTOP_ENUMERATE = uint32 0x00000040
- let DESKTOP_WRITEOBJECTS = uint32 0x00000080
- let DESKTOP_SWITCHDESKTOP = uint32 0x00000100
- let WINSTA_ENUMDESKTOPS = uint32 0x00000001
- let WINSTA_READATTRIBUTES = uint32 0x00000002
- let WINSTA_ACCESSCLIPBOARD = uint32 0x00000004
- let WINSTA_CREATEDESKTOP = uint32 0x00000008
- let WINSTA_WRITEATTRIBUTES = uint32 0x00000010
- let WINSTA_ACCESSGLOBALATOMS = uint32 0x00000020
- let WINSTA_EXITWINDOWS = uint32 0x00000040
- let WINSTA_ENUMERATE = uint32 0x00000100
- let WINSTA_READSCREEN = uint32 0x00000200
- let WINSTA_ALL_ACCESS = uint32 0x0000037F
- module SERVICE_ACCESS =
- /// <summary>
- /// Required to call the QueryServiceConfig and
- /// QueryServiceConfig2 functions to query the service configuration.
- /// </summary>
- let SERVICE_QUERY_CONFIG = uint32 0x00001
- /// <summary>
- /// Required to call the ChangeServiceConfig or ChangeServiceConfig2 function
- /// to change the service configuration. Because this grants the caller
- /// the right to change the executable file that the system runs,
- /// it should be granted only to administrators.
- /// </summary>
- let SERVICE_CHANGE_CONFIG = uint32 0x00002
- /// <summary>
- /// Required to call the QueryServiceStatusEx function to ask the service
- /// control manager about the status of the service.
- /// </summary>
- let SERVICE_QUERY_STATUS = uint32 0x00004
- /// <summary>
- /// Required to call the EnumDependentServices function to enumerate all
- /// the services dependent on the service.
- /// </summary>
- let SERVICE_ENUMERATE_DEPENDENTS = uint32 0x00008
- /// <summary>
- /// Required to call the StartService function to start the service.
- /// </summary>
- let SERVICE_START = uint32 0x00010
- /// <summary>
- /// Required to call the ControlService function to stop the service.
- /// </summary>
- let SERVICE_STOP = uint32 0x00020
- /// <summary>
- /// Required to call the ControlService function to pause or continue
- /// the service.
- /// </summary>
- let SERVICE_PAUSE_CONTINUE = uint32 0x00040
- /// <summary>
- /// Required to call the EnumDependentServices function to enumerate all
- /// the services dependent on the service.
- /// </summary>
- let SERVICE_INTERROGATE = uint32 0x00080
- /// <summary>
- /// Required to call the ControlService function to specify a user-defined
- /// control code.
- /// </summary>
- let SERVICE_USER_DEFINED_CONTROL = uint32 0x00100
- /// <summary>
- /// Includes STANDARD_RIGHTS_REQUIRED in addition to all access rights in this table.
- /// </summary>
- let SERVICE_ALL_ACCESS =
- uint32 (
- ACCESS_MASK.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)
- let GENERIC_READ =
- uint32 (
- ACCESS_MASK.STANDARD_RIGHTS_READ |||
- SERVICE_QUERY_CONFIG |||
- SERVICE_QUERY_STATUS |||
- SERVICE_INTERROGATE |||
- SERVICE_ENUMERATE_DEPENDENTS)
- let GENERIC_WRITE =
- uint32 (
- ACCESS_MASK.STANDARD_RIGHTS_WRITE |||
- SERVICE_CHANGE_CONFIG)
- let GENERIC_EXECUTE =
- uint32 (
- ACCESS_MASK.STANDARD_RIGHTS_EXECUTE |||
- SERVICE_START |||
- SERVICE_STOP |||
- SERVICE_PAUSE_CONTINUE |||
- SERVICE_USER_DEFINED_CONTROL )
- /// <summary>
- /// Required to call the QueryServiceObjectSecurity or
- /// SetServiceObjectSecurity function to access the SACL. The proper
- /// way to obtain this access is to enable the SE_SECURITY_NAME
- /// privilege in the caller's current access token, open the handle
- /// for ACCESS_SYSTEM_SECURITY access, and then disable the privilege.
- /// </summary>
- let ACCESS_SYSTEM_SECURITY = uint32 ACCESS_MASK.ACCESS_SYSTEM_SECURITY
- /// <summary>
- /// Required to call the DeleteService function to delete the service.
- /// </summary>
- let DELETE = uint32 ACCESS_MASK.DELETE
- /// <summary>
- /// Required to call the QueryServiceObjectSecurity function to query
- /// the security descriptor of the service object.
- /// </summary>
- let READ_CONTROL = uint32 ACCESS_MASK.READ_CONTROL
- /// <summary>
- /// Required to call the SetServiceObjectSecurity function to modify
- /// the Dacl member of the service object's security descriptor.
- /// </summary>
- let WRITE_DAC = uint32 ACCESS_MASK.WRITE_DAC
- /// <summary>
- /// Required to call the SetServiceObjectSecurity function to modify
- /// the Owner and Group members of the service object's security
- /// descriptor.
- /// </summary>
- let WRITE_OWNER = uint32 ACCESS_MASK.WRITE_OWNER
- module SERVICE_TYPE =
- /// <summary>
- /// Driver service.
- /// </summary>
- let SERVICE_KERNEL_DRIVER = uint32 0x00000001
- /// <summary>
- /// File system driver service.
- /// </summary>
- let SERVICE_FILE_SYSTEM_DRIVER = uint32 0x00000002
- /// <summary>
- /// Service that runs in its own process.
- /// </summary>
- let SERVICE_WIN32_OWN_PROCESS = uint32 0x00000010
- /// <summary>
- /// Service that shares a process with one or more other services.
- /// </summary>
- let SERVICE_WIN32_SHARE_PROCESS = uint32 0x00000020
- /// <summary>
- /// The service can interact with the desktop.
- /// </summary>
- let SERVICE_INTERACTIVE_PROCESS = uint32 0x00000100
- /// <summary>
- /// Service start options
- /// </summary>
- module SERVICE_START =
- /// <summary>
- /// A device driver started by the system loader. This value is valid
- /// only for driver services.
- /// </summary>
- let SERVICE_BOOT_START = uint32 0x00000000
- /// <summary>
- /// A device driver started by the IoInitSystem function. This value
- /// is valid only for driver services.
- /// </summary>
- let SERVICE_SYSTEM_START = uint32 0x00000001
- /// <summary>
- /// A service started automatically by the service control manager
- /// during system startup. For more information, see Automatically
- /// Starting Services.
- /// </summary>
- let SERVICE_AUTO_START = uint32 0x00000002
- /// <summary>
- /// A service started by the service control manager when a process
- /// calls the StartService function. For more information, see
- /// Starting Services on Demand.
- /// </summary>
- let SERVICE_DEMAND_START = uint32 0x00000003
- /// <summary>
- /// A service that cannot be started. Attempts to start the service
- /// result in the error code ERROR_SERVICE_DISABLED.
- /// </summary>
- let SERVICE_DISABLED = uint32 0x00000004
- /// <summary>
- /// Severity of the error, and action taken, if this service fails
- /// to start.
- /// </summary>
- module SERVICE_ERROR =
- /// <summary>
- /// The startup program ignores the error and continues the startup
- /// operation.
- /// </summary>
- let SERVICE_ERROR_IGNORE = uint32 0x00000000
- /// <summary>
- /// The startup program logs the error in the event log but continues
- /// the startup operation.
- /// </summary>
- let SERVICE_ERROR_NORMAL = uint32 0x00000001
- /// <summary>
- /// The startup program logs the error in the event log. If the
- /// last-known-good configuration is being started, the startup
- /// operation continues. Otherwise, the system is restarted with
- /// the last-known-good configuration.
- /// </summary>
- let SERVICE_ERROR_SEVERE = uint32 0x00000002
- /// <summary>
- /// The startup program logs the error in the event log, if possible.
- /// If the last-known-good configuration is being started, the startup
- /// operation fails. Otherwise, the system is restarted with the
- /// last-known good configuration.
- /// </summary>
- let SERVICE_ERROR_CRITICAL = uint32 0x00000003
- module ServiceManagerRights =
- /// <summary>
- ///
- /// </summary>
- let Connect = unativeint 0x00001
- /// <summary>
- ///
- /// </summary>
- let CreateService = unativeint 0x00002
- /// <summary>
- ///
- /// </summary>
- let EnumerateService = unativeint 0x00004
- /// <summary>
- ///
- /// </summary>
- let Lock = unativeint 0x0008
- /// <summary>
- ///
- /// </summary>
- let QueryLockStatus = unativeint 0x00010
- /// <summary>
- ///
- /// </summary>
- let ModifyBootConfig = unativeint 0x00020
- /// <summary>
- ///
- /// </summary>
- let StandardRightsRequired = unativeint 0xF0000
- /// <summary>
- ///
- /// </summary>
- let AllAccess =
- unativeint (StandardRightsRequired ||| Connect ||| CreateService |||
- EnumerateService ||| Lock ||| QueryLockStatus ||| ModifyBootConfig)
- /// <summary>
- ///
- /// </summary>
- module ServiceState =
- /// <summary>
- ///
- /// </summary>
- let Unknown = -1 // The state cannot be (has not been) retrieved.
- /// <summary>
- ///
- /// </summary>
- let NotFound = 0 // The service is not known on the host server.
- /// <summary>
- ///
- /// </summary>
- let Stop = 1 // The service is NET stopped.
- /// <summary>
- ///
- /// </summary>
- let Run = 2 // The service is NET started.
- /// <summary>
- ///
- /// </summary>
- let Stopping = 3
- /// <summary>
- ///
- /// </summary>
- let Starting = 4
- /// <summary>
- ///
- /// </summary>
- module ServiceControl =
- /// <summary>
- ///
- /// </summary>
- let Stop = 0x00000001
- /// <summary>
- ///
- /// </summary>
- let Pause = 0x00000002
- /// <summary>
- ///
- /// </summary>
- let Continue = 0x00000003
- /// <summary>
- ///
- /// </summary>
- let Interrogate = 0x00000004
- /// <summary>
- ///
- /// </summary>
- let Shutdown = 0x00000005
- /// <summary>
- ///
- /// </summary>
- let ParamChange = 0x00000006
- /// <summary>
- ///
- /// </summary>
- let NetBindAdd = 0x00000007
- /// <summary>
- ///
- /// </summary>
- let NetBindRemove = 0x00000008
- /// <summary>
- ///
- /// </summary>
- let NetBindEnable = 0x00000009
- /// <summary>
- ///
- /// </summary>
- let NetBindDisable = 0x0000000A
- [<StructLayout(LayoutKind.Sequential)>]
- type SERVICE_STATUS =
- struct
- [<DefaultValue>] val mutable dwServiceType: int
- [<DefaultValue>] val mutable dwCurrentState: int
- [<DefaultValue>] val mutable dwControlsAccepted: int
- [<DefaultValue>] val mutable dwWin32ExitCode: int
- [<DefaultValue>] val mutable dwServiceSpecificExitCode: int
- [<DefaultValue>] val mutable dwCheckPoint: int
- [<DefaultValue>] val mutable dwWaitHint: int
- end
- [<DllImport("advapi32.dll", EntryPoint="OpenSCManagerW", CharSet=CharSet.Unicode, SetLastError=true)>]
- extern IntPtr OpenSCManager(
- string lpMachineName,
- string lpDatabaseName,
- unativeint dwDesiredAccess);
- [<DllImport("advapi32.dll", EntryPoint = "OpenServiceW", SetLastError=true, CharSet = CharSet.Unicode)>]
- extern IntPtr OpenService(
- IntPtr hSCManager,
- string lpServiceName,
- int dwDesiredAccess);
- [<DllImport(@"advapi32.dll", EntryPoint = "CreateServiceW", SetLastError=true, CharSet=CharSet.Unicode)>]
- extern IntPtr CreateService(
- IntPtr hSCManager,
- string lpServiceName,
- string lpDisplayName,
- uint32 dwDesiredAccess,
- uint32 dwServiceType,
- uint32 dwStartType,
- uint32 dwErrorControl,
- string lpBinaryPathName,
- string lpLoadOrderGroup,
- string lpdwTagId,
- string lpDependencies,
- string lpServiceStartName,
- string lpPassword);
- [<DllImport("advapi32.dll", SetLastError=true)>]
- extern int CloseServiceHandle(IntPtr hSCObject);
- [<DllImport("advapi32.dll", SetLastError=true)>]
- extern int QueryServiceStatus(
- IntPtr hService,
- SERVICE_STATUS lpServiceStatus);
- [<DllImport("advapi32.dll", SetLastError = true)>]
- extern int DeleteService(IntPtr hService);
- [<DllImport("advapi32.dll", SetLastError=true)>]
- extern int ControlService(
- IntPtr hService,
- int dwControl,
- SERVICE_STATUS lpServiceStatus);
- [<DllImport("advapi32.dll", EntryPoint = "StartServiceW", CharSet=CharSet.Unicode , SetLastError=true)>]
- extern int StartService(
- IntPtr hService,
- int dwNumServiceArgs,
- int lpServiceArgVectors);
- module ServiceControl =
- open advapi32
- /// <summary>
- /// Takes a service name, a service display name and the path to the service executable and installs / starts the windows service.
- /// </summary>
- /// <param name="ServiceName">The service name that this service will have</param>
- /// <param name="DisplayName">The display name that this service will have</param>
- /// <param name="FileName">The path to the executable of the service</param>
- /// <param name="UserName">Account name</param>
- /// <param name="Password">Password</param>
- let Install
- (ServiceName: string)
- (DisplayName: string)
- (FileName: string)
- (UserName: string)
- (Password: string) =
- //ServiceManagerRights.CreateService will need elevation
- let scman: IntPtr = OpenSCManager(null, null, unativeint(ServiceManagerRights.Connect ||| ServiceManagerRights.EnumerateService ||| ServiceManagerRights.CreateService));
- try
- if scman = IntPtr.Zero then failwithf "OpenSCManager: %d" (Marshal.GetLastWin32Error())
- let mutable service: IntPtr = OpenService(scman, ServiceName, int (SERVICE_ACCESS.SERVICE_QUERY_STATUS ||| SERVICE_ACCESS.SERVICE_START));
- if service = IntPtr.Zero then
- service <- CreateService(
- scman,
- ServiceName,
- DisplayName,
- SERVICE_ACCESS.SERVICE_QUERY_STATUS ||| SERVICE_ACCESS.SERVICE_START,
- SERVICE_TYPE.SERVICE_WIN32_OWN_PROCESS,
- SERVICE_START.SERVICE_AUTO_START,
- SERVICE_ERROR.SERVICE_ERROR_NORMAL,
- FileName,
- null,
- null,
- null,
- UserName,
- Password)
- if service = IntPtr.Zero then failwithf "CreateService: %d" (Marshal.GetLastWin32Error())
- service
- finally
- CloseServiceHandle(scman) |> ignore
- ServiceControl.InstallAndStart "lol" "lol" "lol" "Cukor" "tvojamatka";;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement