function SetPrivilege(hToken : THandle; strPrivilege : PChar; bEnable:BOOL):BOOL;
function SetCurProcessDbgPrivilege:BOOL;
function UnSetCurProcessDbgPrivilege:BOOL;
implementation
function SetPrivilege(hToken : THandle; strPrivilege : PChar; bEnable:BOOL):BOOL;
var
tp : TOKEN_PRIVILEGES;
luid : TLargeInteger;
tpPrevious : TOKEN_PRIVILEGES;
cbPrevious : DWORD;
cbRtn : DWORD;
begin
cbPrevious := sizeof(TOKEN_PRIVILEGES);
if not LookupPrivilegeValue(nil, strPrivilege, luid) then begin
result := FALSE;
exit;
end;
AdjustTokenPrivileges(hToken, FALSE, tp,
sizeof(TOKEN_PRIVILEGES),
tpPrevious,
cbPrevious);
if (GetLastError() <> ERROR_SUCCESS) then begin
result := FALSE;
exit;
end;
if (bEnable) then begin
tpPrevious.Privileges[0].Attributes :=
tpPrevious.Privileges[0].Attributes or SE_PRIVILEGE_ENABLED;
end
else begin
tpPrevious.Privileges[0].Attributes :=
tpPrevious.Privileges[0].Attributes and (not SE_PRIVILEGE_ENABLED)
end;
AdjustTokenPrivileges(
hToken,
FALSE,
tpPrevious,
cbPrevious,
nil,
cbRtn);
if (GetLastError() <> ERROR_SUCCESS) then
result := FALSE;
result := TRUE;
end;
function SetCurProcessDbgPrivilege:BOOL;
var
hToken : THandle;
begin
result := TRUE;
if (not OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
hToken ))
then begin
ShowMessage('Can not get the Process Token!');
result := FALSE;
exit;
end;
if (not SetPrivilege(hToken, SE_DEBUG_NAME, TRUE)) then begin
result := FALSE;
CloseHandle(hToken);
exit;
end;
CloseHandle(hToken);
end;
function UnSetCurProcessDbgPrivilege:BOOL;
var
hToken : THandle;
begin
result := TRUE;
if (not OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
hToken ))
then begin
ShowMessage('Can not get the Process Token!');
result := FALSE;
exit;
end;
if (not SetPrivilege(hToken, SE_DEBUG_NAME, FALSE)) then begin
result := FALSE;
CloseHandle(hToken);
exit;
end;
(******************************************************************************
* CopyRight (c) By GanHuaXin 2002
* All Right Reserved
* Email : huiyugan@263.net
* Date :
* New Develop : 2002-x-x
* Modified : 2001-05-26
******************************************************************************)
unit OpenThread;
interface
uses
Windows,
TlHelp32,
SysUtils;
function OpenThread2(dwThreadID : DWORD; bInherit : BOOL):THandle;stdcall;
function GetProcessID(strProcessName : string):DWORD;
function GetThreadID(dwOwnerProcessID : DWORD):DWORD;
if osvi.dwPlatformId = VER_PLATFORM_WIN32_NT then
result := OpenThreadNT(dwThreadID, bInherit)
else begin
procpPdb := PPDB(XORProcessThreadID(GetCurrentProcessID()));
lpThreadObj := PTCB (XORProcessThreadID(dwThreadID));
if IsBadReadPtr(lpThreadObj, sizeof(TCB)) then begin
result := 0;
exit;
end;
if PBYTE(lpThreadObj)^ <> 7 then begin
result := 0;
exit;
end;
if (dwProcessID = GetCurrentProcessID()) then
hPrc := GetCurrentProcess()
else begin
hPrc := OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessID);
if (hPrc = 0) then begin
result := 0;
exit;
end;
end;
// 4 is the lowest handle in the table
// all proceses have this handle
b1 := DuplicateHandle(hPrc,
THandle(4),
GetCurrentProcess(),
@hThread,
THREAD_ALL_ACCESS,
bInherit, 0);
if (hPrc <> GetCurrentProcess()) then CloseHandle(hPrc);
function GetTrueProcAddress(lpMod : PChar; lpFunc : PChar):pointer;stdcall;
var
bla : pointer;
hMod : HModule;
begin
hMod := GetModuleHandle(lpMod);
if hMod=0 then begin
result := nil;
exit;
end;
bla := Pointer(GetProcAddress(hMod, lpFunc));
if (DWORD(bla) = 0) then begin
result := nil;
exit;
end;
if PByte(bla)^ = $68 then
bla := Pointer(PDWORD(DWORD(bla) + 1)^);
result := bla;
end;
function GetProcessID(strProcessName : string):DWORD;
var
dwRet : DWORD;
hSnapShot : THandle;
ProcessEntry : PROCESSENTRY32;
bFlag : BOOL;
begin
dwRet := 0;
hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(hSnapshot <> INVALID_HANDLE_VALUE) then
begin
FillChar(ProcessEntry,sizeof(PROCESSENTRY32),0);
ProcessEntry.dwSize := sizeof(PROCESSENTRY32);
bFlag := Process32First(hSnapshot,ProcessEntry);
while (bFlag) do
begin
if Pos(UpperCase(strProcessName), UpperCase(ProcessEntry.szExeFile)) <> 0 then
begin
dwRet := ProcessEntry.th32ProcessID;
break;
end;
ProcessEntry.dwSize := sizeof(PROCESSENTRY32);
bFlag := Process32Next(hSnapshot,ProcessEntry);
end;
CloseHandle(hSnapshot);
end;
result := dwRet;
end;
function GetThreadID(dwOwnerProcessID : DWORD):DWORD;
var
dwRet : DWORD;
hThreadSnap : THandle;
te32 : THREADENTRY32;
begin
dwRet := 0;
FillChar(te32, SizeOf(te32), 0);
hThreadSnap := CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap <> INVALID_HANDLE_VALUE) then begin
te32.dwSize := sizeof(THREADENTRY32);
if (Thread32First(hThreadSnap, te32)) then
repeat
if (te32.th32OwnerProcessID = dwOwnerProcessID) then begin
dwRet := te32.th32ThreadID;
break;
end;
until not (Thread32Next(hThreadSnap, te32));
CloseHandle (hThreadSnap);
end;
result := dwRet;
end;
procedure TfrmDll.FormShow(Sender: TObject);
begin
ShowWindow( Application.Handle, SW_HIDE );
SetWindowPos(handle, HWND_TOPMOST, 0, 0,
0, 0, SWP_NOMOVE or SWP_NOSIZE);
SetTimeLabel;
end;
procedure TfrmDll.SetTimeLabel;
var
szTextOut : array [0..255] of char;
SystemTime : _SYSTEMTIME;
begin
GetLocalTime(SystemTime);
StrLFmt(szTextOut, 255,
'Current Time is %d:%d:%d',
[SystemTime.wHour,
SystemTime.wMinute,
SystemTime.wSecond]);
label1.Caption := szTextOut;
if not Visible then begin
ShowWindow(Handle, SW_SHOW);
end;
end;
procedure TfrmDll.DoMessageProcess;
begin
while true do
Application.HandleMessage;
end;
procedure TfrmDll.Timer1Timer(Sender: TObject);
begin
SetTimeLabel;
end;
当你不需要让用户按Alt+Enter、Ctrl+Alt+Del、Ctrl+Esc等功能键的时候加入
以下代码:
Var
temp:integer;
begin
SystemParametersInfo(Spi_screensaverrunning,1,@temp,0);
end;
当你要恢复功能键时用以下代码:
Var
Temp:integer;
begin
SystemParametersInfo(spi_screensaverrunning,0,@temp,0);
end;
{
功能:低级键盘钩子的回调函数,在里面过滤消息
参数:nCode 是Hook的标志
WParam 表示消息的类型
LParam 是一个指向我们在上面定义的哪个结构KBDLLHOOKSTRUCT的指针
返回值:如果不是0的话windows就把这个消息丢掉,程序就不会再收到这个消息了。
}
function LowLevelKeyboardProc(nCode: Integer;
WParam: WPARAM;LParam: LPARAM):LRESULT; stdcall;
var
fEatKeystroke: BOOL;
p: PKBDLLHOOKSTRUCT;
begin
Result := 0;
fEatKeystroke := FALSE;
p := PKBDLLHOOKSTRUCT (lParam);
//nCode值为HC_ACTION时表示WParam和LParam参数包涵了按键消息
if (nCode = HC_ACTION) then
begin
//拦截按键消息并测试是否是Ctrl+Esc、Alt+Tab、Windows键和Alt+Esc功能键。
case wParam of
WM_KEYDOWN,
WM_SYSKEYDOWN,
WM_KEYUP,
WM_SYSKEYUP:
fEatKeystroke := ((p.vkCode = VK_TAB) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or
((p.vkCode = VK_ESCAPE) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or
((p.vkCode = VK_ESCAPE) and ((GetKeyState(VK_CONTROL) and $8000) <> 0)) or
((p.vkCode = VK_LWIN) or (p.vkCode = VK_RWIN) or (p.vkCode = VK_APPS)) or
((p.vkCode = VK_DELETE) and(p.vkCode =VK_CONTROL) and(p.vkCode=KF_ALTDOWN));// or (p.vkCode = VK_DELETE);
//fEatKeystroke := (p.vkCode = VK_DELETE) and(p.vkCode =VK_CONTROL) and(p.vkCode=KF_ALTDOWN);
end;
end;
if fEatKeystroke = True then
Result := 1;
if nCode <> 0 then
Result := CallNextHookEx(0, nCode, wParam, lParam);
end;
调用键盘HOOK的代码如下:
var
hhkLowLevelKybd: THandle;
begin
if hhkLowLevelKybd = 0 then
hhkLowLevelKybd := SetWindowsHookExW(WH_KEYBOARD_LL, LowLevelKeyboardProc,
Hinstance, 0); //使WINDOWS系统键失效(CTRL+ESC, WINDOWS_L, WINDOWS_R)
end;
以上是屏蔽ALT+TAB键的代码