禁止关机程序为何不管用
在网上看到的程序,如下:
1.先打开头文件作如下修改:
class TForm1 : public TForm {
__published: // IDE-managed Components
private: // User declarations
void __fastcall WMQueryEndSession(TWMQueryEndSession &msg);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_QUERYENDSESSION,TWMQueryEndSession,WMQueryEndSession)
END_MESSAGE_MAP(TForm)
};
//---------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------
#endif
2.然后对unit.cpp文件添加如下代码:
void __fastcall TForm1::WMQueryEndSession(TWMQueryEndSession &msg) {
msg.Result = 0;
ShowMessage("你不能关闭系统");
}
我在c++ builder下运行,通过开始菜单,关机。根本不起作用,怎么回事?
[解决办法]
给你找了个完美的。
#include <Windows.h>#include <tlhelp32.h>#include <winbase.h>//---------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"TForm1 *Form1;//---------------------------------------__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){}//---------------------------------------void EnableDebugPriv( void ){HANDLE hToken;TOKEN_PRIVILEGES tkp;OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES |TOKEN_QUERY, &hToken);LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);tkp.PrivilegeCount = 1;tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES)NULL, 0);CloseHandle( hToken );}//--------------------int GetPidByProcessName(AnsiString AppName){HANDLE hSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); PROCESSENTRY32 pe; int AppPid = 0; pe.dwSize = sizeof(pe); BOOL bNext=Process32First(hSnap, &pe); while(bNext) { if(strcmp(pe.szExeFile,AppName.c_str())==0) { AppPid = pe.th32ProcessID; CloseHandle(hSnap); return AppPid; } bNext=Process32Next(hSnap, &pe); } CloseHandle(hSnap); return AppPid;}//------------------------------void __fastcall TForm1::Button1Click(TObject *Sender){BOOL res = FALSE;JOBOBJECT_BASIC_UI_RESTRICTIONS JobInfo;ZeroMemory(&JobInfo, sizeof(JOBOBJECT_BASIC_UI_RESTRICTIONS));JobInfo.UIRestrictionsClass = JOB_OBJECT_UILIMIT_EXITWINDOWS;EnableDebugPriv();// 建立JOB 对象 命名为WINLOCKHANDLE hjob = CreateJobObject(NULL, TEXT("WINLOCK"));SetInformationJobObject(hjob, JobObjectBasicUIRestrictions, &JobInfo, sizeof(JobInfo));DWORD Pid = GetPidByProcessName("winlogon.exe");HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, Pid);if (hProcess == NULL){ ShowMessage("打开winlogon进程失败"); return;}res = AssignProcessToJobObject(hjob,hProcess);//将进程和对象关联起来if (!res){ ShowMessage("挂机失败");}}//---------------------------------------