2013年5月3日星期五

GetCurrentUserAndDomain.Cpp

#include "stdafx.h"

#include <windows.h>

/*
文本就命名为:GetCurrentUserAndDomain.Cpp吧!
made by correy
made at 2013.05.03
QQ:112426112
Email:kouleguan at hotmail dot com
Homepage:http://correy.webs.com

本文摘自:// http://support.microsoft.com/kb/111544/zh-cn
*/

//**********************************************************************
//  FUNCTION:     GetCurrentUserAndDomain - This function looks up the user name and domain name for the user account associated with the calling thread.
//
//  PARAMETERS:   szUser - a buffer that receives the user name
//                pcchUser - the size, in characters, of szUser
//                szDomain - a buffer that receives the domain name
//                pcchDomain - the size, in characters, of szDomain
//
//  RETURN VALUE: TRUE if the function succeeds. Otherwise, FALSE and GetLastError() will return the failure reason.
//
//                If either of the supplied buffers are too small,
//                GetLastError() will return ERROR_INSUFFICIENT_BUFFER and pcchUser and pcchDomain will be adjusted to reflect the required buffer sizes.
//**********************************************************************
BOOL GetCurrentUserAndDomain(PTSTR szUser, PDWORD pcchUser, PTSTR szDomain, PDWORD pcchDomain)
{
    BOOL         fSuccess = FALSE;
    HANDLE       hToken   = NULL;
    PTOKEN_USER  ptiUser  = NULL;
    DWORD        cbti     = 0;
    SID_NAME_USE snu;

    __try
    {    
        if (!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &hToken))// Get the calling thread's access token.
        {
            if (GetLastError() != ERROR_NO_TOKEN) {
                __leave;
            }

            if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {// Retry against process token if no thread token exists.
                __leave;
            }
        }
     
        if (GetTokenInformation(hToken, TokenUser, NULL, 0, &cbti)) { // Obtain the size of the user information in the token.        
            __leave;// Call should have failed due to zero-length buffer.
        } else {      
            if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {// Call should have failed due to zero-length buffer.
                __leave;
            }
        }

        ptiUser = (PTOKEN_USER) HeapAlloc(GetProcessHeap(), 0, cbti);// Allocate buffer for user information in the token.
        if (!ptiUser) {
            __leave;
        }

        if (!GetTokenInformation(hToken, TokenUser, ptiUser, cbti, &cbti)) {// Retrieve the user information from the token.
            __leave;
        }

        if (!LookupAccountSid(NULL, ptiUser->User.Sid, szUser, pcchUser, szDomain, pcchDomain, &snu)) {// Retrieve user name and domain name based on user's SID.
            __leave;
        }

        fSuccess = TRUE;

    } __finally {

        // Free resources.
        if (hToken) {
            CloseHandle(hToken);
        }

        if (ptiUser) {
            HeapFree(GetProcessHeap(), 0, ptiUser);
        }
    }

    return fSuccess;
}

int _tmain(int argc, _TCHAR* argv[])
{
    wchar_t szUser[MAX_PATH] = {0};//估计最大值不是这个。
    wchar_t szDomain[MAX_PATH] = {0};//估计最大值不是这个。这个好像和计算机名一样。

    DWORD d = MAX_PATH;

    //bool b = (szUser, &d, szDomain, &d);//这一行编译通过。但是用汇编写就不会出现这样的未达到预期的错误。
    bool b = GetCurrentUserAndDomain(szUser, &d, szDomain, &d);

    MessageBox(0,szUser,szDomain,0);

    return 0;
}

没有评论:

发表评论