GetTokenInformation,TOKEN_OWNER,和LookupAccountSidA

时间:2016-05-03 11:24:03

标签: winapi

我正在尝试使用这些功能来获取运行该进程的用户名。这是代码:

#include <windows.h>
#include <psapi.h>

using namespace std;

int main()    
{
    HANDLE hProcess = GetCurrentProcess();
    HANDLE hToken;
    OpenProcessToken(hProcess, TOKEN_QUERY, &hToken);
    DWORD len = 0;
    GetTokenInformation(hToken, TokenOwner, NULL, 0, &len);
    PTOKEN_OWNER to = (PTOKEN_OWNER)LocalAlloc(LPTR, len);
    GetTokenInformation(hToken, TokenOwner, (LPVOID)&to, len, &len);
    char nameUser[50];
    DWORD nameUserSize = sizeof(nameUser);
    SID_NAME_USE snu;
    cout << "work";
    LookupAccountSidA(NULL, to->Owner, nameUser, &nameUserSize, NULL, NULL, &snu);
    cout << "not work";
    cout << nameUser << endl;
    LocalFree(to);
    CloseHandle(hToken);
    CloseHandle(hProcess);
    return 0;
}

我在调用LookupAccountSidA()的那一刻休息了。但我怀疑问题是由于我对这个函数的参数错了,或者我错误地创建了一个结构TOKEN_OWNER。第二种选择更有可能,因为我不明白这里是如何分配内存的。

我从MSDN上拿了一个例子并根据我的需要重写了它,但没有任何作用。以下是MSDN的一些示例:

Getting the Logon SID in C++

Searching for a SID in an Access Token in C++

P.S。俄语:

https://ru.stackoverflow.com/questions/519602/

1 个答案:

答案 0 :(得分:3)

您对GetTokenInformation()的第二次调用是在第3个参数中传递了错误的内存地址。您正在传递to变量本身的内存地址,因此您要求GetTokenInformation()写入周围的堆栈空间。您需要传递变量所指向的内存地址(已分配的TOKEN_OWNER结构),因此请删除&运算符(以及类型转换,这是不必要的):< / p>

GetTokenInformation(hToken, TokenOwner, to, len, &len);

您根本没有进行任何错误处理,因此您不知道是否正在成功分配内存,或者是否有任何API函数失败。

请改为尝试:

#include <windows.h>
#include <psapi.h>

using namespace std;

int main()    
{
    DWORD dwError;

    HANDLE hToken;
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
    {
        dwError = GetLastError();
        cerr << "OpenProcessToken failed, error " << dwError;
        return 0;
    }

    DWORD len = 0;
    if (!GetTokenInformation(hToken, TokenOwner, NULL, 0, &len))
    {
        dwError = GetLastError();
        if (dwError != ERROR_INSUFFICIENT_BUFFER) 
        {
            cerr << "GetTokenInformation failed, error " << dwError;
            CloseHandle(hToken);
            return 0;
        }
    }

    PTOKEN_OWNER to = (PTOKEN_OWNER) LocalAlloc(LPTR, len);
    if (!to)
    {
        dwError = GetLastError();
        cerr << "LocalAlloc failed, error " << dwError;
        CloseHandle(hToken);
        return 0;
    }

    if (!GetTokenInformation(hToken, TokenOwner, to, len, &len))
    {
        dwError = GetLastError();
        cerr << "GetTokenInformation failed, error " << dwError;
        LocalFree(to);
        CloseHandle(hToken);
        return 0;
    }

    char nameUser[256] = {0};
    char domainName[256] = {0};
    DWORD nameUserLen = 256;
    DWORD domainNameLen = 256;
    SID_NAME_USE snu;

    if (!LookupAccountSidA(NULL, to->Owner, nameUser, &nameUserLen, domainName, &domainNameLen, &snu))
    {
        dwError = GetLastError();
        cerr << "LookupAccountSid failed, error " << dwError;
        LocalFree(to);
        CloseHandle(hToken);
        return 0;
    }

    cout << domainName << '/' << nameUser << endl;

    LocalFree(to);
    CloseHandle(hToken);

    return 0;
}