Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How to initiate a shutdown for Windows?



Permlink Replies: 6 - Last Post: Nov 6, 2017 4:05 PM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
Timur Tekil

Posts: 25
Registered: 12/25/13
How to initiate a shutdown for Windows?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 25, 2017 6:51 PM
(I am not sure if this is the right forum section for my question)

How can I initiate a shutdown for Windows?

I am using C++ Builder 10 Berlin.
Alex Belo

Posts: 626
Registered: 10/8/06
Re: How to initiate a shutdown for Windows?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 25, 2017 8:13 PM   in response to: Timur Tekil in response to: Timur Tekil
Timur Tekil wrote:

How can I initiate a shutdown for Windows?

I use this:

// what do you want:
bool Logout;
bool Reboot;
bool ShutDown;

bool QuerySystemStop(void)
{
if(Logout)
return ExitWindowsEx(EWX_LOGOFF, 0);
else
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
if(!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return false;

LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount=1; // one privilege to set
tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges
(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
if(GetLastError()!=ERROR_SUCCESS)
return false;

UINT flags=(Reboot ? EWX_REBOOT : EWX_POWEROFF);
return ExitWindowsEx(flags, 0);
}
}

--
Alex
Timur Tekil

Posts: 25
Registered: 12/25/13
Re: How to initiate a shutdown for Windows?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 26, 2017 1:42 PM   in response to: Alex Belo in response to: Alex Belo
Thanks for your answer, it worked great :)

Is using native api/Winapi the only choice or does C++ Builder's Api has it's own functions to do the same thing(shutdown Windows) too?
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: How to initiate a shutdown for Windows?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 26, 2017 3:32 PM   in response to: Timur Tekil in response to: Timur Tekil
Timur wrote:

Is using native api/Winapi the only choice

Yes.

does C++ Builder's Api has it's own functions to do
the same thing (shutdown Windows) too?

No.

--
Remy Lebeau (TeamB)
Timur Tekil

Posts: 25
Registered: 12/25/13
Re: How to initiate a shutdown for Windows?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 26, 2017 7:22 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thank you for your answer. :)

Remy Lebeau (TeamB) wrote:
Timur wrote:

Is using native api/Winapi the only choice

Yes.

does C++ Builder's Api has it's own functions to do
the same thing (shutdown Windows) too?

No.

--
Remy Lebeau (TeamB)
Guest
Re: How to initiate a shutdown for Windows?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 5, 2017 7:28 AM   in response to: Timur Tekil in response to: Timur Tekil
Timur Tekil wrote:
(I am not sure if this is the right forum section for my question)

How can I initiate a shutdown for Windows?

I am using C++ Builder 10 Berlin.

WOW so much fuss just call shutdown -r

spawnl(P_NOWAIT, "command", "shutdown -r", NULL);

or winAPI below more controll over executing level

/* PROCESS_INFORMATION pi;
STARTUPINFO si;

ZeroMemory(&si,sizeof(STARTUPINFO));
ZeroMemory(&pi,sizeof(PROCESS_INFORMATION));
si.cb=sizeof(STARTUPINFO);
si.dwFlags=STARTF_USESHOWWINDOW;
si.wShowWindow=SW_SHOWNORMAL;

CreateProcess("tcpserver_etrade.exe",NULL,NULL,NULL,false,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);
WaitForInputIdle(GetCurrentProcess(),INFINITE);
*/
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: How to initiate a shutdown for Windows?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 6, 2017 4:05 PM   in response to: Guest in response to: Guest
Marc Van den Broeck wrote:

WOW so much fuss just call shutdown -r

Calling the ExitWindowsEx() function is the correct and appropriate way to shutdown Windows in code.

spawnl(P_NOWAIT, "command", "shutdown -r", NULL);

shutdown.exe is an actual program, you can run it directly, you don't need to run it through the command processor (and you should be using cmd.exe instead of command.com anyway).

CreateProcess("tcpserver_etrade.exe",NULL,NULL,NULL,false,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);

I assume you meant something more like this instead:

CreateProcess("cmd.exe","shutdown -r", NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);


WaitForInputIdle(GetCurrentProcess(),INFINITE);

Why would you want to have the calling process wait on itself? That is a deadlock waiting to happen. If yo are going to wait at all (but you specified P_NOWAIT in your spawnl() example), you should be waiting on pi.hProcess instead of GetCurrentProcess().

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02