What i mean is how can i make VB open an .exe file and tell it to be a Service.
Printable View
What i mean is how can i make VB open an .exe file and tell it to be a Service.
Try searching for the RegisterServiceProcess API.
You will also need OpenSCManager, StartService, StopService, and CreateService if this is going to
be on an NT platform OS.
hmm i can't seem to grasp this.. Can someone make me a Demo using Notepad.exe Thankz//
Ok, but is this for Windows 95/98 or NT 4.0+ ?
Also, local computer only or remote manipulation too?
If its for remote manipulation then its allot of code, even for a small demo :(
ITs for WIndows XP. ANd what do you mean Remote and Local?
Remote would be to install/control a service on another computer on your network and local is your
workstation (xp) that your developing on, so to speak.
Since its NT based and sounds like it is going to be local then I would suggest using the unsupported
ntsvc.ocx control from M$.
I will put together something now.
Be back.
The following link may be of some help:
http://www.smsoft.ru/en/ntservice.htm
Ohh thankz alot RobDog888.
Can you make that demo open notepad.exe as a service thankz.
Here is the demo. It writes to a text file every 5 seconds. It works except for the stop command?
Also, previous instance needs to be added, but the details you can handle.
VB Code:
Option Explicit 'Add Microsoft NT Service Control to your toolbox and add to your form. Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long Private iKount As Integer Private Sub Form_Load() On Error GoTo Err_Load Dim strDisplayName As String Dim bStarted As Boolean NTService1.DisplayName = "RobDog888 - Service Demo" NTService1.ServiceName = "RobDog888 - Service Demo" strDisplayName = NTService1.DisplayName If Command = "-install" Then 'enable interaction with desktop NTService1.Interactive = True If NTService1.Install Then Call NTService1.SaveSetting("Parameters", "TimerInterval", "5000") Call NTService1.SaveSetting("Parameters", "Path", "C:\Test_Service.txt") MsgBox strDisplayName & " installed & started successfully" Else MsgBox strDisplayName & " failed to install" End If Timer1.Interval = CInt(NTService1.GetSetting("Parameters", "TimerInterval", "")) frmMain.Visible = False NTService1.ControlsAccepted = svcCtrlPauseContinue NTService1.StartService ElseIf Command = "-uninstall" Then Timer1.Enabled = False If NTService1.Uninstall Then MsgBox strDisplayName & " uninstalled successfully" Else MsgBox strDisplayName & " failed to uninstall" End If Unload Me End ElseIf Command = "-start" Then NTService1.Interactive = True If NTService1.Running = True Then 'do nothing since already running MsgBox "Already running!", vbOKOnly + vbExclamation, App.ProductName Unload Me End Else 'start it If NTService1.StartService Then Call NTService1.SaveSetting("Parameters", "TimerInterval", "5000") Call NTService1.SaveSetting("Parameters", "Path", "C:\Test_Service.txt") MsgBox strDisplayName & " started successfully" Else MsgBox strDisplayName & " failed to start" End If Timer1.Interval = CInt(NTService1.GetSetting("Parameters", "TimerInterval", "")) Timer1.Enabled = True frmMain.Visible = False NTService1.ControlsAccepted = svcCtrlPauseContinue End If ElseIf Command = "-stop" Then Timer1.Enabled = False NTService1.StopService MsgBox strDisplayName & " stopped successfully" Unload Me End ElseIf Command = "-debug" Then NTService1.Debug = True ElseIf Command <> "" Then MsgBox "Invalid command option" Unload Me End Else MsgBox "Invalid command option" Unload Me End End If Call NTService1.LogEvent(svcMessageInfo, svcEventInformation, "[" & Command & "]") Exit Sub Err_Load: Call NTService1.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description) Unload Me End End Sub Private Sub NTService1_Continue(Success As Boolean) On Error GoTo Err_Continue Timer1.Enabled = True Success = True Call NTService1.LogEvent(svcEventInformation, svcMessageInfo, "Service continued") Exit Sub Err_Continue: Call NTService1.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description) End Sub Private Sub NTService1_Pause(Success As Boolean) On Error GoTo Err_Pause Timer1.Enabled = False Call NTService1.LogEvent(svcEventError, svcMessageError, "Service paused") Success = True Exit Sub Err_Pause: Call NTService1.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description) End Sub Private Sub NTService1_Start(Success As Boolean) On Error GoTo Err_Start Call NTService1.LogEvent(svcEventError, svcMessageError, "Service started") Success = True Exit Sub Err_Start: Call NTService1.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description) End Sub Private Sub NTService1_Stop() On Error GoTo Err_Stop Timer1.Enabled = False End Err_Stop: Call NTService1.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description) End Sub Private Sub Timer1_Timer() On Error GoTo Err_Timer Dim sComputerName As String Dim bSubSuccess As Boolean bSubSuccess = WhatIsComputerName(sComputerName) If bSubSuccess = False Then sComputerName = "Error [" & Err.Number & " - " & Err.Description & "]" Else Open "C:\Test_Service.txt" For Append As #1 Print #1, "Machine name: " & sComputerName & " - " & Right("0000" & CStr(iKount), 4) iKount = iKount + 1 Close #1 End If Exit Sub Err_Timer: Call NTService1.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description) End Sub Private Function WhatIsComputerName(ByRef sName As String) As Boolean On Error GoTo No_Bugs Dim strString As String strString = String(255, Chr$(0)) GetComputerName strString, 255 strString = Left$(strString, InStr(1, strString, Chr$(0))) sName = Left(strString, Len(strString) - 1) WhatIsComputerName = True Exit Function No_Bugs: WhatIsComputerName = False End Function