Visual Basic Classic
--
Questions
--
Followers
Top Experts
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Shell Environ("ComSpec") &Â " /c net start messenger", vbHide






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Change it to 'net stop' to stop a service. If you change vbHide to vbNormalFocus you will see the command window open,run the command and close.
The answer in the second link is the API method to start/stop and query services.
This would be a better way to go, but is a bit more code.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Once the referene is set, the code will work on any machine provided that you have the
proper security privileges.
Example:
Private Sub Command1_Click()
 If StartService() Then
   Msgbox "The Messenger service started successfully"
 End If
End Sub
Private Sub Command2_Click()
 If StopService(, , True) Then
   Msgbox "The Messenger service stopped successfully and was disabled"
 End If
End Sub
You can start and stop any service by specifying the name of the service in the second parameter
of each function. Â You can also start and stop any service on any computer by specifying the name
of the desired computer in the first parameter. Â If you omitted, the local computer is used.
==========================
Option Explicit
' Constants for Service Status
Private Const STATUS_STOPPED = &H1
Private Const STATUS_START_PENDING = &H2
Private Const STATUS_STOP_PENDING = &H3
Private Const STATUS_RUNNING = &H4
Private Const STATUS_CONTINUE_PENDING = &H5
Private Const STATUS_PAUSE_PENDING = &H6
Private Const STATUS_PAUSED = &H7
Private Const STATUS_STARTING = &H8
' Startup Type enumerators
Public Enum STARTUP_TYPE_ENUM
  ST_AUTO = 2
  ST_MANUAL = 3
  ST_DISABLED = 4
End Enum
==========================
Public Function StartService(Optional ByVal sMachine As String, _
               Optional ByVal ServiceName As String = "Messenger", _
               Optional ByVal lStartupType As STARTUP_TYPE_ENUM = ST_AUTO) As Boolean
Â
 Dim oComp As ActiveDs.IADsComputer
 Dim oSvc1 As ActiveDs.IADsService
 Dim oSvc2 As ActiveDs.IADsServiceOperat
 Dim sComputerName As String
Â
 On Error GoTo Err_Handler
Â
 If sMachine = "" Then
  sMachine = Environ$("ComputerName")
 End If
Â
 sComputerName = "WinNT://" & sMachine & ",computer"
