Windows Service Creation/Installation
I am using some code found here:
http://www.c-sharpcorner.com/Code/2003/Sept/InstallingWinServiceProgrammatically.asp
as a model to install a Windows service. This code seems to mix and match C++ with C# quite a bit.
I am having problems with the point where I get to this section in the InstallService() method:
if (sc_handle.ToInt32() != 0)
{
IntPtr sv_handle = CreateService(sc_handle,svcName,svcDispName,SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,svcPath,null,0,null,null,null);
if(sv_handle.ToInt32() != 0 )
{
CloseServiceHandle(sc_handle);
return false;
}
else
{
//now trying to start the service
int i = StartService(sv_handle,0,null);
... ... ...
I have determined that the sv_handle.ToInt32() must be NOT EQUAL rather than EQUAL as orignially posted in the code in the link above due to return codes given by CreateService().
I get inside the sc_handle.ToInt32() != 0 block just fine. Then the sv_handle.ToInt32() != 0 fails. sv_handle.ToInt32() IS equal to zero. My question is, why require sv_handle to be equal to zero, then use that (a zero value) as the handle to try to start the service?
I can't quite figure this one out. Anyone have any ideas? Much appreciated.