NT service creation
Hello,
I would like to create a NT service using the APIs. I have followed the MSDN
advices to write my project but it doesn't work. For exemple, the
CreateService function always returns 0 (fail) while GetLastError gives no
error.
Furthermore, I notice that if I call CreateService with a null SCManager
handle the result is the same. GetLastError returns 0 (ERROR_SUCCESS)
instead of returning ERROR_INVALID_HANDLE.
Do you know why?
Here is an extract of my project:
Private 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
...
Dim NullString as String
hSCM = OpenSCManager(NullString,
SERVICES_ACTIVE_DATABASE,
SC_MANAGER_ALL_ACCESS)
If (hSCM = 0) Then
MsgBox "OpenSCManager error " & GetLastError()
Exit Sub
End If
schService = CreateService(hSCM, _
"MyService", _
"My Service", _
SERVICE_ALL_ACCESS, _
SERVICE_WIN32_OWN_PROCESS Or
SERVICE_INTERACTIVE_PROCESS, _
SERVICE_DEMAND_START, _
SERVICE_ERROR_NORMAL, _
PathName, _
NullString, _
0, _
NullString, _
NullString, _
NullString)
If (schService = 0) Then
MsgBox "CreateService Error " & GetLastError()
Else
MsgBox "CreateService Success"
End If
CloseServiceHandle schService
CloseServiceHandle hSCM
Thank you,
Vronique
[1833 byte] By [
Vronique] at [2007-11-10 0:19:50]

