🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
7 messages
7 total messages Started by Marc Thu, 15 Nov 2007 23:43
TTrayIcon and Shutdown (one step later)
#1048
Author: Marc
Date: Thu, 15 Nov 2007 23:43
35 lines
2070 bytes
Hi all

I read a lot about the TTrayIcon component and its problems with detecting 
the WM_ENDSESSION with formless applications.

So I took the source and modified the code in order to call an event in the 
application in addition to simply call Shell_NotifyIcon(NIM_DELETE, &FData).

void __fastcall TMyTrayIcon::EndSession()
{
   OnEndSession(this);
   Shell_NotifyIcon(NIM_DELETE, &FData);
}
//---------------------------------------------------------------------------

In the OnEndSession event my application then does some cleanup and calls
Application->Terminate();

When running the application and log off, the WM_ENDSESSION is caught by 
TMyTrayIcon, the OnEndSession event is fired and my application shuts down 
correctly.

But Windows does not continue with the shutdown...

Does anyone have an idea why or how to get Windows to complete the shut down 
(logoff)?

This problem bugs me now for quite a while. I have tried alternatives like 
the ShutDownWatcher but without success :-(
Does anyone has a version of TTrayIcon that allows to define a shut down 
event?


Rds
Marc
Re: TTrayIcon and Shutdown (one step later)
#1049
Author: JohnC
Date: Thu, 15 Nov 2007 23:51
52 lines
2463 bytes
Is this what you are looking for?

   // Shut down the system and force all applications to close.

   if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
      return FALSE;

   return TRUE;

John

"Marc" <info@mawnet.com> wrote in message 
news:473cd9cf$1@newsgroups.borland.com...
> Hi all
>
> I read a lot about the TTrayIcon component and its problems with detecting 
> the WM_ENDSESSION with formless applications.
>
> So I took the source and modified the code in order to call an event in 
> the application in addition to simply call Shell_NotifyIcon(NIM_DELETE, 
> &FData).
>
> void __fastcall TMyTrayIcon::EndSession()
> {
>   OnEndSession(this);
>   Shell_NotifyIcon(NIM_DELETE, &FData);
> }
> //---------------------------------------------------------------------------
>
> In the OnEndSession event my application then does some cleanup and calls
> Application->Terminate();
>
> When running the application and log off, the WM_ENDSESSION is caught by 
> TMyTrayIcon, the OnEndSession event is fired and my application shuts down 
> correctly.
>
> But Windows does not continue with the shutdown...
>
> Does anyone have an idea why or how to get Windows to complete the shut 
> down (logoff)?
>
> This problem bugs me now for quite a while. I have tried alternatives like 
> the ShutDownWatcher but without success :-(
> Does anyone has a version of TTrayIcon that allows to define a shut down 
> event?
>
>
> Rds
> Marc
>
>
> 
Re: TTrayIcon and Shutdown (one step later)
#1050
Author: JohnC
Date: Thu, 15 Nov 2007 23:55
80 lines
3310 bytes
Here's the whole thing:

//Note: This function was taken verbatim from the SDK documentation.
BOOL TForm1::MySystemShutdown()
{
   HANDLE hToken;
   TOKEN_PRIVILEGES tkp;

   // Get a token for this process.

   if (!OpenProcessToken(GetCurrentProcess(),
        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
      return( FALSE );

   // Get the LUID for the shutdown privilege.

   LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
        &tkp.Privileges[0].Luid);

   tkp.PrivilegeCount = 1;  // one privilege to set
   tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

   // Get the shutdown privilege for this process.

   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
        (PTOKEN_PRIVILEGES)NULL, 0);

   if (GetLastError() != ERROR_SUCCESS)
      return FALSE;

   // Shut down the system and force all applications to close.

   if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
      return FALSE;

   return TRUE;
}
//---------------------------------------------------------------------------

"Marc" <info@mawnet.com> wrote in message 
news:473cd9cf$1@newsgroups.borland.com...
> Hi all
>
> I read a lot about the TTrayIcon component and its problems with detecting 
> the WM_ENDSESSION with formless applications.
>
> So I took the source and modified the code in order to call an event in 
> the application in addition to simply call Shell_NotifyIcon(NIM_DELETE, 
> &FData).
>
> void __fastcall TMyTrayIcon::EndSession()
> {
>   OnEndSession(this);
>   Shell_NotifyIcon(NIM_DELETE, &FData);
> }
> //---------------------------------------------------------------------------
>
> In the OnEndSession event my application then does some cleanup and calls
> Application->Terminate();
>
> When running the application and log off, the WM_ENDSESSION is caught by 
> TMyTrayIcon, the OnEndSession event is fired and my application shuts down 
> correctly.
>
> But Windows does not continue with the shutdown...
>
> Does anyone have an idea why or how to get Windows to complete the shut 
> down (logoff)?
>
> This problem bugs me now for quite a while. I have tried alternatives like 
> the ShutDownWatcher but without success :-(
> Does anyone has a version of TTrayIcon that allows to define a shut down 
> event?
>
>
> Rds
> Marc
>
>
> 
Re: TTrayIcon and Shutdown (one step later)
#1051
Author: Marc
Date: Fri, 16 Nov 2007 06:32
101 lines
4058 bytes
Hi John

Thank you for your answer.
This could probably work, but then my application would call Windows to shut 
down, not the user anymore.
The whole thing takes place when we are already in a shutdown sequence. The 
problem is that somehow the sequence is interrupted and Windows does not 
complete it. So rather than a way of restarting a new shutdown sequence I 
was looking for a way to complete the initial shutdown sequence started by 
the user.

Rds

Marc



"JohnC" <jcibulskis@sbcglobal.net> schrieb im Newsbeitrag 
news:473cdc5d@newsgroups.borland.com...
> Here's the whole thing:
>
> //Note: This function was taken verbatim from the SDK documentation.
> BOOL TForm1::MySystemShutdown()
> {
>   HANDLE hToken;
>   TOKEN_PRIVILEGES tkp;
>
>   // Get a token for this process.
>
>   if (!OpenProcessToken(GetCurrentProcess(),
>        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
>      return( FALSE );
>
>   // Get the LUID for the shutdown privilege.
>
>   LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
>        &tkp.Privileges[0].Luid);
>
>   tkp.PrivilegeCount = 1;  // one privilege to set
>   tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
>
>   // Get the shutdown privilege for this process.
>
>   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
>        (PTOKEN_PRIVILEGES)NULL, 0);
>
>   if (GetLastError() != ERROR_SUCCESS)
>      return FALSE;
>
>   // Shut down the system and force all applications to close.
>
>   if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
>      return FALSE;
>
>   return TRUE;
> }
> //---------------------------------------------------------------------------
>
> "Marc" <info@mawnet.com> wrote in message 
> news:473cd9cf$1@newsgroups.borland.com...
>> Hi all
>>
>> I read a lot about the TTrayIcon component and its problems with 
>> detecting the WM_ENDSESSION with formless applications.
>>
>> So I took the source and modified the code in order to call an event in 
>> the application in addition to simply call Shell_NotifyIcon(NIM_DELETE, 
>> &FData).
>>
>> void __fastcall TMyTrayIcon::EndSession()
>> {
>>   OnEndSession(this);
>>   Shell_NotifyIcon(NIM_DELETE, &FData);
>> }
>> //---------------------------------------------------------------------------
>>
>> In the OnEndSession event my application then does some cleanup and calls
>> Application->Terminate();
>>
>> When running the application and log off, the WM_ENDSESSION is caught by 
>> TMyTrayIcon, the OnEndSession event is fired and my application shuts 
>> down correctly.
>>
>> But Windows does not continue with the shutdown...
>>
>> Does anyone have an idea why or how to get Windows to complete the shut 
>> down (logoff)?
>>
>> This problem bugs me now for quite a while. I have tried alternatives 
>> like the ShutDownWatcher but without success :-(
>> Does anyone has a version of TTrayIcon that allows to define a shut down 
>> event?
>>
>>
>> Rds
>> Marc
>>
>>
>>
>
> 
Re: TTrayIcon and Shutdown (one step later)
#1055
Author: Remy Lebeau (TeamB)
Date: Fri, 16 Nov 2007 09:16
15 lines
1473 bytes
"Marc" <info@mawnet.com> wrote in message
news:473d39cd$1@newsgroups.borland.com...

> The whole thing takes place when we are already in a shutdown
> sequence. The problem is that somehow the sequence is interrupted
> and Windows does not complete it.

That happens when an application tells the OS to abort the shutdown when
replying to the shutdown messages.  So either 1) you are not responding to
them correctly, or 2) another application outside of your control is halting
the shutdown after your app is already terminated.


Gambit
Re: TTrayIcon and Shutdown (one step later)
#1052
Author: Remy Lebeau (TeamB)
Date: Fri, 16 Nov 2007 17:58
14 lines
1560 bytes
"Marc" <info@mawnet.com> wrote in message 
news:473d39cd$1@newsgroups.borland.com...

> The problem is that somehow the sequence is interrupted
> and Windows does not complete it.

That happens when an application tells the OS to abort the shutdown in reply 
to the shutdown messages.  So either 1) you are not replying to them 
correctly, or 2) another application outside of your control is doing so 
after your app has already been terminated.


