Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shutting down Windows

Status
Not open for further replies.

viper123

Technical User
Jun 21, 2003
14
0
0
RO
Hi folks!
I want to make an application which shuts down the machine. Could you tell me how to do this? I've tried to use ExitWindowsEx, but it doesn't work on win98 OS. It says it works on Windows NT only.
 
One day i downloaded a component to do those thing but it didnt' work
here I leave the code, check it & fix it, think that is just a little detail

//*.h file
//#ifndef ShutdownH
#define ShutdownH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
//----------------------------------------------------------enum foo {sfFORCE,sfLOGOFF,sfREBOOT,sfSHUTDOWN,sfPOWEROFF};
typedef Set <foo,sfFORCE,sfPOWEROFF> TShutdownOptions;
//----------------------------------------------------------class PACKAGE TShutdown : public TComponent
{
private:
UINT shutdown_flags;
DWORD dwPlatformId;
TShutdownOptions FFlags;
inline void __fastcall Set_shutdown_flags()
{
if (FFlags.Contains(sfFORCE))
shutdown_flags = shutdown_flags | EWX_FORCE;
if (FFlags.Contains(sfLOGOFF))
shutdown_flags = shutdown_flags | EWX_LOGOFF;
if (FFlags.Contains(sfREBOOT))
shutdown_flags = shutdown_flags | EWX_REBOOT;
if (FFlags.Contains(sfSHUTDOWN))
shutdown_flags = shutdown_flags | EWX_SHUTDOWN;
if (FFlags.Contains(sfPOWEROFF))
shutdown_flags = shutdown_flags | EWX_POWEROFF;};
bool __fastcall ShutdownW32s();
bool __fastcall Shutdown95();
bool __fastcall ShutdownNT();
protected:
public:
bool __fastcall Shutdown();
__fastcall TShutdown(TComponent* Owner);
__published:
__property TShutdownOptions Flags = {read = FFlags,write = FFlags,nodefault};
};
//----------------------------------------------------------#endif




//*.cpp file
#include <vcl.h>
#include &quot;Shutdown.h&quot;
//----------------------------------------------------------#pragma hdrstop
#pragma package(smart_init)
//----------------------------------------------------------#define BADPLATFORM (WM_USER+3426)
//----------------------------------------------------------static inline void ValidCtrCheck(TShutdown *)
{
new TShutdown(NULL);
}
//----------------------------------------------------------__fastcall TShutdown::TShutdown(TComponent* Owner)
: TComponent(Owner)
{
if (!ComponentState.Contains(csDesigning))
{
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (GetVersionEx(&osvi))
dwPlatformId = osvi.dwPlatformId;
else
dwPlatformId = BADPLATFORM;
}
}
//----------------------------------------------------------bool __fastcall TShutdown::Shutdown()
{
switch (dwPlatformId)
{
case BADPLATFORM:
return false;
case VER_PLATFORM_WIN32s:
return ShutdownW32s();
case VER_PLATFORM_WIN32_WINDOWS:
return Shutdown95();
case VER_PLATFORM_WIN32_NT:
return ShutdownNT();
default:
return false;
}
}
//----------------------------------------------------------bool __fastcall TShutdown::ShutdownW32s()
{
return ExitWindows(NULL,NULL);
}
//----------------------------------------------------------bool __fastcall TShutdown::Shutdown95()
{
Set_shutdown_flags();
return ExitWindowsEx(shutdown_flags,NULL);
}
//----------------------------------------------------------bool __fastcall TShutdown::ShutdownNT()
{
HANDLE hToken,hProcess;
TOKEN_PRIVILEGES tp;
LUID_AND_ATTRIBUTES la;
LUID luid;
if (LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid))
{
la.Luid = luid;
la.Attributes = SE_PRIVILEGE_ENABLED;
tp.PrivilegeCount = 1;
tp.Privileges[0] = la;
hProcess = OpenProcess(PROCESS_ALL_ACCESS,false,GetCurrentProcessId());
if (hProcess != INVALID_HANDLE_VALUE)
{
if (OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES,&hToken))
{
if (AdjustTokenPrivileges(hToken,false,&tp,NULL,NULL,NULL))
{
CloseHandle(hToken);
Set_shutdown_flags();
return ExitWindowsEx(shutdown_flags,NULL);
}
CloseHandle(hToken);
}
}
}
return false;
}
//----------------------------------------------------------namespace Shutdown
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TShutdown)};
RegisterComponents(&quot;Cyborg&quot;, classes, 0);
}
}




---LastCyborg---
 
sorry for not indent like normal but I just copied & paste it




---LastCyborg---
 
The instruction that you metioned works in win 9x & Me, but not in NT, because you need some privileges to shutdown the system



---LastCyborg---
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top