K
kindly
Unregistered / Unconfirmed
GUEST, unregistred user!
我想实现一个功能,就是让程序在执行时用其他用户身份登陆<br>举例来说,当前用户是guest,我想让程序以administrator帐号运行,(知道密码)<br>我查到了logonuser可以实现登陆(我的理解),但是怎么试都不行,MSDN上说如果要<br>logonuser必须有SE_TCB_NAME权限,我加了这个权限还是不行,<br>我的代码如下,请高手指点!<br>procedure AdjustToken;<br>var<br> hdlProcessHandle : Cardinal;<br> hdlTokenHandle : Cardinal;<br> tmpLuid : Int64;<br> tkp : TOKEN_PRIVILEGES;<br> tkpNewButIgnored : TOKEN_PRIVILEGES;<br> lBufferNeeded : Cardinal;<br>begin<br> hdlProcessHandle := GetCurrentProcess;<br> OpenProcessToken(hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES or<br> TOKEN_QUERY), hdlTokenHandle);<br><br> // Get the LUID for shutdown privilege.<br> if not LookupPrivilegeValue('', SE_TCB_NAME, tmpLuid) then showmessage('LookupPrivilegeValue');<br><br> tkp.PrivilegeCount := 1; // ' One privilege to set<br> tkp.Privileges[0].Luid := tmpLuid;<br> tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;<br> // Enable the shutdown privilege in the access token of this process.<br><br> if not AdjustTokenPrivileges(hdlTokenHandle, False,<br> tkp, SizeOf(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded) then showmessage('AdjustToken');<br>end;<br><br>procedure LogonAsUser(AUsername,APassword:string);<br>var<br> tkn : Cardinal;<br>begin<br> AdjustToken;<br> writeln(LogonUser(PChar(AUsername),<br> 'soj',<br> PChar(APassword),<br> LOGON32_LOGON_INTERACTIVE,<br> LOGON32_PROVIDER_DEFAULT,<br> tkn)); //always be False , why?<br> writeln(ImpersonateLoggedOnUser(tkn)); //always be False, why?<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> LogonAsUser('guest','guest');<br>end;<br><br>运行在w2kp下,总是不能通过,我搞了N天了...