delphi自动关机代码分析
下面的代码摘自网上的一个delphi自动关机源程序,请大家看一下,这样的自动关机代码会导致硬盘出现坏道吗?不知怎么的,这几天,我那块老硬盘出了好几块物理坏道,就在用这个自动关机程序之后,大家说说看,与下面的代码有关吗?精通软硬件的朋友显身吧。
//NT关机准备函数
function SetPrivilege(sPrivilegeName : string;bEnabled : boolean ): boolean;
var
TPPrev,TP : TTokenPrivileges;
Token : THandle;
dwRetLen : DWord;
begin
Result := False;
OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,Token);
TP.PrivilegeCount:=1;
if( LookupPrivilegeValue(Nil,PChar( sPrivilegeName ),TP.Privileges[ 0 ].LUID ))then
begin
if( bEnabled )then //Give this privileges
begin
TP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;
end
else begin //NOT Give this privileges
TP.Privileges[ 0 ].Attributes := 0;
end;
dwRetLen := 0;
//enables or disables privileges in the specified access token.
Result := AdjustTokenPrivileges(Token,False,TP,SizeOf( TPPrev ),TPPrev,dwRetLen);
end;
CloseHandle( Token );
end;
//NT关机函数
function WinExitInNT( iFlags : integer ) : boolean;
begin
Result := True;
if( SetPrivilege( 'SeShutdownPrivilege',True ) )then
begin
if( not ExitWindowsEx( iFlags,0 ) )then
begin
Result := False;
end;
SetPrivilege( 'SeShutdownPrivilege',False )
end
else begin
// handle errors...
Result := False;
end;
end;
procedure TFShutDown.Timer1Timer(Sender: TObject);
begin
m:=m+1;
if m>=n then begin
if CkB_beforshut.Checked then begin
FShutDownInfo.show;
n:=n+1;
exit;
end;
if Lab_CanShutdown.Caption='0' then begin
// win9x
if Win32Platform = VER_PLATFORM_WIN32_WINDOWS then
ExitWindowsEx(EWX_FORCE+EWX_SHUTDOWN+EWX_POWEROFF,32);
// winNT
if Win32Platform = VER_PLATFORM_WIN32_NT then
WinExitInNT(EWX_FORCE+EWX_SHUTDOWN+EWX_POWEROFF);
end else Btn_cancelClick(Sender);
end else
edt_mm.text:=inttostr(n-m);
end;
[ Last edited by zqssoft on 2005-11-20 at 09:36 ]