Path: news.cs.au.dk!not-for-mail From: Morten Grouleff Newsgroups: comp.lang.beta Subject: Re: Reboot Windows via BETA? Date: 16 Feb 1999 08:27:24 +0100 Organization: Mjolner Informatics Lines: 114 Message-ID: References: <79sfni$r6l$1@sun579.rz.ruhr-uni-bochum.de> <7a9qot$kl4$1@sun579.rz.ruhr-uni-bochum.de> NNTP-Posting-Host: fenris.mjolner.dk Mime-Version: 1.0 (generated by tm-edit 7.106) Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: xinwen.cs.au.dk 919150045 6307 255.255.255.255 (16 Feb 1999 07:27:25 GMT) X-Complaints-To: news@cs.au.dk NNTP-Posting-Date: 16 Feb 1999 07:27:25 GMT X-Newsreader: Gnus v5.5/Emacs 19.34 Xref: news.cs.au.dk comp.lang.beta:11822 "Sascha Kimmel" writes: > Sascha Kimmel schrieb in Nachricht > 79sfni$r6l$1@sun579.rz.ruhr-uni-bochum.de.../// > >Hi! > > > >I want to have my program restart Windows (yes, ONLY Windows, not MacOS, > Sun > >solaris, Linux etc.) automatically. I found out that I have to use > >"EWX_REBOOT" for that and found the following quite interesting lines in > >winuserconsts.bet (Mjolner System Professional 4.1, Windows 98): > > [...] > > Hi again! > > I hope that someone from Mjolner will answer to my posting, because I think > they'll need everyone who uses BETA for "real" programs. > I did not buy the Professional version to get NO support. Well, you should mail support@mjolner.com then. This newsgroup and usergroup is a forum for discussion among users of BETA, including the Mjolner System. > Please post a reply as soon as possible because I really need this > functionality! Okay, I'll look it up in the windows documentation for you. The following information is mostly cut&paste from the documentation of ExitWindowsEx in Visual C. On windows 95/98, the following function suffices: BOOL ExitWindowsEx( UINT uFlags, // shutdown operation DWORD dwReserved // reserved ); And for windows NT, the following should work: HANDLE hToken; TOKEN_PRIVILEGES tkp; // 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"); // Shut down the system and force all applications to close. if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0)) error("ExitWindowsEx"); For both, the following values for uFlags are possible: Specifies the type of shutdown. This parameter must be some combination of the following values: EWX_FORCE Forces processes to terminate. When this flag is set, Windows does not send the messages WM_QUERYENDSESSION and WM_ENDSESSION to the applications currently running in the system. This can cause the applications to lose data. Therefore, you should only use this flag in an emergency. EWX_LOGOFF Shuts down all processes running in the security context of the process that called the ExitWindowsEx function. Then it logs the user off. EWX_POWEROFF Shuts down the system and turns off the power. The system must support the power-off feature. EWX_REBOOT Shuts down the system and then restarts the system. EWX_SHUTDOWN Shuts down the system to a point at which it is safe to turn off the power. All file buffers have been flushed to disk, and all running processes have stopped. Apparently, using the EWX_FORCE is not recommended. Return Values for ExitWindowsEx: If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError. Remarks The ExitWindowsEx function returns as soon as it has initiated the shutdown. The shutdown or logoff then proceeds asynchronously. Writing the shutdown stuff for NT in BETA is somewhat cumbersome, so I'd recommend putting it in a function in an external C file, and calling that function from beta. Hope this helps. -- ** Morten Grouleff: ** ** Earthworm Jim PC: ** ** Mjølner Informatics: **