Click here to Skip to main content
15,896,475 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: Connection String Pin
Paul Conrad9-Dec-06 9:53
professionalPaul Conrad9-Dec-06 9:53 
GeneralRe: Connection String Pin
Guffa9-Dec-06 19:05
Guffa9-Dec-06 19:05 
GeneralRe: Connection String Pin
S Douglas9-Dec-06 19:58
professionalS Douglas9-Dec-06 19:58 
Questionimage file Pin
Tauseef A8-Dec-06 18:42
Tauseef A8-Dec-06 18:42 
AnswerRe: image file Pin
Guffa9-Dec-06 8:04
Guffa9-Dec-06 8:04 
QuestionAutocomplete VB.NET Pin
Xequence8-Dec-06 10:23
Xequence8-Dec-06 10:23 
AnswerRe: Autocomplete VB.NET Pin
Paul Conrad25-Dec-06 18:44
professionalPaul Conrad25-Dec-06 18:44 
GeneralInstalling services programmically Pin
Alsvha7-Dec-06 20:27
Alsvha7-Dec-06 20:27 
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# Smile | :) )

<br />
 Private Declare Auto Function OpenSCManager Lib "advapi32.dll" (ByVal strMachineName As String, ByVal strServiceControllerDB As String, ByVal intServiceController As Integer) As IntPtr<br />
    Private Declare Auto Function CreateService Lib "advapi32.dll" (ByVal SCHANDLE As IntPtr, ByVal strServiceName As String, ByVal strDisplayName As String, _<br />
    ByVal intDesiredAccess As Integer, ByVal intServiceType As Integer, ByVal intStartType As Integer, ByVal intErrorControl As Integer, ByVal strServicePath As String, _<br />
    ByVal strLoadOrderGroup As String, ByVal intTagId As Integer, ByVal strDependencies As String, ByVal strServiceStartName As String, ByVal strPassword As String) As IntPtr<br />
    Private Declare Auto Sub CloseServiceHandle Lib "advapi32.dll" (ByVal SCHANDLE As IntPtr)<br />
    Private Declare Auto Function StartService Lib "advapi32.dll" (ByVal SVHANDLE As IntPtr, ByVal intNumServiceArgs As Integer, ByVal strServiceArgVectors As String) As Integer<br />
    Private Declare Auto Function OpenService Lib "advapi32.dll" (ByVal SCHANDLE As IntPtr, ByVal strServiceName As String, ByVal intNumServiceArgs As Integer) As IntPtr<br />
    Private Declare Auto Function DeleteService Lib "advapi32.dll" (ByVal SVHANDLE As IntPtr) As Integer<br />
    Private Declare Auto Function GetLastError Lib "advapi32.dll" () As Integer<br />
    Private Declare Function ControlService Lib "advapi32.dll" (ByVal SVHANDLE As IntPtr, ByVal intControl As Integer, ByVal objServiceStatus As SERVICE_STATUS) As Integer<br />
<br />
 Friend Function InstallService(ByVal strServicePath As String, ByVal strServiceName As String, ByVal strServiceDisplayName As String) As Boolean<br />
        Try<br />
            'Get handle to Service Control Manager<br />
            Dim SCMHANDLE As IntPtr = OpenSCManager(Nothing, Nothing, SC_MANAGER_CREATE_SERVICE)<br />
            If SCMHANDLE.ToInt32 <> 0 Then<br />
                'Get Handle to Service<br />
                Dim SERVICEHANDLE As IntPtr = CreateService(SCMHANDLE, strServiceName, strServicePath, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, strServiceDisplayName, Nothing, 0, Nothing, Nothing, Nothing)<br />
                If SERVICEHANDLE.ToInt32 <> 0 Then<br />
                    If StartService(SERVICEHANDLE, 0, Nothing) <> 0 Then<br />
                        'Service started, close handler.<br />
                        CloseServiceHandle(SCMHANDLE)<br />
                        Return True<br />
                    Else<br />
                        'Service could not be started. Service could be running as well ( or installed?)<br />
                        Return False<br />
                    End If<br />
                Else<br />
                    'Service handler could not be opened.<br />
                End If<br />
            Else<br />
                'Service Control manager could not be opened.<br />
                Return False<br />
            End If<br />
        Catch ex As Exception<br />
            Throw ex<br />
        End Try<br />
    End Function<br />
