Running Visual Basic Applications as Windows NT Serviceshttp://support.microsoft.com/default.aspx?scid=kb;ZH-cn;175948
Sub Main() Dim hSCManager As Long Dim hService As Long Dim ServiceTableEntry As SERVICE_TABLE_ENTRY Dim b As Boolean Dim cmd As String cmd = Trim(LCase(Command())) Select Case cmd Case "install" 'Install service on machine hSCManager = OpenSCManager(vbNullString, vbNullString, _ SC_MANAGER_CREATE_SERVICE) hService = CreateService(hSCManager, SERVICE_NAME, _ SERVICE_NAME, SERVICE_ALL_ACCESS, _ SERVICE_WIN32_OWN_PROCESS, _ SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, _ App.Path & "\" & App.EXEName, vbNullString, _ vbNullString, vbNullString, vbNullString, _ vbNullString) CloseServiceHandle hService CloseServiceHandle hSCManager Case "uninstall" 'Remove service from machine hSCManager = OpenSCManager(vbNullString, vbNullString, _ SC_MANAGER_CREATE_SERVICE) hService = OpenService(hSCManager, SERVICE_NAME, _ SERVICE_ALL_ACCESS) DeleteService hService CloseServiceHandle hService CloseServiceHandle hSCManager Case Else 'Start the service ServiceTableEntry.lpServiceName = SERVICE_NAME ServiceTableEntry.lpServiceProc = _ FncPtr(AddressOf ServiceMain) b = StartServiceCtrlDispatcher(ServiceTableEntry)
'* if service is not start,then start //add by me If Not b Then hSCManager = OpenSCManager(vbNullString, vbNullString, _ SC_MANAGER_CREATE_SERVICE) hService = OpenService(hSCManager, SERVICE_NAME, 0) StartService hService, 0, 0
你也许会问,为什么需要在Windows9X中让程序做为服务运行?在Windows9X环境中,能象WindowsNT一样做为服务程序运行吗?
回答是肯定的!原因是你可能有一些程序要在登录前就执行,这通常是做为服务器的的机器所需要的,比如web或者mail服务器。由于Windows9X系统缺乏安全性,将程序做为服务运行非常简单,所有需要做的就是修改注册表的相关内容。下面介绍详细步骤:
1、运行regedit,打开注册表。
2、在项目“HKLMSOFTWAREMicrosoft WindowsCurrentVersionRunServices”下添加一个新字符串数值。比如,如果程序的名字叫做“MicroSpud”,就建立一个名为“MicroSpud”的字符串数值,然后在数据域中输入执行程序的完整路径。
建立一个新的字符串数值
输入执行程序的完整路径
设置完毕!重新启动机器后,MicroSpud将在登录前运行。
http://www.dapha.net/down/12/5ivb_2035.asp
Dim hSCManager As Long
Dim hService As Long
Dim ServiceTableEntry As SERVICE_TABLE_ENTRY
Dim b As Boolean
Dim cmd As String cmd = Trim(LCase(Command()))
Select Case cmd
Case "install" 'Install service on machine
hSCManager = OpenSCManager(vbNullString, vbNullString, _
SC_MANAGER_CREATE_SERVICE)
hService = CreateService(hSCManager, SERVICE_NAME, _
SERVICE_NAME, SERVICE_ALL_ACCESS, _
SERVICE_WIN32_OWN_PROCESS, _
SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, _
App.Path & "\" & App.EXEName, vbNullString, _
vbNullString, vbNullString, vbNullString, _
vbNullString)
CloseServiceHandle hService
CloseServiceHandle hSCManager
Case "uninstall" 'Remove service from machine
hSCManager = OpenSCManager(vbNullString, vbNullString, _
SC_MANAGER_CREATE_SERVICE)
hService = OpenService(hSCManager, SERVICE_NAME, _
SERVICE_ALL_ACCESS)
DeleteService hService
CloseServiceHandle hService
CloseServiceHandle hSCManager
Case Else 'Start the service
ServiceTableEntry.lpServiceName = SERVICE_NAME
ServiceTableEntry.lpServiceProc = _
FncPtr(AddressOf ServiceMain)
b = StartServiceCtrlDispatcher(ServiceTableEntry)
'* if service is not start,then start //add by me
If Not b Then
hSCManager = OpenSCManager(vbNullString, vbNullString, _
SC_MANAGER_CREATE_SERVICE)
hService = OpenService(hSCManager, SERVICE_NAME, 0)
StartService hService, 0, 0
End If
End Select
End Sub