Jump to content
AllanF

In my Service I want to to receive notification when the system is suspended or resumed

Recommended Posts

In my Service I want to to receive notification when the system is suspended or resumed (Hibernate/Sleep) so that I can Close Active Sockets connections.

Similar questions have got the answer 'the service must use a HandlerEx() callback via RegisterServiceCtrlHandlerEx()' but I have not been able to use it in Delphi.

Need more assistance please.

Share this post


Link to post

HandlexEx is supported by the DDService replacement for the Delphi version, written by Arno Garrels. 

 

You can also intercept WMPowerBroadcast messages which is sent at various stages before and after suspend and works fine in a service.  I have a THardwareEvents component I've never got around to releasing due to lack of documentation.

 

Angus

 

 

 

Share this post


Link to post

I have already tried the WM_POWERBROADCAST for GUID_MONITOR_POWER_ON with this I get notified even if display is begin turned off.

Share this post


Link to post

Don't think monitor power off will really help you, you need to check PowerEvents like PBT_APMQUERYSUSPEND and PBT_APMRESUMESUSPEND.  I'll email my unit that does all this.

 

Angus

 

  • Like 2

Share this post


Link to post
9 hours ago, Angus Robertson said:

HandlexEx is supported by the DDService replacement for the Delphi version, written by Arno Garrels. 

Same with SvCom, written by Aldyn Software (this is the one I use).  In fact, there are quite a few 3rd party TService replacements available.  Or, you could simply make a copy of SvcMgr.pas and patch TService yourself.

 

Share this post


Link to post

Yes, I've been using SvCom for 15 years, but it is commercial software.  DDService actually patches your local copy of TService, while SvCom borrowed it. 

 

Angus

 

Share this post


Link to post

>> I'll email my unit that does all this.

 

I looked at all the options suggested by Angus and Remy. Then I implemented the emailed Pas files and tested it with all possible options I could manage. Was most simple to include in my program and works perfectly fine. Very grateful to both for pulling me out of this one too. 

 

Share this post


Link to post

So Is that mean I can't simply call RegisterServiceCtrlHandlerEx from Delphi? 
And does windows 10 fast-startup throws those events too? Because I need to stop my service when Hibernate/Shutdown and resume it on after Waked-up/Startup. 

I'm trying this:

function MySvcHandlerFunctionEx(dwControl, dwEventType: DWORD; lpEventData, lpContext: Pointer): DWORD; stdcall;
var _lpContext: PMyDefine;
    F: TextFile;
begin
  AssignFile(F, 'C:\Test.txt');
  try
    if FileExists('C:\Test.txt') then
      Append(F)
    else
      Rewrite(F);
    WriteLn(F, 'Got Data: ' + inttostr(dwControl));

    if dwEventType = WM_POWERBROADCAST then
    begin
      _lpContext := PMyDefine(lpContext);

      WriteLn(F, 'MySvcHandler: ' + IntToStr(dwControl) + ' - ' + _lpContext.s);
    end;
  finally
    Result := NO_ERROR;
    CloseFile(F);
  end;
end;

procedure TCMCRTEngine.ServiceStart(Sender: TService; var Started: Boolean);
var lpContext: PMyDefine;
begin
  ...
  New(lpContext);
  lpContext.s := 'My_Test_Svc';
  SvcHandler := RegisterServiceCtrlHandlerEx(PWideChar('MyService'), @MySvcHandlerFunctionEx, lpContext);
end;

And I didn't receive anything yet

Share this post


Link to post
8 hours ago, THANHNM said:

So Is that mean I can't simply call RegisterServiceCtrlHandlerEx from Delphi?

You can, but not the way you are thinking.  If you call RegisterServiceCtrlHandler/Ex() manually, then you can't use any pre-existing service framework (TService, DDService, SvCom, etc), as they make their own call to RegisterServiceCtrlHandler/Ex() for you.  You would have to make ALL of the SCM-related API calls manually, starting with StartServiceCtrlDispatcher() and implementing ServiceMain() to call RegisterServiceCtrlHandler/Ex().  Read MSDN for more details: Service Program Tasks

Quote

And does windows 10 fast-startup throws those events too?

I'm not sure.  I've seen conflicting info on that.  Some people say yes, some say no.  I haven't tried it myself to check.

Quote

I need to stop my service when Hibernate/Shutdown and resume it on after Waked-up/Startup. 

How can I Execute a Function from service program when Windows Fast Startup

How to detect wake up from sleep mode in windows service?

Detect resume from hibernate in a windows service

Detecting Hibernation / Standby and resume in a Service

Chapter 6: OnNow/ACPI Support

Quote

I'm trying this:

That code will not work:

 

- a service's Handler/Ex() cannot call RegisterServiceCtrlHandler/Ex(), because it has already been called before the OnStart event is called.

 

- WM_POWERBROADCAST is a window message, not a service notification.  A service would need to handle SERVICE_CONTROL_POWEREVENT notifications instead.

Edited by Remy Lebeau
  • Thanks 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×