Gambit 
Re: TTrayIcon and Shutdown (one step later)
#1056
Author: Marc
Date: Sat, 17 Nov 2007 10:02
34 lines
2295 bytes
Hi Remy

Yes this must be the case... I tried to have just an empty form and the 
TTrayIcon control on it. Here I was able to catch the WM_ENDSESSION message 
but only when the Form was visible. When the form is minimized the TTrayIcon 
calls ShowWindow(Application->Handle, SW_HIDE) and then does not receive any 
messages anymore (at least this is what I think).
Anyway I got rid of all the external components (TTrayIcon and 
TShutDownWatcher) and handle the Shell_NotifyIcon calls myself. Since then I 
also get the WM_ENDSESSION messages and it works...

Rds
Marc




"Remy Lebeau (TeamB)" <no.spam@no.spam.com> schrieb im Newsbeitrag 
news:473dda9b$1@newsgroups.borland.com...
>
> "Marc" <info@mawnet.com> wrote in message 
> news:473d39cd$1@newsgroups.borland.com...
>
>> The problem is that somehow the sequence is interrupted
>> and Windows does not complete it.
>
> That happens when an application tells the OS to abort the shutdown in 
> reply to the shutdown messages.  So either 1) you are not replying to them 
> correctly, or 2) another application outside of your control is doing so 
> after your app has already been terminated.
>
>
> Gambit
> 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads