WINDOWNS 2000关机问题(老问题)(100分)

  • 主题发起人 主题发起人 lausunny
  • 开始时间 开始时间
L

lausunny

Unregistered / Unconfirmed
GUEST, unregistred user!
按照以前论坛和MSDN的说明用DELPHI写了WIN 2K 下定时关机的程序,但如果用WIN 2K 锁定后程序无法关闭电脑请教坛友!<br>共享个经验我觉得如果要学WINDOWS下编程去MSDN.MICROSFT.COM的网站很有用的。<br>以下贴出我的代码还请坛友指出问题所在(AdjustToken();是在以前的帖子抄的,如果用BUTTON的事件关机没问题)<br>procedure TForm1.AdjustToken();<br>var<br>&nbsp; hdlProcessHandle : Cardinal;<br>&nbsp; hdlTokenHandle : Cardinal;<br>&nbsp; tmpLuid : Int64;<br>&nbsp; tkpPrivilegeCount : Int64;<br>&nbsp; tkp : TOKEN_PRIVILEGES;<br>&nbsp; tkpNewButIgnored : TOKEN_PRIVILEGES;<br>&nbsp; lBufferNeeded : Cardinal;<br>&nbsp; Privilege : array[0..0] of _LUID_AND_ATTRIBUTES;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hdlProcessHandle := GetCurrentProcess;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OpenProcessToken(hdlProcessHandle,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hdlTokenHandle);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Get the LUID for shutdown privilege.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Privilege[0].Luid := tmpLuid;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp.PrivilegeCount := 1; &nbsp; // One privilege to set<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp.Privileges[0] := Privilege[0];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Enable the shutdown privilege in the access token of this<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// process.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AdjustTokenPrivileges(hdlTokenHandle,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;False,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Sizeof(tkpNewButIgnored),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkpNewButIgnored,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lBufferNeeded);<br><br>&nbsp;end;<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp; &nbsp;AdjustToken;<br>&nbsp; &nbsp; &nbsp;ExitWindowsEx((EWX_SHUTDOWN Or ewx_poweroff), 0);<br><br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>begin<br><br>if ((formatdatetime('dddd',now)='星期六') and (formatdatetime('h',now)='4')) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp;AdjustToken;<br>&nbsp; &nbsp; &nbsp;ExitWindowsEx((EWX_force Or ewx_shutdown or ewx_poweroff), 0);<br>&nbsp; &nbsp; end;<br>end;<br>end.<br><br>MICROSOFT CODE WINDOWS 2K<br>BOOL MySystemShutdown()<br>{<br>&nbsp; &nbsp;HANDLE hToken; <br>&nbsp; &nbsp;TOKEN_PRIVILEGES tkp; <br>&nbsp;<br>&nbsp; &nbsp;// Get a token for this process. <br>&nbsp;<br>&nbsp; &nbsp;if (!OpenProcessToken(GetCurrentProcess(), <br>&nbsp; &nbsp; &nbsp; &nbsp; TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &amp;hToken)) <br>&nbsp; &nbsp; &nbsp; return( FALSE ); <br>&nbsp;<br>&nbsp; &nbsp;// Get the LUID for the shutdown privilege. <br>&nbsp;<br>&nbsp; &nbsp;LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, <br>&nbsp; &nbsp; &nbsp; &nbsp; &amp;tkp.Privileges[0].Luid); <br>&nbsp;<br>&nbsp; &nbsp;tkp.PrivilegeCount = 1; &nbsp;// one privilege to set &nbsp; &nbsp;<br>&nbsp; &nbsp;tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; <br>&nbsp;<br>&nbsp; &nbsp;// Get the shutdown privilege for this process. <br>&nbsp;<br>&nbsp; &nbsp;AdjustTokenPrivileges(hToken, FALSE, &amp;tkp, 0, <br>&nbsp; &nbsp; &nbsp; &nbsp; (PTOKEN_PRIVILEGES)NULL, 0); <br>&nbsp;<br>&nbsp; &nbsp;if (GetLastError() != ERROR_SUCCESS) <br>&nbsp; &nbsp; &nbsp; return FALSE; <br>&nbsp;<br>&nbsp; &nbsp;// Shut down the system and force all applications to close. <br>&nbsp;<br>&nbsp; &nbsp;if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0)) <br>&nbsp; &nbsp; &nbsp; return FALSE; <br><br>&nbsp; &nbsp;return TRUE;<br>}<br>
 
sorry ~发贴的时候没仔细检查句子更正一下<br>(但如果用WIN 2K 锁定后程序无法关闭电脑请教坛友!)<br>更正 : 如果用WIN 2K 把电脑锁定后无法关闭电脑(CTRL+ALT+DEL锁定)<br>
 
惨没人关注去回答?!
 
做这个有用吗?
 
当然有用啦<br>周末公司的服务器可以自动关机不用整个星期都是开着的不用手动关了
 
