Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stop and start a service selected from a ListView 1

Status
Not open for further replies.

LFC8

Programmer
Nov 18, 2004
45
0
0
GB
Hi

I want to be able to stop and start a service that is selected from a ListView(using XP), I've tried searching but cant find anything, does anyone have any ideas?

Thanks
 
You can start and stop services from a command prompt, like so...

Net Stop [!]ServiceName[/!]
Net Start [!]ServiceName[/!]

So, you can accomplish the same thing with VB with...

call Shell("CMD /C Net Stop [!]ServiceName[/!]")



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Hi gmmastros

I'm looping through the Listview and stopping the selected service, here is what i have so far but i'm getting a type mismatch 13 i've stepped through the code and it looks fine up until it tries to stop the selected service.

Code:
Private Sub cmdEndServ_click()
    Dim i As Integer
    
    For i = 0 To ListView1.ListItems.Count - 1
        If ListView1.SelectedItem = True Then
            ServiceStop "", ListView1.SelectedItem <<<Error
        End If
    Next i
End Sub

Any ideas

TIA
 
ServiceStop is a procedure you wrote? Can you post the code for it. I interested to see what the parameters are for this subroutine.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Hi gmmastros

This is a procedure i got off someone else. The parameters are ComputerName & Service name.

Code:
Public Const SERVICE_CONTINUE_PENDING = &H5
Public Const SERVICE_PAUSE_PENDING = &H6
Public Const SERVICE_PAUSED = &H7
'Service Control Manager object specific access types
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const SC_MANAGER_CONNECT = &H1
Public Const SC_MANAGER_CREATE_SERVICE = &H2
Public Const SC_MANAGER_ENUMERATE_SERVICE = &H4
Public Const SC_MANAGER_LOCK = &H8
Public Const SC_MANAGER_QUERY_LOCK_STATUS = &H10
Public Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20
Public Const SC_MANAGER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SC_MANAGER_CONNECT Or SC_MANAGER_CREATE_SERVICE Or SC_MANAGER_ENUMERATE_SERVICE Or SC_MANAGER_LOCK Or SC_MANAGER_QUERY_LOCK_STATUS Or SC_MANAGER_MODIFY_BOOT_CONFIG)
'Service object specific access types
Public Const SERVICE_QUERY_CONFIG = &H1
Public Const SERVICE_CHANGE_CONFIG = &H2
Public Const SERVICE_QUERY_STATUS = &H4
Public Const SERVICE_ENUMERATE_DEPENDENTS = &H8
Public Const SERVICE_START = &H10
Public Const SERVICE_STOP = &H20
Public Const SERVICE_PAUSE_CONTINUE = &H40
Public Const SERVICE_INTERROGATE = &H80
Public Const SERVICE_USER_DEFINED_CONTROL = &H100
Public Const SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG Or SERVICE_QUERY_STATUS Or SERVICE_ENUMERATE_DEPENDENTS Or SERVICE_START Or SERVICE_STOP Or SERVICE_PAUSE_CONTINUE Or SERVICE_INTERROGATE Or SERVICE_USER_DEFINED_CONTROL)

Type SERVICE_STATUS
    dwServiceType As Long
    dwCurrentState As Long
    dwControlsAccepted As Long
    dwWin32ExitCode As Long
    dwServiceSpecificExitCode As Long
    dwCheckPoint As Long
    dwWaitHint As Long
End Type

Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
Declare Function ControlService Lib "advapi32.dll" (ByVal hService As Long, ByVal dwControl As Long, lpServiceStatus As SERVICE_STATUS) As Long
Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal dwDesiredAccess As Long) As Long
Declare Function QueryServiceStatus Lib "advapi32.dll" (ByVal hService As Long, lpServiceStatus As SERVICE_STATUS) As Long
Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" (ByVal hService As Long, ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As Long
   
Public Function ServiceStatus(ComputerName As String, ServiceName As String) As String
    Dim ServiceStat As SERVICE_STATUS
    Dim hSManager As Long
    Dim hService As Long
    Dim hServiceStatus As Long

    ServiceStatus = ""
    hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
    If hSManager <> 0 Then
        hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
        If hService <> 0 Then
            hServiceStatus = QueryServiceStatus(hService, ServiceStat)
            If hServiceStatus <> 0 Then
                Select Case ServiceStat.dwCurrentState
                Case SERVICE_STOPPED
                    ServiceStatus = "Stopped"
                Case SERVICE_START_PENDING
                    ServiceStatus = "Start Pending"
                Case SERVICE_STOP_PENDING
                    ServiceStatus = "Stop Pending"
                Case SERVICE_RUNNING
                    ServiceStatus = "Running"
                Case SERVICE_CONTINUE_PENDING
                    ServiceStatus = "Coninue Pending"
                Case SERVICE_PAUSE_PENDING
                    ServiceStatus = "Pause Pending"
                Case SERVICE_PAUSED
                    ServiceStatus = "Paused"
                End Select
            End If
            CloseServiceHandle hService
        End If
        CloseServiceHandle hSManager
    End If
End Function

Public Sub ServicePause(ComputerName As String, ServiceName As String)
    Dim ServiceStatus As SERVICE_STATUS
    Dim hSManager As Long
    Dim hService As Long
    Dim res As Long

    hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
    If hSManager <> 0 Then
        hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
        If hService <> 0 Then
            res = ControlService(hService, SERVICE_CONTROL_PAUSE, ServiceStatus)
            CloseServiceHandle hService
        End If
        CloseServiceHandle hSManager
    End If
End Sub

Public Sub ServiceStart(ComputerName As String, ServiceName As String)
    Dim ServiceStatus As SERVICE_STATUS
    Dim hSManager As Long
    Dim hService As Long
    Dim res As Long

    hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
    If hSManager <> 0 Then
        hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
        If hService <> 0 Then
            res = StartService(hService, 0, 0)
            CloseServiceHandle hService
        End If
        CloseServiceHandle hSManager
    End If
End Sub

Public Sub ServiceStop(ComputerName As String, ServiceName As String)
    Dim ServiceStatus As SERVICE_STATUS
    Dim hSManager As Long
    Dim hService As Long
    Dim res As Long

    hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
    If hSManager <> 0 Then
        hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
        If hService <> 0 Then
            res = ControlService(hService, SERVICE_CONTROL_STOP, ServiceStatus)
            CloseServiceHandle hService
        End If
        CloseServiceHandle hSManager
    End If
End Sub

Thanks
 
There's your problem...

You have ServiceStop "", ListView1.SelectedItem

So the second parameter you pass in is the List View's Selected item, which is not a string. From the help files...

SelectedItem Property (ActiveX Controls)

Returns a reference to a selected ListItem, Node, or Tab object.

You need to get the name of the service from the list view control as a string. Perhaps it could be as simple as...

ServiceStop "", ListView1.SelectedItem[!].Text[/!]

I don't have much experience with the ListView control, so this may not be the exact syntax you need. I hope it helps.


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Hi

Yeah just tried that and no joy, even tried creating a string variable and equaling it to the selected item from the listview box but yet again no good!

Code:
Private Sub cmdEndServ_click()
    Dim i As Integer
    Dim SelServ As String
    
    For i = 0 To ListView1.ListItems.Count - 1
        If ListView1.SelectedItem.Text Then
            SelServ = ListView1.SelectedItem.Text
            ServiceStop "", SelServ
        End If
    Next i
End Sub

If you have any more ideas i can try that would be great

Thanks
 
gmmastros,
There is technically no harm in passing the ListItem object as ServiceName argument to the ServiceStop function. When VB sees that an object passed in place of a string, it tries to evaluate the default property or method that can be evaluated as string (if any).

If it finds such a member, it uses its value and goes on without any complain, otherwise, it throws a runtime error.

Same happens in this case; when VB sees the following statement:

ServiceStop "", ListView1.SelectedItem

It automatically evaluates the .Text property of the SelectedItem object and passes it as argument to the ServiceStop function. This is same as explicitly passing ListView1.SelectedItem.Text.

I remember we had some more discussion on this topic in past, but I cannot find that thread now.

LFC8,
Your code is not working because it misses two constant definitions used in ServiceStop function.
[tt]
Public Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
Public Const SERVICE_CONTROL_STOP = &H1
[/tt]
Since VB does not know their value, it passes the default values "" and 0 in their place respectively, resulting in failure.

I checked the ServiceStop function after adding these two constants, and it worked successfully.

If you put [tt]Option Explicit[/tt] at the top of your module, you will come to know that following constant definitions are also missing from your code.
[tt]
Public Const SERVICE_STOPPED = &H1
Public Const SERVICE_START_PENDING = &H2
Public Const SERVICE_STOP_PENDING = &H3
Public Const SERVICE_RUNNING = &H4
Public Const SERVICE_CONTROL_PAUSE = &H2[/tt]
 