# 1 Re: NT service creation
Vronique,
>I would like to create a NT service using the APIs. I have followed the MSDN
>advices to write my project but it doesn't work. For exemple, the
>CreateService function always returns 0 (fail) while GetLastError gives no
>error.
GetLastError() will always return 0. That's because VB calls it after
each API call in order to stuff the return into the Err object
Check Err.LastDllError instead. That has the real return value from
GetLastError().
Oh, and NullString is invalid. Use vbNullString instead.
Oh (finally), and your best resource for doing NT services in VB is
L.J. Johnson:
<http://www.flash.net/~ljjohnsn>
Ciao, Craig
# 2 Re: NT service creation
Vronique,
>I would like to create a NT service using the APIs. I have followed the MSDN
>advices to write my project but it doesn't work. For exemple, the
>CreateService function always returns 0 (fail) while GetLastError gives no
>error.
GetLastError() will always return 0. That's because VB calls it after
each API call in order to stuff the return into the Err object
Check Err.LastDllError instead. That has the real return value from
GetLastError().
Oh, and NullString is invalid. Use vbNullString instead.
Oh (finally), and your best resource for doing NT services in VB is
L.J. Johnson:
<http://www.flash.net/~ljjohnsn>
Ciao, Craig
# 3 Re: NT service creation
If you haven't already, check out Desaware's products:
http://www.desaware.com
I've found their services tools to be excellent.
Best,
Pete Y.
Vronique <vrossi@ceitel.fr> wrote in message news:38cfbef2@news.dev-archive.com...
> Hello,
>
> I would like to create a NT service using the APIs. I have followed the
MSDN
> advices to write my project but it doesn't work. For exemple, the
> CreateService function always returns 0 (fail) while GetLastError gives no
> error.
> Furthermore, I notice that if I call CreateService with a null SCManager
> handle the result is the same. GetLastError returns 0 (ERROR_SUCCESS)
> instead of returning ERROR_INVALID_HANDLE.
>
> Do you know why?
>
> Here is an extract of my project:
>
> Private 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
>
> ...
> Dim NullString as String
>
> hSCM = OpenSCManager(NullString,
> SERVICES_ACTIVE_DATABASE,
> SC_MANAGER_ALL_ACCESS)
> If (hSCM = 0) Then
> MsgBox "OpenSCManager error " & GetLastError()
> Exit Sub
> End If
>
> schService = CreateService(hSCM, _
> "MyService", _
> "My Service", _
> SERVICE_ALL_ACCESS, _
> SERVICE_WIN32_OWN_PROCESS Or
> SERVICE_INTERACTIVE_PROCESS, _
> SERVICE_DEMAND_START, _
> SERVICE_ERROR_NORMAL, _
> PathName, _
> NullString, _
> 0, _
> NullString, _
> NullString, _
> NullString)
>
> If (schService = 0) Then
> MsgBox "CreateService Error " & GetLastError()
> Else
> MsgBox "CreateService Success"
> End If
>
> CloseServiceHandle schService
> CloseServiceHandle hSCM
>
> Thank you,
>
> Vronique
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
# 4 Re: NT service creation
If you haven't already, check out Desaware's products:
http://www.desaware.com
I've found their services tools to be excellent.
Best,
Pete Y.
Vronique <vrossi@ceitel.fr> wrote in message news:38cfbef2@news.dev-archive.com...
> Hello,
>
> I would like to create a NT service using the APIs. I have followed the
MSDN
> advices to write my project but it doesn't work. For exemple, the
> CreateService function always returns 0 (fail) while GetLastError gives no
> error.
> Furthermore, I notice that if I call CreateService with a null SCManager
> handle the result is the same. GetLastError returns 0 (ERROR_SUCCESS)
> instead of returning ERROR_INVALID_HANDLE.
>
> Do you know why?
>
> Here is an extract of my project:
>
> Private 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
>
> ...
> Dim NullString as String
>
> hSCM = OpenSCManager(NullString,
> SERVICES_ACTIVE_DATABASE,
> SC_MANAGER_ALL_ACCESS)
> If (hSCM = 0) Then
> MsgBox "OpenSCManager error " & GetLastError()
> Exit Sub
> End If
>
> schService = CreateService(hSCM, _
> "MyService", _
> "My Service", _
> SERVICE_ALL_ACCESS, _
> SERVICE_WIN32_OWN_PROCESS Or
> SERVICE_INTERACTIVE_PROCESS, _
> SERVICE_DEMAND_START, _
> SERVICE_ERROR_NORMAL, _
> PathName, _
> NullString, _
> 0, _
> NullString, _
> NullString, _
> NullString)
>
> If (schService = 0) Then
> MsgBox "CreateService Error " & GetLastError()
> Else
> MsgBox "CreateService Success"
> End If
>
> CloseServiceHandle schService
> CloseServiceHandle hSCM
>
> Thank you,
>
> Vronique
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
# 5 Re: NT service creation
I successfully used NT service OCX that Microsoft provides for free, be aware
though that there's no support for it. It comes with C source and you have
to compile it yourself. I saw a sample project of VB service with modified
NT service OCX somewhere, can't remember, maybe Desaware.
I tried to use Win APIs to create service in VB 6. I get to install and unistall
but couldn't start it. It will GPF when started - something to do with how
VB 6 handles threading.
Sergey.
"Peter Young" <pyoung@mr.net> wrote:
>If you haven't already, check out Desaware's products:
>http://www.desaware.com
>
>I've found their services tools to be excellent.
>
>Best,
>
>Pete Y.
>
>Vronique <vrossi@ceitel.fr> wrote in message news:38cfbef2@news.dev-archive.com...
>> Hello,
>>
>> I would like to create a NT service using the APIs. I have followed the
>MSDN
>> advices to write my project but it doesn't work. For exemple, the
>> CreateService function always returns 0 (fail) while GetLastError gives
no
>> error.
>> Furthermore, I notice that if I call CreateService with a null SCManager
>> handle the result is the same. GetLastError returns 0 (ERROR_SUCCESS)
>> instead of returning ERROR_INVALID_HANDLE.
>>
>> Do you know why?
>>
>> Here is an extract of my project:
>>
>> Private 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
>>
>> ...
>> Dim NullString as String
>>
>> hSCM = OpenSCManager(NullString,
>> SERVICES_ACTIVE_DATABASE,
>> SC_MANAGER_ALL_ACCESS)
>> If (hSCM = 0) Then
>> MsgBox "OpenSCManager error " & GetLastError()
>> Exit Sub
>> End If
>>
>> schService = CreateService(hSCM, _
>> "MyService", _
>> "My Service", _
>> SERVICE_ALL_ACCESS, _
>> SERVICE_WIN32_OWN_PROCESS Or
>> SERVICE_INTERACTIVE_PROCESS, _
>> SERVICE_DEMAND_START, _
>> SERVICE_ERROR_NORMAL, _
>> PathName, _
>> NullString, _
>> 0, _
>> NullString, _
>> NullString, _
>> NullString)
>>
>> If (schService = 0) Then
>> MsgBox "CreateService Error " & GetLastError()
>> Else
>> MsgBox "CreateService Success"
>> End If
>>
>> CloseServiceHandle schService
>> CloseServiceHandle hSCM
>>
>> Thank you,
>>
>> Vronique
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>
>
sergey at 2007-11-11 20:08:39 >

