cbuilder 关机程序问题
#include <vcl.h>
#pragma hdrstop
#include "guanji.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
CheckBox1->Checked=false;
//初始设置为不可访问
CheckBox1->Enabled=false;
DateTimePicker4->DateTime=Now();
DateTimePicker3->DateTime=Now();
DateTimePicker2->DateTime=Now();
//用系统时间初始化组件
DateTimePicker1->DateTime=Now();
}
//---------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
DateTimePicker2->DateTime=Now();
//接收当前日期和时间,用于即时显示
DateTimePicker1->DateTime=Now();
//随时检测设定的关机日期和时间是否有效
if(DateTimePicker4->DateTime < Now())
{
CheckBox1->Checked=false;
//无效时不可访问
CheckBox1->Enabled=false;
}
else
CheckBox1->Enabled=true; //有效时可以访问
if(CheckBox1->Checked == true
&&DateTimeToStr(DateTimePicker4->DateTime) == DateTimeToStr(Now()))
{//判断定时关机条件是否满足
DWORD tmp;
//调用win api函数关闭计算机
ExitWindowsEx(EWX_SHUTDOWN,tmp);
}
//定义日期时间型对象(变量)
TDateTime presetDateTime,currentDateTime;
presetDateTime=DateTimePicker3->DateTime;
currentDateTime=Now();
//得到时间差(秒数)
if(CheckBox1->Checked)
Edit1->Text=int((presetDateTime.Val-currentDateTime.Val)*60*60*24);
else
Edit1->Text=NULL;
}
//---------------------------------------
void __fastcall TForm1::DateTimePicker3Change(TObject *Sender)
{
//组件变化“联动”
DateTimePicker4->DateTime=DateTimePicker3->DateTime;
//检测设置的日期是否有效
if(DateTimePicker4->DateTime < Now())
{
CheckBox1->Checked=false;
CheckBox1->Enabled=false;
}
else
CheckBox1->Enabled=true;
}
//---------------------------------------
void __fastcall TForm1::DateTimePicker4Change(TObject *Sender)
{
//组件变化“联动”
DateTimePicker3->DateTime=DateTimePicker4->DateTime;
//检测设置的时间是否有效
if(DateTimePicker3->DateTime < Now())
{
CheckBox1->Checked=false;
CheckBox1->Enabled=false;
}
else
CheckBox1->Enabled=true;
}
//---------------------------------------
这是用cbuilder做的关机程序源码,运行后一切正常,就是倒计时0后,什么也不发生,电脑也不关机,求高手指教``
[解决办法]
先获取系统管理员权限然后关机..
以下是关机部分代码
void __fastcall TFormMessage::CloseWindows()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
OpenProcessToken(OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId()), TOKEN_ADJUST_PRIVILEGES, &hToken);
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (AdjustTokenPrivileges(hToken, FALSE, &tkp, NULL, NULL, NULL))
ExitWindowsEx(EWX_FORCE
[解决办法]
EWX_POWEROFF, 0);
}