下面一段代码,是实现远程关机:
unit main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DsFancyButton;type
TForm1 = class(TForm)
EdtRomateName: TEdit;
BtnShutDown: TButton;
procedure BtnShutDownClick(Sender: TObject);
private
{ Private declarations }
function EnablePrivilege(lpSystemName:PChar; lpName:PChar):Bool;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}{ TForm1 }function TForm1.EnablePrivilege(lpSystemName, lpName: PChar): Bool;
var
hToken : THANDLE;
fOk : Bool;
fTest : Bool;
tp, tpNew : TOKEN_PRIVILEGES;
ReturnLength : Cardinal;
begin
fOk := FALSE;
if OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES,hToken) then
begin
tp.PrivilegeCount := 1;
if not (LookupPrivilegeValue(lpSystemName,lpName,tp.Privileges[0].Luid)) then
ShowMessage('Can not lookup privilege value!');
tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
if not (AdjustTokenPrivileges(hToken,FALSE,tp,sizeof(tp),tpNew,ReturnLength)) then
ShowMessage('Can not adjust privilege value.');
if GetLastError = ERROR_SUCCESS then
fOk := TRUE
else
fOK := FALSE;
CloseHandle(hToken);
end;
Result := fOk;
end;procedure TForm1.BtnShutDownClick(Sender: TObject);
var
RemoteShutDown : Bool;
begin
if EdtRomateName.Text <> '' then
begin
EnablePrivilege(PChar(EdtRomateName.Text),'SeRemoteShutdownPrivilege');
RemoteShutDown :=InitiateSystemShutdown(nil,nil,0,TRUE,FALSE);
if RemoteShutDown then
ShowMessage('远程关机成功!')
else
ShowMessage('远程关机失败!');
end;
end;end.
但是无法实现与远程关机;
如果将
EnablePrivilege(PChar(EdtRomateName.Text),'SeRemoteShutdownPrivilege');
改为EnablePrivilege(Nil,'SeRemoteShutdownPrivilege');
是实现本地关机,但是同样无法实现,请问错在哪了?
unit main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DsFancyButton;type
TForm1 = class(TForm)
EdtRomateName: TEdit;
BtnShutDown: TButton;
procedure BtnShutDownClick(Sender: TObject);
private
{ Private declarations }
function EnablePrivilege(lpSystemName:PChar; lpName:PChar):Bool;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}{ TForm1 }function TForm1.EnablePrivilege(lpSystemName, lpName: PChar): Bool;
var
hToken : THANDLE;
fOk : Bool;
fTest : Bool;
tp, tpNew : TOKEN_PRIVILEGES;
ReturnLength : Cardinal;
begin
fOk := FALSE;
if OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES,hToken) then
begin
tp.PrivilegeCount := 1;
if not (LookupPrivilegeValue(lpSystemName,lpName,tp.Privileges[0].Luid)) then
ShowMessage('Can not lookup privilege value!');
tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
if not (AdjustTokenPrivileges(hToken,FALSE,tp,sizeof(tp),tpNew,ReturnLength)) then
ShowMessage('Can not adjust privilege value.');
if GetLastError = ERROR_SUCCESS then
fOk := TRUE
else
fOK := FALSE;
CloseHandle(hToken);
end;
Result := fOk;
end;procedure TForm1.BtnShutDownClick(Sender: TObject);
var
RemoteShutDown : Bool;
begin
if EdtRomateName.Text <> '' then
begin
EnablePrivilege(PChar(EdtRomateName.Text),'SeRemoteShutdownPrivilege');
RemoteShutDown :=InitiateSystemShutdown(nil,nil,0,TRUE,FALSE);
if RemoteShutDown then
ShowMessage('远程关机成功!')
else
ShowMessage('远程关机失败!');
end;
end;end.
但是无法实现与远程关机;
如果将
EnablePrivilege(PChar(EdtRomateName.Text),'SeRemoteShutdownPrivilege');
改为EnablePrivilege(Nil,'SeRemoteShutdownPrivilege');
是实现本地关机,但是同样无法实现,请问错在哪了?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货