# 6 Re: NT service creation
I successfully used NT service OCX that Microsoft provides for free, be aware
though that there's no support for it. It comes with C source and you have
to compile it yourself. I saw a sample project of VB service with modified
NT service OCX somewhere, can't remember, maybe Desaware.
I tried to use Win APIs to create service in VB 6. I get to install and unistall
but couldn't start it. It will GPF when started - something to do with how
VB 6 handles threading.
Sergey.
"Peter Young" <pyoung@mr.net> wrote:
>If you haven't already, check out Desaware's products:
>http://www.desaware.com
>
>I've found their services tools to be excellent.
>
>Best,
>
>Pete Y.
>
>Vronique <vrossi@ceitel.fr> wrote in message news:38cfbef2@news.dev-archive.com...
>> Hello,
>>
>> I would like to create a NT service using the APIs. I have followed the
>MSDN
>> advices to write my project but it doesn't work. For exemple, the
>> CreateService function always returns 0 (fail) while GetLastError gives
no
>> error.
>> Furthermore, I notice that if I call CreateService with a null SCManager
>> handle the result is the same. GetLastError returns 0 (ERROR_SUCCESS)
>> instead of returning ERROR_INVALID_HANDLE.
>>
>> Do you know why?
>>
>> Here is an extract of my project:
>>
>> Private 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
>>
>> ...
>> Dim NullString as String
>>
>> hSCM = OpenSCManager(NullString,
>> SERVICES_ACTIVE_DATABASE,
>> SC_MANAGER_ALL_ACCESS)
>> If (hSCM = 0) Then
>> MsgBox "OpenSCManager error " & GetLastError()
>> Exit Sub
>> End If
>>
>> schService = CreateService(hSCM, _
>> "MyService", _
>> "My Service", _
>> SERVICE_ALL_ACCESS, _
>> SERVICE_WIN32_OWN_PROCESS Or
>> SERVICE_INTERACTIVE_PROCESS, _
>> SERVICE_DEMAND_START, _
>> SERVICE_ERROR_NORMAL, _
>> PathName, _
>> NullString, _
>> 0, _
>> NullString, _
>> NullString, _
>> NullString)
>>
>> If (schService = 0) Then
>> MsgBox "CreateService Error " & GetLastError()
>> Else
>> MsgBox "CreateService Success"
>> End If
>>
>> CloseServiceHandle schService
>> CloseServiceHandle hSCM
>>
>> Thank you,
>>
>> Vronique
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>
>
sergey at 2007-11-11 20:09:38 >

