请问用VC如何获得系统的用户名

kunshu 2004-07-27 10:12:07
rt,想获得当前用户的名字,该如何实现,请指点!
...全文
256 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
palmax 2004-08-16
  • 打赏
  • 举报
回复
不错 是读注册表
在HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer下 有一个叫“Logon User Name” 的REG_SZ键值,读出它即可。
taianmonkey 2004-07-28
  • 打赏
  • 举报
回复
BOOL SearchTokenGroupsForSID(VOID)
{
DWORD i, dwSize = 0, dwResult = 0;
HANDLE hToken;
PTOKEN_GROUPS pGroupInfo;
SID_NAME_USE SidType;
char lpName[MAX_NAME];
char lpDomain[MAX_NAME];
BYTE sidBuffer[100];
PSID pSID = (PSID)&sidBuffer;
SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_NT_AUTHORITY;

// Open a handle to the access token for the calling process.
CString str;
if (!OpenProcessToken( GetCurrentProcess(),
TOKEN_QUERY,
&hToken ))
{
str.Format("OpenProcessToken Error %u\n", GetLastError());
AfxMessageBox(str);
return FALSE;
}

// Call GetTokenInformation to get the buffer size.

if(!GetTokenInformation(hToken,
TokenGroups,
NULL,
dwSize,
&dwSize))
{
dwResult = GetLastError();
if( dwResult != ERROR_INSUFFICIENT_BUFFER )
{
str.Format("GetTokenInformation Error %u\n", dwResult );
AfxMessageBox(str);
return FALSE;
}
}

// Allocate the buffer.

pGroupInfo = (PTOKEN_GROUPS) GlobalAlloc( GPTR, dwSize );

// Call GetTokenInformation again to get the group information.

if(! GetTokenInformation(hToken,
TokenGroups,
pGroupInfo,
dwSize,
&dwSize ) )
{
str.Format("GetTokenInformation Error %u\n", GetLastError() );
AfxMessageBox(str);
return FALSE;
}

// Create a SID for the BUILTIN\Administrators group.

if(! AllocateAndInitializeSid( &SIDAuth,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&pSID) )
{
str.Format("AllocateAndInitializeSid Error %u\n", GetLastError() );
AfxMessageBox(str);
return FALSE;
}

// Loop through the group SIDs looking for the administrator SID.

for(i=0; i<pGroupInfo->GroupCount; i++)
{
if ( EqualSid(pSID, pGroupInfo->Groups[i].Sid) )
{

// Lookup the account name and print it.

dwSize = MAX_NAME;

if( !LookupAccountSid( NULL,
pGroupInfo->Groups[i].Sid,
lpName,
&dwSize,
lpDomain,
&dwSize,
&SidType ) )
{
dwResult = GetLastError();
if( dwResult == ERROR_NONE_MAPPED )
strcpy( lpName, "NONE_MAPPED" );
else {
str.Format("LookupAccountSid Error %u\n", GetLastError());
AfxMessageBox(str);
return FALSE;
}
}
str.Format("Current user is a member of the %s\\%s group\n",
lpDomain, lpName );
AfxMessageBox(str);
// Find out if the SID is enabled in the token.
if (pGroupInfo->Groups[i].Attributes & SE_GROUP_ENABLED)
{
AfxMessageBox("The group SID is enabled.\n");
}
else if (pGroupInfo->Groups[i].Attributes &
SE_GROUP_USE_FOR_DENY_ONLY)
{
AfxMessageBox("The group SID is a deny-only SID.\n");
}
else
{
AfxMessageBox("The group SID is not enabled.\n");
}
}
}

if (pSID)
FreeSid(pSID);
if ( pGroupInfo )
GlobalFree( pGroupInfo );
return TRUE;

}
kunshu 2004-07-28
  • 打赏
  • 举报
回复
谢谢大家,我想到了一个方法,搜索系统的注册表来获得当前的用户名应该是可以的。并且应该说相对简单一些。我想得到的是系统的用户名。
suisuibianbian 2004-07-27
  • 打赏
  • 举报
回复
GetUserName()

The GetUserName function retrieves the user name of the current thread. This is the name of the user currently logged onto the system.

broown 2004-07-27
  • 打赏
  • 举报
回复
void CGetProcessUserMfcDlg::GetProcessAuth(CString strPath,long pid)
{
//获得运行进程的用户身份,此处对于8以上的进程没问题,对于8,0进程无法列出(8是Win2000下的,WinXP下为4)
SID_NAME_USE peUse;
HANDLE hp;
HANDLE hToken;
int isok;
char buf[0x400];
char buf1[100];
char buf2[100];
DWORD dwNumBytesRet;
DWORD dwNumBytesRet1;

hp=OpenProcess(0x400, 0, pid);//0x400 is PROCESS_QUERY_INFORMATION
isok=OpenProcessToken(hp, 0x20008, &hToken);//这个0x20008不知道什么,TOKEN_QUERY?
if(isok)
{
isok=GetTokenInformation(hToken, TokenUser, &buf, 0x400, &dwNumBytesRet);
if(isok)
{
dwNumBytesRet=100;
dwNumBytesRet1=100;
isok=LookupAccountSid(NULL, (DWORD *) (*(DWORD *)buf), buf1, &dwNumBytesRet, buf2, &dwNumBytesRet1, &peUse);
if(isok)
{
strPath.Format("Run Auth:%s\\%s", buf2, buf1);
strPathValid = strPath;
}

CloseHandle(hToken);
}
}

CloseHandle(hp);
}
broown 2004-07-27
  • 打赏
  • 举报
回复
http://win32.mvps.org/security/opt_gti.cpp

2,640

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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