Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CreateService in Visual Basic

Status
Not open for further replies.

Mountie

Programmer
Oct 23, 2001
3
US
I am trying to install and run a service using Visual Basic 6. I receive a handle to the SCM after executing the function OpenSCManager. I then try to install the service by using the function CreateService. At this point the program returns a 0 to indicate that it has an error and has not returned a handle. The code at that point is as follows:

schService = CreateService( _
schOpenSCM, _
lpServiceName, _
lpDisplayName, _
SERVICE_ALL_ACCESS, _
SERVICE_WIN32_OWN_PROCESS, _
SERVICE_AUTO_START, _
SERVICE_ERROR_IGNORE, _
lpszBinaryPathName, _
vbNullString, _
vbNull, _
vbNullString, _
vbNullString, _
vbNullString)

I receive the error message from Err.LastDllError of 87.

Any ideas?

Mountie
 
WinError.h lists 87 as being a Invalid Parameter.

Are you passing the strings as ByVal?

Chip H.
 
Below is the Function I call from the API. This I got from Microsoft's web site, from the API Viewer in Visual Basic and from Dan Appleman's book.

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 Long


In the previous message of what is in my own program, the following are the values which I pass for the variables.

schOpenSCM = a long value which is the handle to the SCM, and this looks okay in the Visual Basic Watch Window

lpServiceName = "SecureScreen"
lpDisplayName = "SecureScreen"
lpszBinaryPathName = "c:\\winnt\\system32\\securesc.exe"
 
Why are you doubling up the back-slashes in your path? This is VB, not C.

Chip H.
 
The following is my function in VB. The whole thing...

Dim lpszBinaryPathName As String, lpServiceName As String, _
lpDisplayName As String
Dim schService As Long ' Handle to CreateService function
Dim schOpenSCM As Long ' Handle to Service Control Manager

lpszBinaryPathName = "C:\WINNT\system32\securesc.exe"
lpServiceName = "SecureScreen"
lpDisplayName = "SecureScreen"

'// Open a handle to the SC Manager database.
schOpenSCM = OpenSCManager( _
vbNullString, _
vbNullString, _
SC_MANAGER_ALL_ACCESS)

schService = CreateService( _
schOpenSCM, _
lpServiceName, _
lpDisplayName, _
SERVICE_ALL_ACCESS, _
SERVICE_WIN32_OWN_PROCESS, _
SERVICE_AUTO_START, _
SERVICE_ERROR_IGNORE, _
lpszBinaryPathName, _
vbNullString, _
vbNull, _
vbNullString, _
vbNullString, _
vbNullString)

If schService = 0 Then
MsgBox "Error in installing Service!" & vbCrLf _
& "Error: " & Err.LastDllError & vbCrLf, _
vbOKOnly, _
"SecureScreen Service"
Else
MsgBox "SecureScreen Service installed.", vbOKOnly, _
"SecureScreen Service"
End If
Call CloseServiceHandle(schService)
Call CloseServiceHandle(schOpenSCM)

-------------------------------------------------------

I have defined the constants I use in the CreateService function as thus:

Public Const SERVICE_ALL_ACCESS As Long = _
(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)

Public Const SERVICE_WIN32_OWN_PROCESS As Long = &H10
Public Const SERVICE_AUTO_START As Long = &H2
Public Const SERVICE_ERROR_IGNORE As Long = 0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top