AllanF 0 Posted February 14, 2020 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
Angus Robertson 574 Posted February 14, 2020 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
AllanF 0 Posted February 14, 2020 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
Angus Robertson 574 Posted February 14, 2020 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 2 Share this post Link to post
Remy Lebeau 1394 Posted February 14, 2020 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
Angus Robertson 574 Posted February 15, 2020 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
AllanF 0 Posted February 15, 2020 >> 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
THANHNM 0 Posted May 7, 2020 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
Remy Lebeau 1394 Posted May 7, 2020 (edited) 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 May 7, 2020 by Remy Lebeau 2 Share this post Link to post