用API函数:CreateService Public Declare Function CreateService Lib "advapi32.dll" Alias "CreateServiceA" ( _ ByVal hSCManager As Long, _ ByVal lpServiceName As String, _ ByVal lpDisplayName As String, _ ByVal dwDesiredAccess As Long, _ ByVal dwServiceType As Long, _ ByVal dwStartType As Long, _ ByVal dwErrorControl As Long, _ ByVal lpBinaryPathName As String, _ ByVal lpLoadOrderGroup As String, _ lpdwTagId As Long, _ ByVal lpDependencies As String, _ ByVal lp As String, ByVal lpPassword As String) As Longc范例: VOID CreateSampleService() { LPCTSTR lpszBinaryPathName = "%SystemRoot%\\system\\testserv.exe";
schService = CreateService( schSCManager, // SCManager database "Sample_Srv", // name of service lpszDisplayName, // service name to display SERVICE_ALL_ACCESS, // desired access SERVICE_WIN32_OWN_PROCESS, // service type SERVICE_DEMAND_START, // start type SERVICE_ERROR_NORMAL, // error control type lpszBinaryPathName, // service's binary NULL, // no load ordering group NULL, // no tag identifier NULL, // no dependencies NULL, // LocalSystem account NULL); // no password
if (schService == NULL) MyErrorExit("CreateService"); else printf("CreateService SUCCESS.\n");
!
Public Declare Function CreateService Lib "advapi32.dll" Alias "CreateServiceA" ( _
ByVal hSCManager As Long, _
ByVal lpServiceName As String, _
ByVal lpDisplayName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwServiceType As Long, _
ByVal dwStartType As Long, _
ByVal dwErrorControl As Long, _
ByVal lpBinaryPathName As String, _
ByVal lpLoadOrderGroup As String, _
lpdwTagId As Long, _
ByVal lpDependencies As String, _
ByVal lp As String, ByVal lpPassword As String) As Longc范例:
VOID CreateSampleService()
{
LPCTSTR lpszBinaryPathName =
"%SystemRoot%\\system\\testserv.exe";
schService = CreateService(
schSCManager, // SCManager database
"Sample_Srv", // name of service
lpszDisplayName, // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_DEMAND_START, // start type
SERVICE_ERROR_NORMAL, // error control type
lpszBinaryPathName, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password
if (schService == NULL)
MyErrorExit("CreateService");
else
printf("CreateService SUCCESS.\n");
CloseServiceHandle(schService);
}
http://www.applevb.com/sourcecode/nt_server.zip
不过微软并不推荐用VB做服务程序