2015. 11. 18. 11:34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
bool get_sid_from_session_id(__out LPTSTR &string_sid, __in DWORD session_number)
{
    bool result = false;
    HANDLE current_user_handle = nullptr;
 
    do
    {
        if (FALSE == WTSQueryUserToken(session_number, current_user_handle))
        {            
            break;
        }
 
        DWORD dwSize = 0;
        BYTE * token_information = nullptr;
        if (FALSE == GetTokenInformation(current_user_handle, TokenUser, 
            static_cast<LPVOID>(token_information), 0, &dwSize))
        {
            if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
            {
                token_information = new(std::nothrow) BYTE[dwSize];
                if(nullptr == token_information)
                {
                    break;
                }
 
                ::memset(token_information, 0, dwSize);
            }
            else
            {                
                break;
            }
        }
 
        PTOKEN_USER token_user = reinterpret_cast<PTOKEN_USER>(token_information);
        if (FALSE == GetTokenInformation(current_user_handle, TokenUser, 
            static_cast<LPVOID>(token_user), dwSize, &dwSize))
        {            
            break;
        }
 
        if (FALSE == ConvertSidToStringSid(token_user->User.Sid, &string_sid))
        {            
            break;
        }
 
        if (nullptr != string_sid)
        {
            result = true;
        }
 
    } while (false);
 
    if (nullptr != current_user_handle)
        CloseHandle(current_user_handle);
    current_user_handle = nullptr;
 
    return result;
}
cs


__out 의 string_sid는 ::LocalFree를 해야함.

https://msdn.microsoft.com/ko-kr/library/windows/desktop/aa376399%28v=vs.85%29.aspx



'긁적긁적..' 카테고리의 다른 글

MS driver sample bsod  (0) 2016.02.26
vmware host/guest 이동 할때 numlock 토글 문제  (0) 2016.02.18
office군 제어 관련 자료  (0) 2016.02.03
__debugbreak enable/disable  (0) 2015.12.16
Posted by GRoovAllstar