Â
 Set oComp = GetObject(sComputerName)
 Set oSvc1 = oComp.GetObject("Service",
 Set oSvc2 = oComp.GetObject("Service",
Â
 ' First, check the service start-up type
 ' and set it to the specified startup type
 If oSvc1.StartType <> lStartupType Then
  oSvc1.StartType = lStartupType
  oSvc1.SetInfo   'Apply changes
 End If
Â
 ' Second, check the current status
 ' and then start it as necessary
 If oSvc2.Status = STATUS_STOPPED Then
  oSvc2.Start
 End If
Â
 StartService = True
Â
 Exit Function
Err_Handler:
 StartService = False
 'MsgBox Err.Number & " - " & Err.Description
Â
End Function
==========================
Public Function StopService(Optional ByVal sMachine As String, _
              Optional ByVal ServiceName As String = "Messenger", _
              Optional ByVal bDisable As Boolean) As Boolean
 Dim oComp As ActiveDs.IADsComputer
 Dim oSvc1 As ActiveDs.IADsService
 Dim oSvc2 As ActiveDs.IADsServiceOperat
 Dim sComputerName As String
Â
 On Error GoTo Err_Handler
Â
 If sMachine = "" Then
  sMachine = Environ$("ComputerName")
 End If
Â
 sComputerName = "WinNT://" & sMachine & ",computer"
Â
 Set oComp = GetObject(sComputerName)
 Set oSvc1 = oComp.GetObject("Service",
 Set oSvc2 = oComp.GetObject("Service",
Â
 ' First, check to see if service is
 ' currently runnng and stop it
 If oSvc2.Status = STATUS_RUNNING Then
  oSvc2.Stop
 End If
Â
 ' Second, check the startup type to see
 ' if it has not already been disabled
 If bDisable Then
  If oSvc1.StartType <> STARTUP_DISABLED Then
   oSvc1.StartType = STARTUP_DISABLED
   oSvc1.SetInfo   'Apply changes
  End If
 End If
Â
 StopService = True
Â
 Exit Function
Err_Handler:
 StopService = False
 'MsgBox Err.Number & " - " & Err.Description
Â
End Function
==========================
i want to query if the PC the app is run on is connected to the LAN (if there is any network connection)
i would run this code in a timer with a small interval - so i don't want the proc to take too long.
Thanks (worth the other half of the points)






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Option Explicit
Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, _
  phWritePipe As Long, lpPipeAttributes As Any, ByVal nSize As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As String, _
  ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function CreateProcessA Lib "kernel32" (ByVal lpApplicationName As Long, _
  ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, _
  lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, _
  ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, _
  ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, _
  lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hHandle As Long) As Long
Private Type SECURITY_ATTRIBUTES
  nLength As Long
  lpSecurityDescriptor As Long
  bInheritHandle As Long
End Type
Private Type STARTUPINFO
  cb As Long
  lpReserved As Long
  lpDesktop As Long
  lpTitle As Long
  dwX As Long
  dwY As Long
  dwXSize As Long
  dwYSize As Long
  dwXCountChars As Long
  dwYCountChars As Long
  dwFillAttribute As Long
  dwFlags As Long
  wShowWindow As Integer
  cbReserved2 As Integer
  lpReserved2 As Long
  hStdInput As Long
  hStdOutput As Long
  hStdError As Long
End Type
Private Type PROCESS_INFORMATION
  hProcess As Long
  hThread As Long
  dwProcessID As Long
  dwThreadID As Long
End Type
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const STARTF_USESTDHANDLES = &H100&
Private Const STARTF_USESHOWWINDOW = &H1
Private DefaultGateway As String
Private Sub Form_Load()
DefaultGateway = GetDefaultGateway
End Sub
Private Sub Timer1_Timer()
If PingGateway = False Then
  MsgBox "Not Connected"
End If
End Sub
Private Function RunCommand(sCmd As String) As String
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim sa As SECURITY_ATTRIBUTES
Dim hReadPipe As Long, hWritePipe As Long
Dim ret As Long, lngBytesread As Long
Dim strBuff As String * 256, RetData As String
sa.nLength = Len(sa)
sa.bInheritHandle = 1&
sa.lpSecurityDescriptor = 0&
ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
start.cb = Len(start)
start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
start.hStdOutput = hWritePipe
start.hStdError = hWritePipe
ret = CreateProcessA(0&, sCmd, sa, sa, 1&, _
    NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
ret = CloseHandle(hWritePipe)
RetData = ""
Do
  ret = ReadFile(hReadPipe, strBuff, 256, lngBytesread, 0&)
  RetData = RetData & Left(strBuff, lngBytesread)
Loop While ret <>Â 0
ret = CloseHandle(proc.hProcess)
ret = CloseHandle(proc.hThread)
ret = CloseHandle(hReadPipe)
RunCommand = RetData
End Function
Private Function GetDefaultGateway() As String
Dim sCmd As String, pos As Integer
sCmd = RunCommand("ipconfig")
pos = InStr(sCmd, "Default Gateway")
pos = InStr(pos, sCmd, ":") + 1
GetDefaultGateway = Trim$(Mid$(sCmd, pos, 15))
End Function
Private Function PingGateway() As Boolean
Dim sCmd As String, pos As Integer
sCmd = RunCommand("ping -n 1 "Â & DefaultGateway)
pos = InStr(sCmd, "Received = 1")
If pos >Â 0 Then
  PingGateway = True
Else
  PingGateway = False
End If
End Function

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
btw i just used the lines:
tmrLan.Timer
sCmd = RunCommand("ipconfig")
if instr(UCase(scmd), "DISCONNECTED" then
  ' disconnected icon
else
 '  connected icon
endif
Regarding the Services part - works fine except for one thing - Messenger service by default on XP is DISABLED - which means my app can't run it (no errors generated - except when i query if it's running it says that it's stopped)
How in VB6 can i change it from Disabled to Automatic?
Thanks. almost done with this Q.
Ryan R
Option Explicit
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const ERROR_SUCCESS = 0&
Private Const REG_DWORD = 4
Private Sub Command1_Click()
Call EnableMessengerService
End Sub
Private Sub EnableMessengerService()
Dim lResult As Long, keyhand As Long, r As Long
r = RegCreateKey(HKEY_LOCAL_MA
lResult = RegSetValueEx(keyhand, "Start", 0&, REG_DWORD, 2, 4) ' 2=automatic
r = RegCloseKey(keyhand)
End Sub






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
However, when i run both items of code in the same app (leaving time inbetween running the two proc's) the code to change from Diasabled works but the code to start Messenger does absolutely NOTHING now - any ideas why???
Thanks
any ideas guys?

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
If module1.EnableMessengerSer
   module1.StartMessengerServ
End If
'''''''''''
That worked no probs... thanks a lot vinnyd79. Will close Q now.
Ryan R
Thanks again...

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Visual Basic Classic
--
Questions
--
Followers
Top Experts
Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.