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 "Shutdown.h"
//----------------------------------------------------------#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("Cyborg", classes, 0);
}
}
---LastCyborg---