用Delphi创建服务程序

用Delphi创建服务程序
日期:2005年11月29日 作者:sunmohe 人气: 3154 查看:[大字体中字体小字体]

function EnableDebugPrivilege: Boolean;

function EnablePrivilege(hToken: Cardinal; PrivName: string; bEnable: Boolean): Boolean;

var

TP: TOKEN_PRIVILEGES;

Dummy: Cardinal;

begin

TP.PrivilegeCount := 1;

LookupPrivilegeValue(nil, pchar(PrivName), TP.Privileges[0].Luid);

if bEnable then

TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED

else TP.Privileges[0].Attributes := 0;

AdjustTokenPrivileges(hToken, False, TP, SizeOf(TP), nil, Dummy);

Result := GetLastError = ERROR_SUCCESS;

end;

var

hToken: Cardinal;

begin

OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, hToken);

result:=EnablePrivilege(hToken, SeDebugPrivilege, True);

CloseHandle(hToken);

end;

使用方法:

EnableDebugPrivilege;//提升权限

KillTask(xxxx.exe);//关闭该服务程序.