如何判断用户是否是Administrator用户(200分)

  • 主题发起人 主题发起人 厚脸皮
  • 开始时间 开始时间

厚脸皮

Unregistered / Unconfirmed
GUEST, unregistred user!
写一个钩子程序,要求打开某一类文件或程序时判断打开的用户是否是Administrator,如果是可以正常执行,不是就给出警告。请问有没有API可以直接判断用户是否是Administrator用户?
 
getusername
 
我的意思是具有Administrator权限的用户,有可能是user1,user2,user3等,他们都拥有Administrator权限
 
const <br>SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority = (Value: (0, 0, 0, 0, 0, 5));<br>SECURITY_BUILTIN_DOMAIN_RID = $00000020;<br>DOMAIN_ALIAS_RID_ADMINS = $00000220;<br>function IsAdmin: Boolean;<br>var<br> &nbsp;hAccessToken: THandle;<br> &nbsp;ptgGroups: PTokenGroups;<br> &nbsp;dwInfoBufferSize: DWORD;<br> &nbsp;psidAdministrators: PSID;<br> &nbsp;x: Integer;<br> &nbsp;bSuccess: BOOL;<br>begin<br> &nbsp;Result := False;<br> &nbsp;bSuccess := OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True,<br> &nbsp; &nbsp;hAccessToken);<br> &nbsp;if not bSuccess then<br> &nbsp;begin<br> &nbsp; &nbsp;if GetLastError = ERROR_NO_TOKEN then<br> &nbsp; &nbsp;bSuccess := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hAccessToken);<br> &nbsp;end; <br> &nbsp;if bSuccess then<br> &nbsp;begin<br> &nbsp; &nbsp;GetMem(ptgGroups, 1024);<br> &nbsp; &nbsp;bSuccess := GetTokenInformation(hAccessToken, TokenGroups,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ptgGroups, 1024, dwInfoBufferSize);<br> &nbsp; &nbsp;CloseHandle(hAccessToken);<br> &nbsp; &nbsp;if bSuccess then <br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2,<br> &nbsp; &nbsp; &nbsp; &nbsp;SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,<br> &nbsp; &nbsp; &nbsp; &nbsp;0, 0, 0, 0, 0, 0, psidAdministrators);<br> &nbsp; &nbsp; &nbsp;{$R-}<br> &nbsp; &nbsp; &nbsp;for x := 0 to ptgGroups.GroupCount - 1 do<br> &nbsp; &nbsp; &nbsp; &nbsp;if EqualSid(psidAdministrators, ptgGroups.Groups[x].Sid) then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := True;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Break;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;{$R+}<br> &nbsp; &nbsp; &nbsp;FreeSid(psidAdministrators);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;FreeMem(ptgGroups);<br> &nbsp;end;<br>end;
 
好像在Win2003下没有反应,有没有在Win2003下的解决方法
 
有没有人知道呀,上面的程序我在Win2003下运行,只有一个空白窗体呀,高人们给点意见、提示呀,谢谢
 
晕了,你要调用呀!<br>if IsAdmin then<br> &nbsp;showmessage('是管理员用户');
 
Win2000下有效,收藏之<br><br>只有个空白窗体?不明白你的意思。<br>你调用上面这个函数,如果返回值为True,说明是Administrator权限,否则不是。<br><br>你这样调用就知道有没有效果了呀:<br><br>if IsAdmin then<br> &nbsp;messagebox(form1.handle, '该用户有Administrator权限','',mb_ok)<br>else<br> messagebox(form1.handle, '该用户不具有Administrator权限','',mb_ok)<br><br>把它放在一个按钮的click事件里就可以了。
 
大哥调用了,还是空白窗体呀。
 
高人我的解决方法就是放在按钮里调用,看来英雄所见略同呀,哈哈,[:D],先自己吹个牛,哈哈,好了就结了此帖,谢谢各位不厌其烦的帮忙,哈哈。
 
后退
顶部