有关win2000下关机的问题(300分)

  • 主题发起人 主题发起人 www
  • 开始时间 开始时间
W

www

Unregistered / Unconfirmed
GUEST, unregistred user!
我用下面的代码在win2000下关机,为什么最后总是出现"现在可以安全的关闭计算机了"的提示,而不是直接关闭计算机电源???<br><br>unit guan;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure shutdown;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure Tform1.ShutDown;<br>const<br>&nbsp; SE_SHUTDOWN_NAME = 'SeShutdownPrivilege'; // Borland forgot this declaration<br>var<br>&nbsp; hToken : THandle;<br>&nbsp; tkp : TTokenPrivileges;<br>&nbsp; tkpo : TTokenPrivileges;<br>&nbsp; zero : DWORD;<br>begin<br>&nbsp; zero:=0;<br>&nbsp; if not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then begin<br>&nbsp; &nbsp; MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK );<br>&nbsp; &nbsp; Exit;<br>&nbsp; end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)<br>&nbsp; if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then begin<br>&nbsp; &nbsp; MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK );<br>&nbsp; &nbsp; Exit;<br>&nbsp; end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)<br><br>// SE_SHUTDOWN_NAME<br>&nbsp; if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[ 0 ].Luid ) then begin<br>&nbsp; &nbsp; &nbsp; MessageBox( 0, 'Exit Error', 'LookupPrivilegeValue() Failed', MB_OK );<br>&nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; end; // if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[0].Luid )<br>&nbsp; tkp.PrivilegeCount := 1;<br>&nbsp; tkp.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; AdjustTokenPrivileges( hToken, False, tkp, SizeOf( TTokenPrivileges ), tkpo, zero );<br>&nbsp; if Boolean( GetLastError() ) then begin<br>&nbsp; &nbsp; &nbsp; MessageBox( 0, 'Exit Error', 'AdjustTokenPrivileges() Failed', MB_OK );<br>&nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; end // if Boolean( GetLastError() )<br>&nbsp; else ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN, EWX_POWEROFF &nbsp; &nbsp; );<br>&nbsp; <br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; shutdown;<br>end;<br><br>end.<br>
 
请问,在NT4.0下可以吗?<br><br>我想答案一定是不可以,为什么呢?<br><br>因为NT是不支付ATX直接断开电源的,具体为何,就不得而知。<br><br>那是不是没有办法了呢?<br><br>不,不,不!!!!<br><br>我曾经在我的电脑上安装了WIN98,然后再安装了WIN2000,(注意:不是升级)<br>我在安装WIN98时,在命令行是这样输入的:<br>F:/WIN98&gt;setup/p j<br>那么,安装后的WIN98支持高级电源管理,可以不退出系统,直接按电源开关,<br>系统会关闭WIN98后,再断开电源。<br>而我此时惊奇的发现,我的WIN2000退出后竟然把电源断开了,我好高兴啊!<br><br>所以将这个消息告诉你!
 
to www<br>在98一定没问题,在2000直接选关机都只有很少的计算机能关上<br>还有一个关机程序你居然写了那么多代码,晕!
 
代码没有问题,我可以正常关机。<br><br>我想可能是电源管理方面的问题,首先必须是ATX电源,BIOS里面需要打开相应的配置,<br>Window NT自己关机也是使用了这些代码,绝对没有问题的。
 
你设置一下高级电源管理,里面有。
 
电源管理
 
我用的是atx电源,可以支持软关机的.用win2000的开始菜单关机可以直接关闭电源<br>的牙,为什么用程序就不行呢????
 
<br>修改<br>&nbsp; AdjustTokenPrivileges( hToken, False, tkp, SizeOf( TTokenPrivileges ), tkpo, zero );<br>为<br>&nbsp; AdjustTokenPrivileges(hToken, False, tkp, 0, nil, zero); <br><br>试试,你的程序没有问题,我这里这是抱着侥幸的心理让你试试。
 
两个地方,试验一下<br>AdjustTokenPrivileges( hToken, False, tkp, SizeOf( TTokenPrivileges ), tkpo, zero );<br>改为:<br>AdjustTokenPrivileges( hToken, False, tkp, 0, tkpo, zero );<br><br>ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN, EWX_POWEROFF &nbsp; &nbsp; );<br>改为:<br>ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN, 0);<br><br>另外参照一席MSDN中的例子:<br>HANDLE hToken; <br>TOKEN_PRIVILEGES tkp; <br>&nbsp;<br>// Get a token for this process. <br>&nbsp;<br>if (!OpenProcessToken(GetCurrentProcess(), <br>&nbsp; &nbsp; &nbsp; &nbsp; TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &amp;hToken)) <br>&nbsp; &nbsp; error("OpenProcessToken"); <br>&nbsp;<br>// Get the LUID for the shutdown privilege. <br>&nbsp;<br>LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, <br>&nbsp; &nbsp; &nbsp; &nbsp; &amp;tkp.Privileges[0].Luid); <br>&nbsp;<br>tkp.PrivilegeCount = 1; &nbsp;// one privilege to set &nbsp; &nbsp;<br>tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; <br>&nbsp;<br>// Get the shutdown privilege for this process. <br>&nbsp;<br>AdjustTokenPrivileges(hToken, FALSE, &amp;tkp, 0, <br>&nbsp; &nbsp; &nbsp; &nbsp; (PTOKEN_PRIVILEGES)NULL, 0); <br>&nbsp;<br>// Cannot test the return value of AdjustTokenPrivileges. <br>&nbsp;<br>if (GetLastError() != ERROR_SUCCESS) <br>&nbsp; &nbsp; error("AdjustTokenPrivileges"); <br>&nbsp;<br>// Shut down the system and force all applications to close. <br>&nbsp;<br>if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0)) <br>&nbsp; &nbsp; error("ExitWindowsEx"); <br><br><br>&nbsp;
 
我也碰到了和你相同的问题,用了下面的代码就好了<br>&nbsp; &nbsp; HANDLE hToken; <br>&nbsp; &nbsp; TOKEN_PRIVILEGES tkp; <br><br>&nbsp; &nbsp; OpenProcessToken(OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId()), TOKEN_ADJUST_PRIVILEGES, &amp;hToken); <br>&nbsp; &nbsp; LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &amp;tkp.Privileges[0].Luid); <br>&nbsp; &nbsp; tkp.PrivilegeCount = 1; <br>&nbsp; &nbsp; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; <br>&nbsp; &nbsp; if (AdjustTokenPrivileges(hToken, FALSE, &amp;tkp, NULL, NULL, NULL)) <br>&nbsp; &nbsp; &nbsp; &nbsp; ExitWindowsEx(EWX_FORCE | EWX_POWEROFF, 0);<br>&nbsp; &nbsp; <br>&nbsp; &nbsp; 和MSDN里给的没什么区别,只是加了“EWX_POWEROFF = 8”,但我见你的代码里也有。<br>&nbsp; &nbsp; 唯一的可能就是OpenProcess的区别了!<br>
 
电源,电源,还是电源,怎么这么没脑子?!
 
上面的程序编译后在win98中运行好象不行啊!
 
问题不是在代码上,而是在你的主板上是否使用了ATX电源。
 
问题不是在代码上,看你的机器硬件配置和系统设置
 
ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF, 0);<br>或者<br>InitiateSystemShutDown(nil, nil, 0, True, False);<br>肯定可以的,如果你开始菜单上可以的话。
 
NT 下可以通过修改注册表实现 自动断电(主板支持)<br>注意:每次重启都须修改一次<br>&nbsp;HKEY hkResult ;<br>&nbsp; &nbsp; &nbsp;DWORD lpType,lpcbData ;<br>&nbsp; &nbsp; &nbsp;BYTE lpData[100];<br>&nbsp; &nbsp; &nbsp;long ii = RegOpenKey(<br>&nbsp; &nbsp; &nbsp;HKEY_LOCAL_MACHINE ,<br>&nbsp; &nbsp; &nbsp;"SOFTWARE//Microsoft//Windows NT//CurrentVersion//Winlogon",<br>&nbsp; &nbsp; &nbsp;&amp;hkResult);<br>&nbsp; &nbsp; &nbsp;if(ii==ERROR_SUCCESS )<br>&nbsp; &nbsp; &nbsp;{<br>&nbsp; &nbsp; &nbsp; lpcbData =100;<br>&nbsp; &nbsp; &nbsp; unsigned char ip_add[20];<br><br>&nbsp; &nbsp; &nbsp; ip_add[0] = '1';<br>&nbsp; &nbsp; &nbsp; ip_add[1] = 0x00;<br>&nbsp; &nbsp; &nbsp; ip_add[2] = 0x00;<br>&nbsp; &nbsp; &nbsp; ip_add[3] = 0x00;<br><br><br>&nbsp; &nbsp; &nbsp; RegQueryValueEx(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hkResult,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "AutoAdminLogon",<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;lpType,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;lpData[0],<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;lpcbData );<br><br> &nbsp;lpcbData = 2;<br>&nbsp; &nbsp; &nbsp; RegSetValueEx(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hkResult,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"AutoAdminLogon",<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lpType,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;ip_add[0],<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lpcbData );<br><br><br>&nbsp; &nbsp; &nbsp;RegCloseKey(hkResult);<br>&nbsp; &nbsp; &nbsp;}
 
谁说NT4.0不可以关闭电源的?<br>我以前用的AST-6150(Pro 150 CPU),在NT4.0Server和Workstation下都可以自动关闭电源,但是98就不行,不知道为什么。(6150是准Server型机器,用过的人就知道了)
 
这个问题真的这么难吗?不可想象,我也被困扰许久了!!!!!!help!!!!!
 
后退
顶部