<br />
  Friend Function Uninstall(ByVal strServiceName As String)<br />
        Dim GENERIC_WRITE As Integer = &H40000000<br />
        'Get handle to Service Control Manager<br />
        Dim SCMHANDLE As IntPtr = OpenSCManager(Nothing, Nothing, GENERIC_WRITE)<br />
        'Get Handle to Service<br />
        If SCMHANDLE.ToInt32 <> 0 Then<br />
            Dim DELETE As Integer = &H10000<br />
            Dim SERVICEHANDLE As IntPtr = OpenService(SCMHANDLE, strServiceName, DELETE)<br />
            If SERVICEHANDLE.ToInt32 <> 0 Then<br />
                If DeleteService(SERVICEHANDLE) <> 0 Then<br />
                    'Service deleted<br />
                    CloseServiceHandle(SCMHANDLE)<br />
                    Return True<br />
                Else<br />
                    'Service could not be deleted<br />
                    CloseServiceHandle(SCMHANDLE)<br />
                    Return False<br />
                End If<br />
            Else<br />
                'Service handler could not be opened.<br />
                Return False<br />
            End If<br />
        Else<br />
            'Service Control manager could not be opened.<br />
            Return False<br />
        End If<br />
    End Function<br />
<br />


The code is a piecing of stuff I found off the net and some decompiling.

Can anybody advice me as to what I'm doing wrong, or is this method of controlling service installation buggy. I really would preferer such a method as this as I do not really want to call Shell and instalutil Smile | :)

Regards.



Questionxml and database Pin
samita_friendly7-Dec-06 13:07
samita_friendly7-Dec-06 13:07 
QuestionProblem referencing embedded resource Pin
dBrong7-Dec-06 8:06
dBrong7-Dec-06 8:06 
AnswerRe: Problem referencing embedded resource [modified] Pin
Luc Pattyn7-Dec-06 10:56
sitebuilderLuc Pattyn7-Dec-06 10:56 
QuestionSystem.Xml.Serialization.XmlSerializer [modified] Pin
Hendrik Debedts6-Dec-06 23:46
Hendrik Debedts6-Dec-06 23:46 
QuestionSaving configuration problem Pin
Julia16-Dec-06 21:23
Julia16-Dec-06 21:23 
QuestionWebservice (encrypting soap username and password) Pin
poongu6-Dec-06 18:59
poongu6-Dec-06 18:59 
AnswerRe: Webservice (encrypting soap username and password) Pin
Vasudevan Deepak Kumar12-Dec-06 3:24
Vasudevan Deepak Kumar12-Dec-06 3:24 
Questionwant to retrieve 2 messages using 2 receive shapes in a orchrestra-BizTalk Pin
mahavir patil6-Dec-06 15:55
mahavir patil6-Dec-06 15:55 
Question.NET app and the App Paths registry key Pin
Scott Dorman6-Dec-06 4:14
professionalScott Dorman6-Dec-06 4:14 
AnswerRe: .NET app and the App Paths registry key Pin
ednrgc11-Dec-06 3:09
ednrgc11-Dec-06 3:09 
GeneralRe: .NET app and the App Paths registry key Pin
Scott Dorman13-Dec-06 3:56
professionalScott Dorman13-Dec-06 3:56 
QuestionVector graphic Pin
asestar6-Dec-06 3:41
asestar6-Dec-06 3:41 
AnswerRe: Vector graphic Pin
Paul Conrad26-Dec-06 17:07
professionalPaul Conrad26-Dec-06 17:07 
GeneralRe: Vector graphic Pin
asestar5-Jan-07 5:44
asestar5-Jan-07 5:44 
Questioncrystalr reports Pin
shaz jazz6-Dec-06 1:17
shaz jazz6-Dec-06 1:17 
AnswerRe: crystalr reports Pin
gnadeem10-Dec-06 6:32
gnadeem10-Dec-06 6:32 
Questionfaulting module mscorwks.dll .Net Runtime 2.0 Error Reporting Pin
bigeyed5-Dec-06 17:47
bigeyed5-Dec-06 17:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.