🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #2031

Re: How can I restart windows XP from my Program?

#2031
From: 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 
Technical Details
Message-ID: <4644a2a7$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <46449b14@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,9f94a6520c48d9bb
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Vladimir Stefanovic" <antivari@po.sbb.co.yu>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <46449b14@newsgroups.borland.com>
Subject: Re: How can I restart windows XP from my Program?
Date: Fri, 11 May 2007 19:06:47 +0200
Lines: 68
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 87.116.181.111
Message-ID: <4644a2a7$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1178903207 87.116.181.111 (11 May 2007 10:06:47 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail