工控机主板+win2000+sp4 用代码不能关闭计算机(50分)

  • 主题发起人 主题发起人 yanghai0437
  • 开始时间 开始时间
Y

yanghai0437

Unregistered / Unconfirmed
GUEST, unregistred user!
工控机主板+win2000+sp4 用代码不能关闭计算机,但是通过windows自己的关机功能能够
关闭计算机。
我用下面的shutdown函数不能关闭计算机,但是能够关闭我的笔记本。

请问大家知道这是为什么吗?

function GetWinVersion: String;
var
VersionInfo : TOSVersionInfo;
OSName : String;
begin
// set the size of the record
VersionInfo.dwOSVersionInfoSize := SizeOf( TOSVersionInfo );

if Windows.GetVersionEx( VersionInfo ) then
begin
with VersionInfo do
begin
case dwPlatformId of
VER_PLATFORM_WIN32s : OSName := 'Win32s';
VER_PLATFORM_WIN32_WINDOWS : OSName := 'Windows 95';
VER_PLATFORM_WIN32_NT : OSName := 'Windows NT';
end; // case dwPlatformId
Result := OSName + ' Version ' + IntToStr( dwMajorVersion ) + '.' + IntToStr( dwMinorVersion ) +
#13#10' (Build ' + IntToStr( dwBuildNumber ) + ': ' + szCSDVersion + ')';
end; // with VersionInfo
end // if GetVersionEx
else
Result := '';
end;

procedure ShutDown;
const
SE_SHUTDOWN_NAME = 'SeShutdownPrivilege'; // Borland forgot this declaration
var
hToken : THandle;
tkp : TTokenPrivileges;
tkpo : TTokenPrivileges;
zero : DWORD;
begin
if Pos( 'Windows NT', GetWinVersion) = 1 then // we've got to do a whole buch of things
begin
zero := 0;
if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
begin
MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK );
Exit;
end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)
if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
begin
MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK );
Exit;
end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)


// SE_SHUTDOWN_NAME
if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[ 0 ].Luid ) then
begin
MessageBox( 0, 'Exit Error', 'LookupPrivilegeValue() Failed', MB_OK );
Exit;
end; // if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[0].Luid )
tkp.PrivilegeCount := 1;
tkp.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;

AdjustTokenPrivileges( hToken, False, tkp, SizeOf( TTokenPrivileges ), tkpo, zero );
if Boolean( GetLastError() ) then
begin
MessageBox( 0, 'Exit Error', 'AdjustTokenPrivileges() Failed', MB_OK );
Exit;
end // if Boolean( GetLastError() )
else
ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN, 0 );
end // if OSVersion = 'Windows NT'
else
begin // just shut the machine down
ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN, 0 );
end; // else
end;
 
用下面代码看看,我在xp,2000下面都用他关机
ExitWindowsNT(EWX_POWEROFF or EWX_FORCE);

procedure tFavcon_in.ExitWindowsNT(uFlags : integer);
var
hToken : THANDLE;
tkp, tkDumb : TTokenPrivileges;
DumbInt : DWORD;
begin
FillChar(tkp, sizeof(tkp), 0);
// Get a token for this process
if not (OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES
or TOKEN_QUERY, hToken)) then
raise Exception.create('OpenProcessToken failed with code '
+ inttostr(GetLastError));

// Get the LUID for the Shutdown privilege
LookupPrivilegeValue(nil, pchar('SeShutdownPrivilege'),
tkp.Privileges[0].Luid);

tkp.PrivilegeCount := 1; // one privilege to set
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;

// Get the shutdown provolege for this process
AdjustTokenPrivileges(hToken,false,tkp, sizeof(tkDumb),tkDumb,DumbInt);

// Cannot test the return value of AdjustTokenPrivileges
if GetLastError <> ERROR_SUCCESS then
Raise Exception.create('AdjustTokenPrivileges failed with code '
+ inttostr(GetLastError));

// shut down the system and for all applications to close
if not ExitWindowsEx(uFlags, 0) then
Raise Exception.create('ExitWindowsEx failed with code '
+ inttostr(GetLastError));
end;
 
你的代码跟我的一样,没有差别
 
mail地址,我发个Pas文件给你。专门用来关机的。
 
huiyue:谢谢了
yanghai0437@21cn.com
我的email
 
不好意思,今天才看到,邮件已发送。注意查看。
 
请问:(huiyue的)与你自己写的这个有什么不同吗?
 
多人接受答案了。
 
后退
顶部