惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
Jina AI
Jina AI
博客园 - 司徒正美
雷峰网
雷峰网
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园_首页
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
MyScale Blog
MyScale Blog
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
I
InfoQ
博客园 - Franky
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
Cyberwarzone
Cyberwarzone
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security
P
Privacy & Cybersecurity Law Blog
T
Threatpost
Cloudbric
Cloudbric
D
Docker
M
MIT News - Artificial intelligence
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Vercel News
Vercel News
Martin Fowler
Martin Fowler
J
Java Code Geeks
AWS News Blog
AWS News Blog
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
L
Lohrmann on Cybersecurity
Hacker News: Ask HN
Hacker News: Ask HN
Last Week in AI
Last Week in AI
S
Security @ Cisco Blogs
Help Net Security
Help Net Security
C
Cisco Blogs
V
V2EX
博客园 - 【当耐特】
I
Intezer
爱范儿
爱范儿
F
Fortinet All Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Privacy International News Feed
IT之家
IT之家
L
LINUX DO - 最新话题
B
Blog RSS Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - catlog

曲终人不散 在Windows2000/XP的安全模式下,替换Gina 如何再Win2000/XP下,加入自己的认证? 是走的时候了 VC++ FAQ--VC和MFC的未来,还有Whidbey 如何在Win2000/xp下禁止某些硬件?比如说网卡,CD-ROM.. 明华的EKey 头晕,出个题目考考你 - catlog - 博客园 基于pkcs11的MS CSP的OpenSource实现 生活就是混下去。 用 WB Editor 连接 博客园 的全攻略 一首词 如何用IPHelp取得网卡的详细信息 令人吃惊的完成端口Copy速度! 如何订阅OSR的新闻组? 1:0中国胜科威特 win98下如何控制登陆? 故土难离 Windows 2000的引导过程
取得当前用户的权限(privileges)
catlog · 2004-03-04 · via 博客园 - catlog

在Windows中如何取得用户的权限呢?

在MSDN中有如下的API可以帮助我们做到这些:

1,打开Token,OpenProcessToken得到HANDLE hToken

2.取得GetTokenInformation

3.遍历得到LookupPrivilegeName,LookupPrivilegeDisplayName

以下为示例:

#include <windows.h>
#include <stdio.h>
#pragma hdrstop

void main()
{
 HANDLE hToken;
 LUID setcbnameValue;
 TOKEN_PRIVILEGES tkp;
 DWORD errcod;
 LPVOID lpMsgBuf;
 LPCTSTR msgptr;
 
 UCHAR InfoBuffer[1000];
 PTOKEN_PRIVILEGES ptgPrivileges = (PTOKEN_PRIVILEGES) InfoBuffer;
 DWORD infoBufferSize;
 DWORD privilegeNameSize;
 DWORD displayNameSize;
 char privilegeName[500];
 char displayName[500];
 DWORD langId;
 UINT i;
 
 if ( ! OpenProcessToken( GetCurrentProcess(),
  TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )
 {
  puts( "OpenProcessToken" );
  return;
 }
 
 // ---------------------------------------------------------------------
 // enumerate currently held privs (NOTE: not *enabled* privs, just the
 // ones you _could_ enable as in the last part)
 
 GetTokenInformation( hToken, TokenPrivileges, InfoBuffer,
  sizeof InfoBuffer, &infoBufferSize);
 
 printf( "Account privileges: \n\n" );
 for( i = 0; i < ptgPrivileges->PrivilegeCount; i ++ )
 {
  privilegeNameSize = sizeof privilegeName;
  displayNameSize = sizeof displayName;
  LookupPrivilegeName( NULL, &ptgPrivileges->Privileges[i].Luid,
   privilegeName, &privilegeNameSize );
  LookupPrivilegeDisplayName( NULL, privilegeName,
   displayName, &displayNameSize, &langId );
  printf( "%40s (%s)\n", displayName, privilegeName );
 }
 
 //----------------------------------------------------------------------
 // enable SeTcbPrivilege: lookup, adjust token privs
 
 if ( !LookupPrivilegeValue( NULL, SE_TCB_NAME, &setcbnameValue ) )
 {
  puts( "LookupPrivilegeValue" );
  return;
 }
 
 tkp.PrivilegeCount = 1;
 tkp.Privileges[0].Luid = setcbnameValue;
 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
 
 AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp,
  NULL, NULL );
 
 errcod = GetLastError();
 if ( errcod != ERROR_SUCCESS )
 {
  FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
   FORMAT_MESSAGE_FROM_SYSTEM, NULL, errcod,
   MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
   (LPTSTR) &lpMsgBuf, 0, NULL );
  
  msgptr = (LPCTSTR) lpMsgBuf;
  printf( "err %d: %s\n", errcod, msgptr );
  return;
 }
}

在VC6.0 Sp5 Win200Pro 下编译测试通过。