No need to trigger WM_QUERYENDSESSION yourself, Delphi already does it in Forms.pas:
procedure TCustomForm.WMQueryEndSession(var Message: TWMQueryEndSession);
begin
Message.Result := Integer(CloseQuery);
end;
So, do your actions in standard OnCloseQuery.
Note. In my Windows 10, on shutdown I receive OnCloseQuery. but not OnClose (WM_CLOSE).