function ShutDown(uFlags: Cardinal):boolean;<br>const<br>&nbsp; ADJUST_PRIV = TOKEN_QUERY or TOKEN_ADJUST_PRIVILEGES;<br>&nbsp; SHTDWN_PRIV = 'SeShutdownPrivilege';<br>&nbsp; PRIV_SIZE &nbsp; = sizeOf(TTokenPrivileges);<br>var<br>&nbsp; Len: DWORD;<br>&nbsp; TokenPriv, Dummy: TTokenPrivileges;<br>&nbsp; Token: THandle;<br>&nbsp; Error:integer;<br>begin<br>&nbsp; error:=0;<br>&nbsp; // 设置特权<br>&nbsp; // Delphi2:<br>&nbsp; //if not OpenProcessToken(GetCurrentProcess(), ADJUST_PRIV, @Token) then<br>&nbsp; if not OpenProcessToken(GetCurrentProcess(), ADJUST_PRIV, Token) then<br>&nbsp; &nbsp; &nbsp; Error := Error or 4;<br>&nbsp; if not LookupPrivilegeValue(nil, SHTDWN_PRIV,TokenPriv.Privileges[0].Luid) then<br>&nbsp; &nbsp; Error := Error or 8;<br>&nbsp; TokenPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; TokenPriv.PrivilegeCount := 1; &nbsp;// One privilege to set<br>&nbsp; if not AdjustTokenPrivileges(Token, false, TokenPriv, PRIV_SIZE,Dummy, Len) then<br>&nbsp; &nbsp; Error:=Error or 16;<br>&nbsp; ExitWindowsEx(uFlags, 0);<br>&nbsp; Result := (Error=0);<br>end;<br><br><br><br><br><br>procedure TForm1.BitBtn1Click(Sender: TObject);<br>begin<br>&nbsp;ShutDown(EWX_POWEROFF + EWX_FORCE);<br>end;
 
遗憾,用上面的方法,但锁住WIN2000后还是不能关电脑。没人救就得了我了。
 
看来真的是没人能救你了..<br>因为当你锁住win2000后,,openprocesstoken根本就不执行,,而且当你锁住win2000后,只要<br>不解锁,兄弟你就别想关机了.(除非你断电),,就像你看到的那个解锁窗口一样没有关机这<br>一项:::不过兄弟我还要以支一招..那就是用那个关机程序先调用winapi函数先解锁再执行<br>关机程序,老兄够你忙的了,,,不过你先把分给我吧,,我只能回答到这儿了...不要吝啬啊..<br>给点分吧!!!!!其它的问题可以再开贴呀 ,,,反正你分很多...我可是穷怕了.....
 
在管理工具-本地安全策略-安全设置-本地策略-安全选项中启用“允许在未登<br>录前关机”即可。
 
dearchenzhihua!<br>老兄很高兴,感激你给的提示,等解决了后会给你分的。但你也实在太黑了。。。。。<br>看来真的是没人能救你了..[:(]
 
TO:lausunny<br>&nbsp; 這個簡單得要命啊!:)<br>&nbsp; 告訴你們想問題不要想得那得復雜!ok?<br><br><br><br>解決的方法非常簡單!<br>就是發送ALT+S然後再發送一個enter!搞定!<br>不過你要編寫一個服務程序啊!要不然剛開機的時候也白搭!<br>再送你一招:就寫一個dll用asp進行遠程關機!<br>哈哈!我們公司就是這樣子的!<br><br>等等:另開300分怎樣?:)<br>&nbsp;<br><br><br>
 
TO ljy_17:<br>为什么不拿出个详细的解决方案就想得300也太不怎么样了吧,为了得分而骗分。<br>我想这个论坛应该是交流和帮助DELPHI爱好者的福地!
 
TO:lausunny<br>&nbsp; /////为什么不拿出个详细的解决方案就想得300也太不怎么样了吧,为了得分而骗分。<br>&nbsp; &nbsp;老弟你有沒有搞錯啊!<br>&nbsp; &nbsp;你說我為得分而骗分<br>&nbsp; &nbsp;我不是給你答案了嗎?<br>&nbsp; ////解決的方法非常簡單!<br>&nbsp; ///////就是發送ALT+S然後再發送一個enter!搞定!<br>&nbsp; 難道還要我寫出用什麼語句來發放ALT+S 和ENTER鍵嗎?????<br>&nbsp;老弟:請你說話客氣一點,看請別人回答的內容!<br>&nbsp; &nbsp; 還有不要等別人把所有的程序寫出來給你!OK?自已要多多想想!明白嗎?<br>我今天心情還算好,所以不跟你一般寫識﹒﹒﹒﹒﹒﹒﹒<br>&nbsp;<br>&nbsp;<br>
 
和气生财!
 
TO ljy_17:<br>真的非常感谢你的关注,但我现在实在是没时间玩PROGRAM所以非常需要一套现成的CODE<br>去解决W2K SHUT DOWN 的问题,如果有恳请SHARE出来,给大家一个参考,我愿意拿出所有分数
 
win2000已经提供了api 啦,不过你要用有关机权限的的账号登录,然后才可以关机、<br>注销,当然你也可以远程关机,不过你要先登录到那台机器上,用有权限的账户,<br>我同事用vc6 写了一个,我用d6 写了一个,其实都一样。
 
我也做过一个 有的时候行 有的时候不行<br>因为WINDOW有的时候自已都不能关机<br>谁有好一点的关机源码?Mail Me<br>bamfk1023@163.com
 
后退
顶部