在 WIN2000 下如何获取当前登录用户的用户名和 SID?

ra3 2004-04-19 11:42:47
我知道获得用户名的函数是 GetUserName
如何获取该用户的 SID ?

如:S-1-5-21-1960408961-2111687655-854245398-500
在注册表 HKEY_USERS 键下面
...全文
753 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ra3 2004-04-19
  • 打赏
  • 举报
回复
可以给段代码吗?
shootingstars 2004-04-19
  • 打赏
  • 举报
回复
GetUserName():获得用户名
LookupAccountName():获得SID
ConvertSidToStringSid:将SID转化为字符串。
guothreelove2004 2004-04-19
  • 打赏
  • 举报
回复
我不知道
帮你顶。
ra3 2004-04-19
  • 打赏
  • 举报
回复
我用的是 VC6 我没有找到 sdll.h 这个头文件!

非常感谢 shootingstars(有容乃大,无欲则刚) 和 ANewGuy(新丁)
我现在帐户里只有 20 分了, 下次有分的时候在多给一点好了。

shootingstars 2004-04-19
  • 打赏
  • 举报
回复
Requirements
Client: Included in Windows XP, Windows 2000 Professional.
Server: Included in Windows Server 2003, Windows 2000 Server.
Unicode: Implemented as Unicode and ANSI versions.
Header: Declared in Sddl.h.
Library: Use Advapi32.lib.

ConvertSidToStringSid
The ConvertSidToStringSid function converts a security identifier (SID) to a string format suitable for display, storage, or transmission.

To convert the string-format SID back to a valid, functional SID, call the ConvertStringSidToSid function.


BOOL ConvertSidToStringSid(
PSID Sid,
LPTSTR* StringSid
);

ANewGuy 2004-04-19
  • 打赏
  • 举报
回复
这个函数在Windows NT4.0或更早的版本中是不受支持的,可以用以下的代码代替:
BOOL GetTextualSid(
PSID pSid, // binary SID
LPTSTR TextualSid, // buffer for Textual representation of SID
LPDWORD lpdwBufferLen // required/provided TextualSid buffersize
)
{
PSID_IDENTIFIER_AUTHORITY psia;
DWORD dwSubAuthorities;
DWORD dwSidRev=SID_REVISION;
DWORD dwCounter;
DWORD dwSidSize;

// Validate the binary SID.

if(!IsValidSid(pSid)) return FALSE;

// Get the identifier authority value from the SID.

psia = GetSidIdentifierAuthority(pSid);

// Get the number of subauthorities in the SID.

dwSubAuthorities = *GetSidSubAuthorityCount(pSid);

// Compute the buffer length.
// S-SID_REVISION- + IdentifierAuthority- + subauthorities- + NULL

dwSidSize=(15 + 12 + (12 * dwSubAuthorities) + 1) * sizeof(TCHAR);

// Check input buffer length.
// If too small, indicate the proper size and set the last error.

if (*lpdwBufferLen < dwSidSize)
{
*lpdwBufferLen = dwSidSize;
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return FALSE;
}

// Add 'S' prefix and revision number to the string.

dwSidSize=wsprintf(TextualSid, TEXT("S-%lu-"), dwSidRev );

// Add a SID identifier authority to the string.

if ( (psia->Value[0] != 0) || (psia->Value[1] != 0) )
{
dwSidSize+=wsprintf(TextualSid + lstrlen(TextualSid),
TEXT("0x%02hx%02hx%02hx%02hx%02hx%02hx"),
(USHORT)psia->Value[0],
(USHORT)psia->Value[1],
(USHORT)psia->Value[2],
(USHORT)psia->Value[3],
(USHORT)psia->Value[4],
(USHORT)psia->Value[5]);
}
else
{
dwSidSize+=wsprintf(TextualSid + lstrlen(TextualSid),
TEXT("%lu"),
(ULONG)(psia->Value[5] ) +
(ULONG)(psia->Value[4] << 8) +
(ULONG)(psia->Value[3] << 16) +
(ULONG)(psia->Value[2] << 24) );
}

// Add SID subauthorities to the string.
//
for (dwCounter=0 ; dwCounter < dwSubAuthorities ; dwCounter++)
{
dwSidSize+=wsprintf(TextualSid + dwSidSize, TEXT("-%lu"),
*GetSidSubAuthority(pSid, dwCounter) );
}

return TRUE;
}
ra3 2004-04-19
  • 打赏
  • 举报
回复
我是直接输出的:
cout << username << endl;
cout << sidlen << endl;
cout << domainbuf << endl;

没有MSDN帮助里面没有 ConvertSidToStringSid 这个函数或结构啊!
shootingstars 2004-04-19
  • 打赏
  • 举报
回复
你是如何打印的sid?SID不是一个字符串,它是一个结构体,如果你想将它打印出来,你还需要使用ConvertSidToStringSid将sidbuf中的数据转换为字符串才能显示。
ra3 2004-04-19
  • 打赏
  • 举报
回复
不行啊,没有 sidbuf 的结果。
这是在 2000 下运行的结果:
username : Administrator
sidbuf :
domainbuf : KING
shootingstars 2004-04-19
  • 打赏
  • 举报
回复
char username[100];
GetUserName(username);

----------------------------------
char sidbuf[100];
DWORD sidlen = 100;
char domainbuf[100];
DWORD domainbuflen = 100;
SID_NAME_USE sidnameuse;
BOOL LookupAccountName(
NULL, // 当前计算机
username, // GetUserName获得的结果
(PSID)sidbuf,
&sidlen,
domainbuf,
&domainbuflen,
&sidnameuse
);

随手写的,没有考虑错误处理。
ra3 2004-04-19
  • 打赏
  • 举报
回复
LookupAccountName 的参数不会写?
老大帮个忙好吗,多谢了!

在注册表中取这个方法不行啊, 怎么判断哪个是当前用户的 SID 号啊?
1HelloWorld 2004-04-19
  • 打赏
  • 举报
回复
好像取注册表是这个RegOpenKeyEx
1HelloWorld 2004-04-19
  • 打赏
  • 举报
回复
既然注册表中有,那么从注册表取就可以了。

16,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