BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How can I restart windows XP from my Program?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API)
View previous topic :: View next topic  
Author Message
bashar
Guest





PostPosted: Fri May 11, 2007 9:34 pm    Post subject: How can I restart windows XP from my Program? Reply with quote



Hi,



How can I restart windows XP from my Program?



Thank you.
Back to top
Clayton Arends
Guest





PostPosted: Fri May 11, 2007 10:04 pm    Post subject: Re: How can I restart windows XP from my Program? Reply with quote



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
Back to top
Vladimir Stefanovic
Guest





PostPosted: Fri May 11, 2007 10:06 pm    Post subject: Re: How can I restart windows XP from my Program? Reply with quote



Quote:
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
Back to top
bashar
Guest





PostPosted: Fri May 11, 2007 11:11 pm    Post subject: Re: How can I restart windows XP from my Program? Reply with quote

That really helps!
Thank you Vladimir Stefanovic

Bashar
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.