Installing services programmically
-
Hiya all. I use advapi32.dll via VB.net to install a service, however I do not seem to think it goes as well as I'd like. Often when I run the install, everything seems to indicate that it succeedes, but I can't find the service in neither my service list nor registry - although if I use the ServiceController class, I can get a status out on the service. Similar when I attempt to start or stop the service, the code seems to run fine, but .... alas no service work seems to be done. Lastly - I am having problems, again similar, with uninstalling - and about 50/50 I either can or can not uninstall (meaning the code runs through fine or not), yet because I can not see my service in the list when installed - I have no visible proof it is uninstalled other then the ServiceController class crashes if I try to get status. This is the code I am using (VB.NET ... sorry, my company uses that instead of C# :) )
Private Declare Auto Function OpenSCManager Lib "advapi32.dll" (ByVal strMachineName As String, ByVal strServiceControllerDB As String, ByVal intServiceController As Integer) As IntPtr Private Declare Auto Function CreateService Lib "advapi32.dll" (ByVal SCHANDLE As IntPtr, ByVal strServiceName As String, ByVal strDisplayName As String, _ ByVal intDesiredAccess As Integer, ByVal intServiceType As Integer, ByVal intStartType As Integer, ByVal intErrorControl As Integer, ByVal strServicePath As String, _ ByVal strLoadOrderGroup As String, ByVal intTagId As Integer, ByVal strDependencies As String, ByVal strServiceStartName As String, ByVal strPassword As String) As IntPtr Private Declare Auto Sub CloseServiceHandle Lib "advapi32.dll" (ByVal SCHANDLE As IntPtr) Private Declare Auto Function StartService Lib "advapi32.dll" (ByVal SVHANDLE As IntPtr, ByVal intNumServiceArgs As Integer, ByVal strServiceArgVectors As String) As Integer Private Declare Auto Function OpenService Lib "advapi32.dll" (ByVal SCHANDLE As IntPtr, ByVal strServiceName As String, ByVal intNumServiceArgs As Integer) As IntPtr Private Declare Auto Function DeleteService Lib "advapi32.dll" (ByVal SVHANDLE As IntPtr) As Integer Private Declare Auto Function GetLastError Lib "advapi32.dll" () As Integer Private Declare Function ControlService Lib "advapi32.dll" (ByVal SVHANDLE As IntPtr, ByVal intControl As Integer, ByVal objServiceStatus As SERVICE_STATUS) As Integer Friend Function InstallService(ByVal strServicePath As String, ByVal strServiceName As String, ByVal strServiceDisplayName As String) As Boolean