tags:

views:

142

answers:

2

I call RegisterEventSource() function during my service stop. But it is returning null during system shutdown. Is there any reason for this behavior?

Error: I get error code 0x800706b5 on GetLastError() When googled, I came to know that it has something to do with missing registry data.

Also, how to check if system shutdown has started? (i.e equivalent to HasShutdownStarted() in C#)

+2  A: 

I can only guess why RegisterEventSource would return null during system shutdown as I've never this behavior. [disclaimer: I left my Win32 programming days behind a long time ago, now 100% .NET].

I also couldn't find the error code 0x800706b5 in any .h file I have access to from the latest SDK.

Could it be that the Windows Event Service has shutdown before your service during a system shutdown?

You could try setting up a dependency between your service and the Windows Event Service. This should ensure that during a system shutdown, your service will be shutdown first. Ironically, best advice on how to do this I found was from Adobe: How to create a dependency between Windows services.

Zach Bonham
+3  A: 

Services can specify whether they will be notified when the system shuts down. Call SetServiceStatus() with SERVICE_ACCEPT_SHUTDOWN flag. You will then receive the SERVICE_CONTROL_SHUTDOWN message in your service control message handler.

EDIT: If indeed, as Zach suggests, the problem you are having with RegisterEventSource is because the event service has already shut down, doing your event log writes in response to this notification may fix that problem as well.

ScottTx
+1 I like this solution better than setting up a service dependency, it gives more control. In response to the message you get the chance to stop your service gracefully.
Zach Bonham