var SERVICE_WIN32=16|32;var SERVICE_STATE={STOPPED:1,SERVICE_START_PENDING:2,SERVICE_STOP_PENDING:3,RUNNING:4};var SERVICE_ACCEPT={SERVICE_ACCEPT_STOP:1,SERVICE_ACCEPT_SHUTDOWN:4,SERVICE_ACCEPT_POWEREVENT:64,SERVICE_ACCEPT_SESSIONCHANGE:128};var SERVICE_CONTROL={SERVICE_CONTROL_SHUTDOWN:5,SERVICE_CONTROL_STOP:1,SERVICE_CONTROL_POWEREVENT:13,SERVICE_CONTROL_SESSIONCHANGE:14};var SESSION_CHANGE_TYPE={WTS_CONSOLE_CONNECT:1,WTS_CONSOLE_DISCONNECT:2,WTS_REMOTE_CONNECT:3,WTS_REMOTE_DISCONNECT:4,WTS_SESSION_LOGON:5,WTS_SESSION_LOGOFF:6,WTS_SESSION_LOCK:7,WTS_SESSION_UNLOCK:8,WTS_SESSION_REMOTE_CONTROL:9,WTS_SESSION_CREATE:10,WTS_SESSION_TERMINATE:11};var NO_ERROR=0;var serviceManager=require("service-manager");function serviceHost(e){this._ObjectID="service-host";var a=require("events").inherits(this);a.createEvent("serviceStart");a.createEvent("serviceStop");a.createEvent("normalStart");a.createEvent("session");a.createEvent("powerStateChange");if(process.platform=="win32"){this.GM=require("_GenericMarshal");this.Advapi=this.GM.CreateNativeProxy("Advapi32.dll");this.Advapi.CreateMethod({method:"StartServiceCtrlDispatcherA",threadDispatch:1});this.Advapi.CreateMethod("RegisterServiceCtrlHandlerExA");this.Advapi.CreateMethod("SetServiceStatus");this.Kernel32=this.GM.CreateNativeProxy("Kernel32.dll");this.Kernel32.CreateMethod("GetLastError");this.Ole32=this.GM.CreateNativeProxy("Ole32.dll");this.Ole32.CreateMethod("CoInitializeEx");this.Ole32.CreateMethod("CoUninitialize");this._ServiceName=this.GM.CreateVariable(typeof(e)=="string"?e:e.name);this._ServiceMain=this.GM.GetGenericGlobalCallback(2);this._ServiceMain.Parent=this;this._ServiceMain.GM=this.GM;this._ServiceMain.on("GlobalCallback",function b(f,g){this.Parent._ServiceStatus=this.GM.CreateVariable(28);this.Parent._ServiceStatus.toBuffer().writeUInt32LE(SERVICE_WIN32);this.Parent._ServiceStatus.toBuffer().writeUInt32LE(SERVICE_STATE.SERVICE_STOPPED,4);this.Parent._ServiceStatusHandle=this.Parent.Advapi.RegisterServiceCtrlHandlerExA(this.Parent._ServiceName,this.Parent._ServiceControlHandler,this.Parent.GM.StashObject(this.Parent._ServiceControlHandler));if(this.Parent._ServiceStatusHandle.Val==0){process.exit(1)}this.Parent._ServiceStatus.toBuffer().writeUInt32LE(SERVICE_STATE.SERVICE_START_PENDING,4);this.Parent.Advapi.SetServiceStatus(this.Parent._ServiceStatusHandle,this.Parent._ServiceStatus);this.Parent._ServiceStatus.toBuffer().writeUInt32LE(SERVICE_STATE.RUNNING,4);this.Parent._ServiceStatus.toBuffer().writeUInt32LE(SERVICE_ACCEPT.SERVICE_ACCEPT_STOP|SERVICE_ACCEPT.SERVICE_ACCEPT_POWEREVENT|SERVICE_ACCEPT.SERVICE_ACCEPT_SESSIONCHANGE,8);this.Parent.Advapi.SetServiceStatus(this.Parent._ServiceStatusHandle,this.Parent._ServiceStatus);this.Parent.Ole32.CoInitializeEx(0,2);this.Parent.on("~",function h(){var j=require("_GenericMarshal");var i=j.CreateNativeProxy("Advapi32.dll");i.CreateMethod("SetServiceStatus");Kernel32=this.GM.CreateNativeProxy("Kernel32.dll");Kernel32.CreateMethod("GetLastError");var k=j.CreateVariable(28);k.toBuffer().writeUInt32LE(SERVICE_WIN32);k.toBuffer().writeUInt32LE(1,4);k.toBuffer().writeUInt32LE(0,8);i.SetServiceStatus(this._ServiceStatusHandle,k);this.Ole32.CoUninitialize()});this.Parent.emit("serviceStart")});this._ServiceControlHandler=this.GM.GetGenericGlobalCallback(4);this._ServiceControlHandler.Parent=this;this._ServiceControlHandler.GM=this.GM;this._ServiceControlHandler.on("GlobalCallback",function c(f,i,h,g){var k=this.Parent.GM.UnstashObject(g);if(k!=null&&k==this){switch(f.Val){case SERVICE_CONTROL.SERVICE_CONTROL_SHUTDOWN:case SERVICE_CONTROL.SERVICE_CONTROL_STOP:this.Parent.emit("serviceStop");return;case SERVICE_CONTROL.SERVICE_CONTROL_SESSIONCHANGE:var l=h.Deref(4,4).toBuffer().readUInt32LE();switch(i.Val){case SESSION_CHANGE_TYPE.WTS_SESSION_LOGON:case SESSION_CHANGE_TYPE.WTS_SESSION_LOGOFF:require("user-sessions").emit("changed");break}break;default:break}this.Parent.Advapi.SetServiceStatus(this.Parent._ServiceStatusHandle,this.Parent._ServiceStatus)}})}if(e){this._ServiceOptions=typeof(e)=="object"?e:{name:e}}else{throw ("Must specify either ServiceName or Options")}if(!this._ServiceOptions.servicePath){this._ServiceOptions.servicePath=process.execPath}this.run=function d(){var r=0;for(var h=0;h