+ Reply to Thread
Results 1 to 2 of 2

Thread: How to get a user name from specified window handle?

  1. #1
    Senior Member
    Join Date
    Feb 2012
    Posts
    109

    How to get a user name from specified window handle?

    I have below code:

    Code:
    HWND hWnd = FindWindow(NULL, "GUI-NAME") ;
    
    //How to find the user name from this 'hWnd' handle?
    How can I get the user name from this window handle?

    Please help.

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    24
    Following code can be useful to you.

    Code:
    CString GetUserNameFromToken(HANDLE hProcessToken) 
    {
    	CString strUserName ;
    	if (NULL != hProcessToken)
    	{
    		PTOKEN_USER pUserToken  = NULL ;
    		DWORD dwRequiredLength  = 0 ;
    		TCHAR szUserName[256]   = _T("") ;
    		TCHAR szDomainName[256] = _T("") ;
    
    		if (!GetTokenInformation(hProcessToken,
    								TokenUser,
    								(LPVOID) pUserToken,
    								0,
    								&dwRequiredLength
    								)) 
    		{
    			if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
    			{
    				pUserToken = (PTOKEN_USER) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwRequiredLength) ;
    			}
    		}
    
    		if ( (NULL != pUserToken) && GetTokenInformation(hProcessToken,
    														TokenUser,
    														(LPVOID) pUserToken,
    														dwRequiredLength,
    														&dwRequiredLength
    														)) 
    		{
    			SID_NAME_USE sidUser ;
    			DWORD dwUserRequiredLength  = dwRequiredLength ;
    			DWORD dwDomainRequiredLength  = dwRequiredLength ;
    
    			if(LookupAccountSid(NULL , pUserToken->User.Sid, szUserName, &dwUserRequiredLength, szDomainName, &dwDomainRequiredLength, &sidUser))
    			{
    				CString strDomainName = szDomainName ;
    				strUserName = szUserName ;
    				if (!strDomainName.IsEmpty())
    				{
    					strUserName = strDomainName + "\\" + CString(szUserName) ;
    				}
    			}
    		}
    
    		if (NULL != pUserToken)
    		{
    			HeapFree(GetProcessHeap(), 0, (LPVOID)pUserToken) ;
    			pUserToken = NULL ;
    		}
    	}
    
    	return strUserName ;
    }
    
    CString GetUserNameFromWindowHandle(HWND hWnd)
    {
    	CString strUserName ;
    	DWORD dwProcessID = -1 ;
    
    	GetWindowThreadProcessId(hWnd, &dwProcessID) ;
    
    	if (-1 != dwProcessID)
    	{
    		HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessID) ; 
    		if(hProcess != NULL)
    		{
    			HANDLE hProcessToken = NULL ;
    			if(OpenProcessToken(hProcess, TOKEN_QUERY, &hProcessToken))
    			{
    				strUserName = GetUserNameFromToken(hProcessToken) ;
    			}
    
    			CloseHandle(hProcessToken) ;
    			hProcessToken = NULL ;
    
    			CloseHandle(hProcess) ;
    			hProcess = NULL ;
    		}
    	}
    
    	return strUserName ;
    }
    Enjoy!

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts