🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
4 messages
4 total messages Started by bashar Fri, 11 May 2007 16:34
How can I restart windows XP from my Program?
#2027
Author: bashar
Date: Fri, 11 May 2007 16:34
9 lines
1010 bytes
Hi,



How can I restart windows XP from my Program?



Thank you.
Re: How can I restart windows XP from my Program?
#2029
Author: Clayton Arends
Date: Fri, 11 May 2007 17:04
8 lines
1200 bytes
Open the Windows SDK help file and navigate to "System Shutdown Functions". 
Or, search the help for ExitWindows or ExitWindowsEx.

Also, this question has been asked an uncountable number of times before. 
Head to http://groups.google.com and search for something like "cppbuilder 
restart windows".

- Clayton 
Re: How can I restart windows XP from my Program?
#2031
Author: Vladimir Stefanovic
Date: Fri, 11 May 2007 17:06
66 lines
2927 bytes
> How can I restart windows XP from my Program?

This has been discussed many times before.

// For example, try this function, with argument mode = 1:
void SystemShutDown( int mode )
{
   HANDLE hToken;
   TOKEN_PRIVILEGES tkp;

   AnsiString error;

   // Get a token for this process.
   if ( !OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | 
TOKEN_QUERY, &hToken ) )
      error = "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 )
      error = "AdjustTokenPrivileges";

   switch( mode )
   {
      // 0 = Forcefully close apps, shutdown and leave system at shutoff 
point
      case 0:   if ( !ExitWindowsEx( EWX_SHUTDOWN | EWX_FORCE, 0 ) )
                   error = "ExitWindowsEx";
                break;

      // 1 = Forcefully close apps, shutdown and reboot
      case 1:   if ( !ExitWindowsEx( EWX_REBOOT | EWX_FORCE, 0 ) )
                   error = "ExitWindowsEx";
                break;

      // 2 = Forcefully close apps, and logoff
      case 2:   if ( !ExitWindowsEx( EWX_LOGOFF | EWX_FORCE, 0 ) )
                   error = "ExitWindowsEx";
                break;

      // 3 = Forcefully close apps, shutdown and poweroff
      case 3:   if ( !ExitWindowsEx( EWX_POWEROFF | EWX_FORCE, 0 ) )
                   error = "ExitWindowsEx";
                break;
   }
}

Alternatively, you can call shutdown command:

   system( "shutdown -r -f -t 03" )
or:
   system( "shutdown -s -t 00" )
... but look at arguments for meanings (restart, logoff, shutdown ,...).


-- 
Best Regards,
Vladimir Stefanovic 
Re: How can I restart windows XP from my Program?
#2034
Author: bashar
Date: Fri, 11 May 2007 18:11
4 lines
1091 bytes
That really helps!
Thank you Vladimir Stefanovic

Bashar 
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