Board index » cppbuilder » Ending Windows Session and WM_QUERYENDSESSION and WM_ENDSESSION

Ending Windows Session and WM_QUERYENDSESSION and WM_ENDSESSION

Hi All,

I have a program that will not shut down when I want to end the current
windows session, I'm using Windows XP.  I have tried a number of methods
(overriding WndProc) but I cannot get it to close.  Could someone offer me a
solution?

Seymour

 

Re:Ending Windows Session and WM_QUERYENDSESSION and WM_ENDSESSION


This works for me...

   HANDLE hToken;
   TOKEN_PRIVILEGES tkp;

   // Get a token for this process.
   //
   if (!OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY, &hToken ) )
   {
      ReportErrorAndTerminate( "OpenProcessToken" );
   }

   // 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 );

   // Cannot test the return value of AdjustTokenPrivileges.
   //
   if ( GetLastError() != ERROR_SUCCESS )
   {
      ReportErrorAndTerminate( "AdjustTokenPrivileges" );
   }

   // Shut down the system and force all applications to close.
   //
   if ( !ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, 0 ) )
   {
      ReportErrorAndTerminate( "ExitWindowsEx" );
   }

Hope this helps,
Michael

On Sun, 31 Mar 2002 20:00:19 +0100, "Fractal Cat"

Quote
<primes...@primesoft.greatxscape.net> wrote:
>Hi All,

>I have a program that will not shut down when I want to end the current
>windows session, I'm using Windows XP.  I have tried a number of methods
>(overriding WndProc) but I cannot get it to close.  Could someone offer me a
>solution?

>Seymour

Remove NOSPAM to get my address.

Re:Ending Windows Session and WM_QUERYENDSESSION and WM_ENDSESSION


Hi Michael,

I have used and tested this sample of code in response to the WM_CLOSE
message within my program.  It works but I don't want it to respond to
WM_CLOSE because if you close the application you shutdown Windows!

Where should this sample of code reside in my program?

Thx

Seymour

Quote
"Michael Wade" <wademNOS...@digilink.net> wrote in message

news:d2ofau0nik3cv52ia88rkes926atqlskpl@4ax.com...
Quote
> This works for me...

>    HANDLE hToken;
>    TOKEN_PRIVILEGES tkp;

>    // Get a token for this process.
>    //
>    if (!OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
> | TOKEN_QUERY, &hToken ) )
>    {
>       ReportErrorAndTerminate( "OpenProcessToken" );
>    }

>    // 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 );

>    // Cannot test the return value of AdjustTokenPrivileges.
>    //
>    if ( GetLastError() != ERROR_SUCCESS )
>    {
>       ReportErrorAndTerminate( "AdjustTokenPrivileges" );
>    }

>    // Shut down the system and force all applications to close.
>    //
>    if ( !ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, 0 ) )
>    {
>       ReportErrorAndTerminate( "ExitWindowsEx" );
>    }

> Hope this helps,
> Michael

> On Sun, 31 Mar 2002 20:00:19 +0100, "Fractal Cat"
> <primes...@primesoft.greatxscape.net> wrote:

> >Hi All,

> >I have a program that will not shut down when I want to end the current
> >windows session, I'm using Windows XP.  I have tried a number of methods
> >(overriding WndProc) but I cannot get it to close.  Could someone offer
me a
> >solution?

> >Seymour

> Remove NOSPAM to get my address.

Other Threads