How to start, stop, pauze, restart or get the status of a service on your computer
With the code below you can manipulate services on your computer.
Calling the function
ServiceStop “”, “SERVICENAME”
Declaration
Option Explicit
‘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 TypePublic Declare Function CloseServiceHandle Lib “advapi32.dll” (ByVal hSCObject As Long) As Long
Public Declare Function ControlService Lib “advapi32.dll” (ByVal hService As Long, ByVal dwControl As Long, lpServiceStatus As SERVICE_STATUS) As Long
Public Declare Function OpenSCManager Lib “advapi32.dll” Alias “OpenSCManagerA” (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Public Declare Function OpenService Lib “advapi32.dll” Alias “OpenServiceA” (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal dwDesiredAccess As Long) As Long
Public Declare Function QueryServiceStatus Lib “advapi32.dll” (ByVal hService As Long, lpServiceStatus As SERVICE_STATUS) As Long
Public Declare Function StartService Lib “advapi32.dll” Alias “StartServiceA” (ByVal hService As Long, ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As Long
Functions
Status
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 LongServiceStatus = “”
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
Pause
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 LonghSManager = 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
Start
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 LonghSManager = 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
Stop
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 LonghSManager = 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
Popularity: 3% [?]
Stored procedure input and output parameters in vb6
The code belows shows how u can access your stored procedures from vb6.
In the example i use an input and output parameter so it is possible to select, update, insert data into the MS SQL 2000 database.
Stored procedure
CREATE Procedure MyStoredProcdure
(@ID integer,
@Description varchar(100) output
)
AS
select @Description = Description
from dbo.MNZ_FOTO
WHERE ID = @ID
GO
vb code
On Error GoTo ErrHandler
Set cn = New ADODB.Connection
cn.ConnectionString = Session.GetConnectionstring
cn.OpenSet cmd = New ADODB.Command
cmd.ActiveConnection = cn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = “MyStoredProcdure”
cmd.Parameters.Append cmd.CreateParameter(“ID”, adInteger, adParamInput, , 614)
cmd.Parameters.Append cmd.CreateParameter(“Description”, adVarChar, adParamOutput, 100)
cmd.ExecuteMsgBox cmd.Parameters(1).Value
Set cmd = Nothing
Set cn = Nothing
Exit Function
ErrHandler:
MsgBox Err.Description
Set cmd = Nothing
Set cn = Nothing
Popularity: 2% [?]
Showing a notifyicon with a pop-up menu
This vb6 code is used to show the small icon near to the system time (notify icon). In this example is also explained how u can create a pop-up menu by right-clicking on the icon.
Declaration
Option Explicit
‘ Type passed to Shell_NotifyIcon
Private Type NotifyIconData
Size As Long
Handle As Long
ID As Long
Flags As Long
CallBackMessage As Long
Icon As Long
Tip As String * 64
End Type‘ Constants for managing System Tray tasks, foudn in shellapi.h
Private Const AddIcon = &H0
Private Const ModifyIcon = &H1
Private Const DeleteIcon = &H2Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202Private Const WM_RBUTTONDBLCLK = &H206
Private Const WM_RBUTTONDOWN = &H204
Private Const WM_RBUTTONUP = &H205Private Const MessageFlag = &H1
Private Const IconFlag = &H2
Private Const TipFlag = &H4Private Declare Function Shell_NotifyIcon _
Lib “shell32″ Alias “Shell_NotifyIconA” ( _
ByVal Message As Long, Data As NotifyIconData) As BooleanPrivate Data As NotifyIconData
Functions
Private Sub Form_Load()
AddIconToTray
End SubPrivate Sub Form_Terminate()
DeleteIconFromTray
End SubPrivate Sub AddIconToTray()
Data.Size = Len(Data)
Data.Handle = hWnd
Data.ID = vbNull
Data.Flags = IconFlag Or TipFlag Or MessageFlag
Data.CallBackMessage = WM_MOUSEMOVE
Data.Icon = Me.Icon
Data.Tip = “The text for the tip” & vbNullChar
Call Shell_NotifyIcon(AddIcon, Data)End Sub
Private Sub DeleteIconFromTray()
Call Shell_NotifyIcon(DeleteIcon, Data)
End SubPrivate Sub Form_MouseMove(Button As Integer, _
Shift As Integer, X As Single, Y As Single)Dim Message As Long
Message = X / Screen.TwipsPerPixelXSelect Case Message
Case WM_LBUTTONDOWN
Case WM_RBUTTONDOWN
Call Me.PopupMenu(MenuPopup, , , , MenuClose)End Select
End SubPrivate Sub MenuClose_Click()
Form_TerminateEnd Sub
Popularity: 4% [?]