My thinking is that George needs a star.

The only problme you might have with the code (after adding Georges Constants) is simple permissons. If you are not a member of a group with permission to stop or start services.. It won't work..

but if you are....
 
NoCoolHandle,

I think you have me confused with Hypetia. It was Hypetia's comments that helped. I feel as though I haven't helped at all. Sorry.

Hypetia,

Thanks for pointing out default properties. I am aware of them, but I have limited experience with the ListView control and didn't know that the Text property was the default property of the ListItem object. (although I should have guessed).

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Looks like we haven't all read faq222-2244 where para 15 shows you how to do it! I'll do it for you this time, as I found the detail illuminating. I just click on the < Thank Hypetia
for this valuable post!> and it happens!

Now that wasn't too hard was it?

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Microsoft decided they didn't like default values, and eliminated them for VB.NET
 
Thanks everyone for all your input into this problem however i did have them constants in my module

Code:
'API Constants
Public Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
' Service Control
Public Const SERVICE_CONTROL_STOP = &H1
Public Const SERVICE_CONTROL_PAUSE = &H2
' Service State - for CurrentState
Public Const SERVICE_STOPPED = &H1
Public Const SERVICE_START_PENDING = &H2
Public Const SERVICE_STOP_PENDING = &H3
Public Const SERVICE_RUNNING = &H4
Public Const SERVICE_CONTINUE_PENDING = &H5
Public Const SERVICE_PAUSE_PENDING = &H6
Public Const SERVICE_PAUSED = &H7
'Service Control Manager object specific access types
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const SC_MANAGER_CONNECT = &H1
Public Const SC_MANAGER_CREATE_SERVICE = &H2
Public Const SC_MANAGER_ENUMERATE_SERVICE = &H4
Public Const SC_MANAGER_LOCK = &H8
Public Const SC_MANAGER_QUERY_LOCK_STATUS = &H10
Public Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20
Public Const SC_MANAGER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SC_MANAGER_CONNECT Or SC_MANAGER_CREATE_SERVICE Or SC_MANAGER_ENUMERATE_SERVICE Or SC_MANAGER_LOCK Or SC_MANAGER_QUERY_LOCK_STATUS Or SC_MANAGER_MODIFY_BOOT_CONFIG)
'Service object specific access types
Public Const SERVICE_QUERY_CONFIG = &H1
Public Const SERVICE_CHANGE_CONFIG = &H2
Public Const SERVICE_QUERY_STATUS = &H4
Public Const SERVICE_ENUMERATE_DEPENDENTS = &H8
Public Const SERVICE_START = &H10
Public Const SERVICE_STOP = &H20
Public Const SERVICE_PAUSE_CONTINUE = &H40
Public Const SERVICE_INTERROGATE = &H80
Public Const SERVICE_USER_DEFINED_CONTROL = &H100
Public Const SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG Or SERVICE_QUERY_STATUS Or SERVICE_ENUMERATE_DEPENDENTS Or SERVICE_START Or SERVICE_STOP Or SERVICE_PAUSE_CONTINUE Or SERVICE_INTERROGATE Or SERVICE_USER_DEFINED_CONTROL)

Type SERVICE_STATUS
    dwServiceType As Long
    dwCurrentState As Long
    dwControlsAccepted As Long
    dwWin32ExitCode As Long
    dwServiceSpecificExitCode As Long
    dwCheckPoint As Long
    dwWaitHint As Long
End Type

Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
Declare Function ControlService Lib "advapi32.dll" (ByVal hService As Long, ByVal dwControl As Long, lpServiceStatus As SERVICE_STATUS) As Long
Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal dwDesiredAccess As Long) As Long
Declare Function QueryServiceStatus Lib "advapi32.dll" (ByVal hService As Long, lpServiceStatus As SERVICE_STATUS) As Long
Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" (ByVal hService As Long, ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As Long
 
gmmastros,
It's ok... You don't need to be sorry for any reason. My post wasn't intended for this.

johnwm,
Thanks for the star.

strongm,
Indeed this is true. Use of of default members make the code easier, but sometimes they create obfuscation, as in the above code. Good programming practices discourage the use of default properties due to the same reason.

I really wish I could find the thread I mentioned above, in which we discussed this topic in detail.

I remember that I was a big fan of default properties and always used them in my programs. But after the discussion in that thread, I quit using them because most of the people in that discussion were against them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top