请问:使用AdjustTokenPrivileges和OpenProcessToken两个函数时参数怎样设置?(100分)

Y

yylg

Unregistered / Unconfirmed
GUEST, unregistred user!
我找了一个关闭Windows(包括NT和Win2000)的例程,可在编译时总是出现两个错误,我曾<br>反复修改其参数,可是总不得要领,希望坛内高手予以赐教.<br><br>例程及错误代码为:<br>function SetPrivilege (sPrivilegeName: string; bEnabled: Boolean) : Boolean;<br>var<br>&nbsp; TPPrev,<br>&nbsp; TP &nbsp; &nbsp; &nbsp; : TTokenPrivileges;<br>&nbsp; Token &nbsp; &nbsp;: cardinal;<br>&nbsp; dwRetLen : DWORD;<br>begin<br>&nbsp; result := False;<br>&nbsp; OpenProcessToken (GetCurrentProcess,<br>&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; @Token); <br>&nbsp; // 此处出现错误提示为: [Error]: Types of actual and formal var parameters must be identical<br>&nbsp;<br>&nbsp; TP.PrivilegeCount := 1;<br>&nbsp; if LookupPrivilegeValue (nil, PChar (sPrivilegeName), TP.Privileges[0].LUID) then<br>&nbsp; begin<br>&nbsp; &nbsp; if bEnabled then<br>&nbsp; &nbsp; &nbsp; TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; TP.Privileges[0].Attributes := 0;<br><br>&nbsp; &nbsp; dwRetLen := 0;<br>&nbsp; &nbsp; result := AdjustTokenPrivileges (<br>&nbsp; &nbsp; Token,<br>&nbsp; &nbsp; False,<br>&nbsp; &nbsp; TP,<br>&nbsp; &nbsp; SizeOf (TPPrev),<br>&nbsp; &nbsp; TPPrev,<br>&nbsp; &nbsp; dwRetLen);<br>// 此处出现错误提示为: [Error]: There is no overloaded version of 'AdjustTokenPrivileges' that can be called with these arguments<br><br>&nbsp; end;<br>&nbsp; CloseHandle (Token);<br>end;<br><br>function WinExit (iFlags: integer) : Boolean;<br>begin<br>&nbsp; result := true;<br>&nbsp; if SetPrivilege ('SeShutdownPrivilege', true) then<br>&nbsp; begin<br>&nbsp; &nbsp; if (not ExitWindowsEx (iFlags, 0)) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; // handle errors...<br>&nbsp; &nbsp; &nbsp; result := False<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; SetPrivilege ('SeShutdownPrivilege', False)<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; // handle errors...<br>&nbsp; &nbsp; result := False<br>&nbsp; end<br>end;<br>
 
1、不是用@Token,而是直接 Token<br>2、
 
OpenProcessToken函数原型中最后一个函数是个指针,我不理解为什么能直接用cardinal;<br>另外,将@Token的@去掉之后,AdjustTokenPrivileges的调用又出现了<br>[Error] : Ambiguous overloaded call to 'AdjustTokenPrivileges' 的错误。<br>还需请教。<br>
 
多谢关注,虽然问题没有解答好,还是要奉送50两大银。
 
在delphi,最后一个参数它翻译为var ReturnLength: DWORD<br>var参数,相当于c的引用 &nbsp;DWORD&amp; ReturnLength<br>引用,在内部和指针又是相同的
 
倒数第二个参数TPPrev改为 @TPPrev
 
现在是:<br>&nbsp; &nbsp; result := AdjustTokenPrivileges (<br>&nbsp; &nbsp; Token,<br>&nbsp; &nbsp; False,<br>&nbsp; &nbsp; TP, &nbsp; &nbsp; &nbsp; //下面的加了,难道这个不加吗?<br>&nbsp; &nbsp; SizeOf (TPPrev),<br>&nbsp; &nbsp; @TPPrev, &nbsp;//原来没有@<br>&nbsp; &nbsp; dwRetLen);<br>&nbsp;加上@后还是有Ambiguous overloaded call to 'AdjustTokenPrivileges' 的错误。<br>&nbsp;老师,怎么才能编译通过呢?
 
改两个地方即可!<br>1 &nbsp;Token :THandle;<br>2 &nbsp;OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,Token);
 
&gt;加上@后还是有Ambiguous overloaded call to 'AdjustTokenPrivileges' 的错误<br>它还有更详细的信息,是哪个参数Ambiguous呢
 
倒数第二个参数这样写:<br>PTokenPrivileges( @TPPrev )<br>
 
现在的情况是: &nbsp; &nbsp; &nbsp;<br>行号 &nbsp; &nbsp;程序<br>.........................................<br>369: &nbsp; &nbsp;result := AdjustTokenPrivileges (<br>370: &nbsp; &nbsp;Token,<br>371: &nbsp; &nbsp;False,<br>372: &nbsp; &nbsp;TP,<br>373: &nbsp; &nbsp;SizeOf (TPPrev),<br>374: &nbsp; &nbsp;PTokenPrivileges( @TPPrev ),<br>375: &nbsp; &nbsp;dwRetLen);<br><br>错误信息: (为了发现错误,我将每个参数都单独占一行,出错指示条在第375行,难道是最后一个参数有问题?)<br>[Error] msysdc.pas(375): Ambiguous overloaded call to 'AdjustTokenPrivileges'<br>[Fatal Error] sysdc.dpr(6): Could not compile used unit 'msysdc.pas'<br><br>就这一个关于参数重载的错误,怎么做都不通。
 
你的是delphi几啊,我的delphi5是通过的<br>你输入 AdjustTokenPrivileges ( <br>然后等待code insight弹出参数的提示,看有哪些参数是重载的<br>我的版本的delphi,仅有倒数第二个参数是不同的<br>即使不强制指定类型, 用了@TPPrev 也是可以通过的
 
已经测试通过,没问题!<br><br>function SetPrivilege (sPrivilegeName: string; bEnabled: Boolean) : Boolean;<br>var<br>&nbsp; TPPrev,<br>&nbsp; TP &nbsp; &nbsp; &nbsp; : TTokenPrivileges;<br>&nbsp; Token :THandle;<br>&nbsp; dwRetLen : DWORD;<br>begin<br>&nbsp; result := False;<br>&nbsp; OpenProcessToken (GetCurrentProcess,<br>&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; Token); <br>&nbsp; TP.PrivilegeCount := 1;<br>&nbsp; if LookupPrivilegeValue (nil, PChar (sPrivilegeName), TP.Privileges[0].LUID) then<br>&nbsp; begin<br>&nbsp; &nbsp; if bEnabled then<br>&nbsp; &nbsp; &nbsp; TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; TP.Privileges[0].Attributes := 0;<br>&nbsp; &nbsp; dwRetLen := 0;<br>&nbsp; &nbsp; result := AdjustTokenPrivileges (<br>&nbsp; &nbsp; Token,<br>&nbsp; &nbsp; False,<br>&nbsp; &nbsp; TP,<br>&nbsp; &nbsp; SizeOf (TPPrev),<br>&nbsp; &nbsp; TPPrev,<br>&nbsp; &nbsp; dwRetLen);<br>&nbsp; end;<br>&nbsp; CloseHandle (Token);<br>end;
 
我用的是delphi6,总是通不过;刚才换了5之后,编译就通过了。为什么呢?<br><br>多日来蒙两位教头耐心赐教,不胜感激,100分送两位,望笑纳。
 
顶部