1个回答
展开全部
CString GetProcessUserName(HANDLE hProcess)
{
HANDLE hToken = NULL;
BOOL bFuncReturn = FALSE;
CString strUserName = _T("");
PTOKEN_USER pToken_User = NULL;
DWORD dwTokenUser = 0;
TCHAR szAccName[MAX_PATH] = {0};
TCHAR szDomainName[MAX_PATH] = {0};
HANDLE hProcessToken = NULL;
if(hProcess != NULL)
{
// 提升本进程的权限
bFuncReturn = ::OpenProcessToken(hProcess,TOKEN_QUERY,&hToken);
if( bFuncReturn == 0) // 失败
return strUserName;
if(hToken != NULL)
{
::GetTokenInformation(hToken, TokenUser, NULL,0L, &dwTokenUser);
if(dwTokenUser>0)
{
pToken_User = (PTOKEN_USER)::GlobalAlloc( GPTR, dwTokenUser );
}
if(pToken_User != NULL)
{
bFuncReturn = ::GetTokenInformation(hToken, TokenUser, pToken_User, dwTokenUser, &dwTokenUser);
}
if(bFuncReturn != FALSE && pToken_User != NULL)
{
SID_NAME_USE eUse = SidTypeUnknown;
DWORD dwAccName = 0L;
DWORD dwDomainName = 0L;
PSID pSid = pToken_User->User.Sid;
bFuncReturn = ::LookupAccountSid(NULL, pSid, NULL, &dwAccName,
NULL,&dwDomainName,&eUse );
if(dwAccName>0 && dwAccName < MAX_PATH && dwDomainName>0 && dwDomainName <= MAX_PATH)
{
bFuncReturn = ::LookupAccountSid(NULL,pSid,szAccName,&dwAccName,
szDomainName,&dwDomainName,&eUse );
}
if( bFuncReturn != 0)
strUserName = szAccName;
}
}
}
if (pToken_User != NULL)
::GlobalFree( pToken_User );
if(hToken != NULL)
::CloseHandle(hToken);
return strUserName;
}
{
HANDLE hToken = NULL;
BOOL bFuncReturn = FALSE;
CString strUserName = _T("");
PTOKEN_USER pToken_User = NULL;
DWORD dwTokenUser = 0;
TCHAR szAccName[MAX_PATH] = {0};
TCHAR szDomainName[MAX_PATH] = {0};
HANDLE hProcessToken = NULL;
if(hProcess != NULL)
{
// 提升本进程的权限
bFuncReturn = ::OpenProcessToken(hProcess,TOKEN_QUERY,&hToken);
if( bFuncReturn == 0) // 失败
return strUserName;
if(hToken != NULL)
{
::GetTokenInformation(hToken, TokenUser, NULL,0L, &dwTokenUser);
if(dwTokenUser>0)
{
pToken_User = (PTOKEN_USER)::GlobalAlloc( GPTR, dwTokenUser );
}
if(pToken_User != NULL)
{
bFuncReturn = ::GetTokenInformation(hToken, TokenUser, pToken_User, dwTokenUser, &dwTokenUser);
}
if(bFuncReturn != FALSE && pToken_User != NULL)
{
SID_NAME_USE eUse = SidTypeUnknown;
DWORD dwAccName = 0L;
DWORD dwDomainName = 0L;
PSID pSid = pToken_User->User.Sid;
bFuncReturn = ::LookupAccountSid(NULL, pSid, NULL, &dwAccName,
NULL,&dwDomainName,&eUse );
if(dwAccName>0 && dwAccName < MAX_PATH && dwDomainName>0 && dwDomainName <= MAX_PATH)
{
bFuncReturn = ::LookupAccountSid(NULL,pSid,szAccName,&dwAccName,
szDomainName,&dwDomainName,&eUse );
}
if( bFuncReturn != 0)
strUserName = szAccName;
}
}
}
if (pToken_User != NULL)
::GlobalFree( pToken_User );
if(hToken != NULL)
::CloseHandle(hToken);
return strUserName;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询