Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 674
  • Last Modified:

Delphi Application Shutdown Problems

I have written a delphi 4 application and am having problems when the user chooses to ShutDown Windows.  When the Win95 shutdown/restart process is called, my application regains focus and Windows ceases to shutdown.  Any ideas?
0
PaulKorzycki
Asked:
PaulKorzycki
  • 5
  • 5
  • 4
  • +3
1 Solution
 
ZifNabCommented:
Why don't you shut-down your application?
0
 
RomanianCommented:
Is this console application? It's like FAR in Win95. I think Win95 don't send WM_QUIT to console application and I think you must hook WM_SHUTDOWN or WM_QUIT(I don't remember which one). Try to do it.
0
 
PaulKorzyckiAuthor Commented:
The windows shutdown process stops completely (ie, I can continue to work under windows).

The reason for not shutting down manually is that the application is running in the System Tray, and is a "background" application.


0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
PaulKorzyckiAuthor Commented:
Romanian, my app is not a console app, and has all of the neccessary forms.  On FormClose I do some clean up of files, but even when I remove that, it still does not work.  I can quit manually with no problems.  I am thinking that OnCloseQuery is not being called

Could this be a problem with a shareware component (I am using a serial communications component, a TrayIcon component, and a delay (sleeper) component.)

0
 
ZifNabCommented:
do you close your serial connection? And how does the delay component work?
0
 
viktornetCommented:
Hello!

The problem is I guess in the way you allow your app to close. I think you've set the CanClose := False; under the OnCloseQuery() but that's not what you should do. YOu need to use the following. You need to set Action := caNone; in the OnClose() of the form. Hope this helps!

Regards,
Viktor Ivanov
0
 
PaulKorzyckiAuthor Commented:
OnCloseQuery does not contain any code (and I've tried setting canClose:=True....to no avail).  When I put Action:=caNone into the OnClose section of the form, I can't close it at all!
0
 
ZifNabCommented:
It seems that my comments don't exist here? Does anybody even care of my comments?
0
 
PaulKorzyckiAuthor Commented:
Sorry ZifNab:
Yes I close the serial connection using the routine in the component.  The sleeper component relies on the system timer (it is basically to put in delays in the program code).

I think it may be a problem with one of these components
0
 
AswCommented:
Try this code in the OnClose event  it may work.

PostMessage(FindWindow(Nil, 'My App'), WM_QUIT, 0, 0);
0
 
MadshiCommented:
Paul,

my suggestion:
Try to write a handler for "Application.OnMessage". For this purpose you'll have to add a new method to your main form and in "mainForm.Create" please add "Application.OnMessage:=MyAppMessageProc". In this MyAppMessageProc answer the WM_QUERYENDSESSION message. So none of your components are able to deny this message.

What Asw suggested probably won't work, since if one of your components deny the WM_QUERYENDSESSION message, the FormClose method isn't called at all. But if I'm wrong in this point you should call PostQuitMessage instead of using PostMessage(...).

Regards, Madshi.
0
 
RomanianCommented:
I've found solution!!!
You must change next code in TTrayIcon component!
----- start ---
procedure TTrayIcon.WndProc(var msg : TMessage);
var MouseCo: Tpoint;
begin
   with msg do
      if (msg = WM_RESETTOOLTIP) then
         SetToolTip( fToolTip )
      else if (msg = WM_TOOLTRAYICON) then begin
         case lParam of
            WM_LBUTTONDBLCLK   : if assigned (FOnDblClick) then FOnDblClick(self);
            WM_LBUTTONUP       : if assigned(FOnClick)then FOnClick(self);
            WM_RBUTTONUP       : if assigned (FOnRightClick)then
                                 begin
                                   GetCursorPos(MouseCo);
                                   FOnRightClick(self,mbRight,[],MouseCo.x,MouseCo.y);
                                 end;
         end;
      end
{<<<<<<<<< ADDED>>>>>>>>>}
else if (msg = WM_QUERYENDSESSION) then
                 Result := Longint(True)
               else if (msg = WM_ENDSESSION) then
                 Application.Terminate;
{<<<<<<<<< ADDED>>>>>>>>>}

end;
---Finish---
Windows with Active = false don't receive messages like WM_QUERYENDSESSION and WM_QUERYENDSESSION. So you must hadle this messages in TTrayIcon.WndProc.
Try it.
I've tested under WinNT my computer was shutdowned without any problem.

0
 
RomanianCommented:
To Madshi:
If MainWindow invisible we can't receive messages by this window and code writed to handle onMessage don't work
0
 
MadshiCommented:
Romanian,

you're right. I didn't know that the main window is invisible. And handling OnMessage really doesn't work...  :-(
But in your answer there's a little bug, too. You should terminate the program only when WM_ENDSESSION comes with "wParam=true"!

Regards, Madshi.
0
 
RomanianCommented:
To Madishi:

Yes, it's a little bug:) Probably I must handle not only wParam, but lParam too. So application could work if you logoff but don't shutdown computer.

I think WndProc must call DefWndProc for any other messages which not handled in this procedure. And what do you think about this?
0
 
MadshiCommented:
Romanian,

I think, it doesn't matter whether windows logs off or shuts down, because all applications are closed in both cases.
Why calling DefWndProc? Can't you just call "inherited" for messages that are not handled?

Regards, Madshi.
0
 
RomanianCommented:
To Madshi:

But if I use WinNT and I need that my application work when nobody is logged in. Or I must do this via NT services?
0
 
MadshiCommented:
Romanian,

oops... don't know that. I am working with win95 only. But I know that if you're using NT, the logoff parameter of the WM(QUERY)ENDSESSION messages is always 0. That means you can't determine, whether windows logs off or shuts down.    :-(

Regards, Madshi.
0
 
PaulKorzyckiAuthor Commented:
Romanian,  you were right (partly!)

The trayicon component seemed to work fine, but the communications component was the culpret.  I inserted your piece of code into the WndProc portion of the communications component, and it works now!

Good Work!!!
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 5
  • 5
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now