编译时的问题overloaded!求救???(100分)

  • 主题发起人 主题发起人 Colinyy
  • 开始时间 开始时间
C

Colinyy

Unregistered / Unconfirmed
GUEST, unregistred user!
在WinXP下尝试控制关闭WinXP.编译时出现了以下问题:<br>[Error] AidKit_Unit1.pas(521): Ambiguous overloaded call to 'AdjustTokenPrivileges'<br>请问是为什么?如何解决???<br>谢谢!!!<br>使用以下程序段:<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>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp; &nbsp;AdjustToken;<br>&nbsp; &nbsp; &nbsp;ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), $FFFF);<br>end;<br>
 
windows应当是uses的第一个单元<br>看一下你的uses中是不是这样<br>或者是别的单元中有同名函数。
 
here is an example posted originally by Lars Marquart, you can use it:<br><br>function GetShutdownPrivilege:Boolean;<br>const<br>&nbsp; SHN &nbsp; &nbsp;: PChar = 'SeShutdownPrivilege';<br>&nbsp; EMP &nbsp; &nbsp;: PChar = '';<br>var<br>&nbsp; hToken : THandle;<br>&nbsp; tkp,p &nbsp;: TTokenPrivileges;<br>&nbsp; RetLen : DWORD;<br>&nbsp; Err &nbsp; &nbsp;: DWord;<br>begin<br>// Get a token for this process.<br>&nbsp; if not OpenProcessToken(GetCurrentProcess,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TOKEN_ADJUST_PRIVILEGES or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TOKEN_QUERY,hToken) &nbsp; &nbsp; &nbsp; &nbsp;then begin<br>&nbsp; &nbsp; Result := False; // 'Error: OpenProcessToken:<br>'+IntToStr(GetLastError)<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br>// Get the LUID for the shutdown privilege.<br>&nbsp; if not LookupPrivilegeValue(EMP,SHN,tkp.Privileges[0].Luid) then begin<br>&nbsp; &nbsp; Result := False; // 'Error: LookupPrivilegeValue:<br>'+IntToStr(GetLastError)<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br>&nbsp; tkp.PrivilegeCount &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; := 1; // One privilege to set<br>&nbsp; tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;<br>// Get the shutdown privilege for this process.<br>&nbsp; AdjustTokenPrivileges(<br>hToken,False,tkp,SizeOf(TTokenPrivileges),p,RetLen);<br>// Cannot test the return value of AdjustTokenPrivileges.<br>&nbsp; Err := GetLastError;<br>&nbsp; if Err&lt;&gt;ERROR_SUCCESS then begin<br>// &nbsp; &nbsp;Err=1300: 'You do not have the right to shut the system down'<br>// &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp;'Error: AdjustTokenPrivileges: '+IntToStr(Err)<br>&nbsp; &nbsp; Result := False;<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br>// Current user have privileges to shutdown the system.<br>&nbsp; Result := True;<br>end;<br><br><br><br><br>I've missed that for some reason (this main unit was created before me)<br>in the interface part instead use just Windows unit, it was used two units<br>WinTypes and WinProcs. It was the reason why "Ambiguous" error occured.
 
以上方法的试过了,但问题仍然没有解决.<br>请再讲解详细的或者还有别的方法吗?<br>可以再加分!!!
 
mypine的方法行得通,我以前也用过.<br>但在我现在搞的程序中编译时出现了overloaded<br>也不知道是为什么?如果有相同情况者请指教!!!<br><br>先谢了mypine给你50分吧.
 
多人接受答案了。
 
InitiateSystemShutdownEx
 
后退
顶部