# 7 Re: NT service creation
Sergey,
> I tried to use Win APIs to create service in VB 6. I get to install and
uninstall
> but couldn't start it. It will GPF when started - something to do with how
> VB 6 handles threading.
That is correct, it will (although you can do it well in VB 5.0, but 6.0
changed the rules). Basically, you *can* do it with the API but you will
need to move *all* the API calls to a type library and turn off the
automatic error returns.
--
L.J. Johnson, Slightly Tilted Software
Microsoft MVP (Visual Basic)
LJJohnsn@Flash.Net or LJJohnson@mvps.org
<http://www.flash.net/~ljjohnsn>
Ask The NT Pro at <http://www.inquiry.com>
# 8 Re: NT service creation
Sergey,
> I tried to use Win APIs to create service in VB 6. I get to install and
uninstall
> but couldn't start it. It will GPF when started - something to do with how
> VB 6 handles threading.
That is correct, it will (although you can do it well in VB 5.0, but 6.0
changed the rules). Basically, you *can* do it with the API but you will
need to move *all* the API calls to a type library and turn off the
automatic error returns.
--
L.J. Johnson, Slightly Tilted Software
Microsoft MVP (Visual Basic)
LJJohnsn@Flash.Net or LJJohnson@mvps.org
<http://www.flash.net/~ljjohnsn>
Ask The NT Pro at <http://www.inquiry.com>
# 9 Re: NT service creation
Actually, you can work around THAT problem to, is you th same extreme means
that one uses to get multithreading to work properly. :-)
--
?MichKa
(insensitive fruitarian)
random junk of dubious value, a multilingual website, the
54-language TSI Form/Report to Data Access Page Wizard,
and lots of replication "stuff" at the (no scripts required!)
http://www.trigeminal.com/
?
"L.J. Johnson" <ljjohnsn@flash.net> wrote in message
news:38d3ee23$1@news.dev-archive.com...
> Sergey,
>
> > I tried to use Win APIs to create service in VB 6. I get to install and
> uninstall
> > but couldn't start it. It will GPF when started - something to do with
how
> > VB 6 handles threading.
>
> That is correct, it will (although you can do it well in VB 5.0, but 6.0
> changed the rules). Basically, you *can* do it with the API but you will
> need to move *all* the API calls to a type library and turn off the
> automatic error returns.
>
> --
> L.J. Johnson, Slightly Tilted Software
> Microsoft MVP (Visual Basic)
> LJJohnsn@Flash.Net or LJJohnson@mvps.org
> <http://www.flash.net/~ljjohnsn>
> Ask The NT Pro at <http://www.inquiry.com>
>
>
>
# 10 Re: NT service creation
Actually, you can work around THAT problem to, is you th same extreme means
that one uses to get multithreading to work properly. :-)
--
?MichKa
(insensitive fruitarian)
random junk of dubious value, a multilingual website, the
54-language TSI Form/Report to Data Access Page Wizard,
and lots of replication "stuff" at the (no scripts required!)
http://www.trigeminal.com/
?
"L.J. Johnson" <ljjohnsn@flash.net> wrote in message
news:38d3ee23$1@news.dev-archive.com...
> Sergey,
>
> > I tried to use Win APIs to create service in VB 6. I get to install and
> uninstall
> > but couldn't start it. It will GPF when started - something to do with
how
> > VB 6 handles threading.
>
> That is correct, it will (although you can do it well in VB 5.0, but 6.0
> changed the rules). Basically, you *can* do it with the API but you will
> need to move *all* the API calls to a type library and turn off the
> automatic error returns.
>
> --
> L.J. Johnson, Slightly Tilted Software
> Microsoft MVP (Visual Basic)
> LJJohnsn@Flash.Net or LJJohnson@mvps.org
> <http://www.flash.net/~ljjohnsn>
> Ask The NT Pro at <http://www.inquiry.com>
>
>
>
# 11 Re: NT service creation
Michael,
> Actually, you can work around THAT problem to, is you th same extreme
means
> that one uses to get multithreading to work properly. :-)
In either case, there is such a thing as being too *much* of a hassle to
deal with. I was one of the original "vb can do anything" people, but some
things just aren't worth the work when you can buy a solution. So, now if I
want to create a service, I'll either use VB 5.0 native (for simple ones) or
use Spyworks (even without their new, super-duper libraries). Or, in about a
year (presumable), I'll be able to do it natively in VB 7.0.
--
L.J. Johnson, Slightly Tilted Software
Microsoft MVP (Visual Basic)
LJJohnsn@Flash.Net or LJJohnson@mvps.org
<http://www.flash.net/~ljjohnsn>
Ask The NT Pro at <http://www.inquiry.com>
# 12 Re: NT service creation
Michael,
> Actually, you can work around THAT problem to, is you th same extreme
means
> that one uses to get multithreading to work properly. :-)
In either case, there is such a thing as being too *much* of a hassle to
deal with. I was one of the original "vb can do anything" people, but some
things just aren't worth the work when you can buy a solution. So, now if I
want to create a service, I'll either use VB 5.0 native (for simple ones) or
use Spyworks (even without their new, super-duper libraries). Or, in about a
year (presumable), I'll be able to do it natively in VB 7.0.
--
L.J. Johnson, Slightly Tilted Software
Microsoft MVP (Visual Basic)
LJJohnsn@Flash.Net or LJJohnson@mvps.org
<http://www.flash.net/~ljjohnsn>
Ask The NT Pro at <http://www.inquiry.com>