NT Service - problem in re-registering

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Guest
Archived from groups: microsoft.public.windowsnt.misc (More info?)

I am trying to develop an NT Service, novice in the field and running
into following problems.
Let me simplify all the steps to explain problem i am facing, please
let me know if i am doing anything wrong

I create a NT Service using Visual Studio 6 ATL Com wizard without a
single line of my own code added here. Only thing i change is remove
command from Project/Settings/Custom Build that re-registers the
service. After that I compile the project. Let say name of the service
is MyService. I use following commands to register and unregister
service.

MyService -Service
... start and stop the service
MyService -Unregserver

after this unregserver command this service goes in "Diabled" mode as
shown in SCM and now if i want to re-register the service i can
"Service cannot be deleted" message box. To only way to register the
service again is to logoff the box.

Since I haven't added any of my code in this test project, there
shouldn't be case of any dangling handle.
Any ideas?
 
Archived from groups: microsoft.public.windowsnt.misc (More info?)

imakimak@hotmail.com (i love snmp) wrote in message news:<55be6b73.0405071303.51a7f8db@posting.google.com>...
> I am trying to develop an NT Service, novice in the field and running
> into following problems.
> Let me simplify all the steps to explain problem i am facing, please
> let me know if i am doing anything wrong
>
> I create a NT Service using Visual Studio 6 ATL Com wizard without a
> single line of my own code added here. Only thing i change is remove
> command from Project/Settings/Custom Build that re-registers the
> service. After that I compile the project. Let say name of the service
> is MyService. I use following commands to register and unregister
> service.
>
> MyService -Service
> .. start and stop the service
> MyService -Unregserver
>
> after this unregserver command this service goes in "Diabled" mode as
> shown in SCM and now if i want to re-register the service i can
> "Service cannot be deleted" message box. To only way to register the
> service again is to logoff the box.
>
> Since I haven't added any of my code in this test project, there
> shouldn't be case of any dangling handle.
> Any ideas?

A service must be stopped in order to uninstall it successfully.
But the code generated by ATL Com wizard does not check if the service
is stopped before uninstalling it!

You need to modify the code generated to be sure that the service is
stopped before uninstalling it. See the code below ...

inline BOOL CServiceModule::Uninstall()
{
if (!IsInstalled())
return TRUE;

SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL,
SC_MANAGER_ALL_ACCESS);

if (hSCM == NULL)
{
MessageBox(NULL, _T("Couldn't open service manager"),
m_szServiceName, MB_OK);
return FALSE;
}

SC_HANDLE hService = ::OpenService(hSCM, m_szServiceName,
SERVICE_STOP | DELETE);

if (hService == NULL)
{
::CloseServiceHandle(hSCM);
MessageBox(NULL, _T("Couldn't open service"), m_szServiceName,
MB_OK);
return FALSE;
}
SERVICE_STATUS status;
::ControlService(hService, SERVICE_CONTROL_STOP, &status);

// Wait for the service to stop
//////////////////////////////////////////
while (QueryServiceStatus(hService, &status) &&
(status.dwCurrentState != SERVICE_STOPPED))
Sleep(1000);
///////////////////////////////////////////////////////////////////////////

BOOL bDelete = :😀eleteService(hService);
::CloseServiceHandle(hService);
::CloseServiceHandle(hSCM);

if (bDelete)
return TRUE;

MessageBox(NULL, _T("Service could not be deleted"),
m_szServiceName, MB_OK);
return FALSE;
}

Hope it will help you,
Regards
Vincent
http://www.activeplus.com