Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ WindowsIdentity.GetCurrent().IsSystem在C++;_C++_Windows - Fatal编程技术网

C++ WindowsIdentity.GetCurrent().IsSystem在C++;

C++ WindowsIdentity.GetCurrent().IsSystem在C++;,c++,windows,C++,Windows,我需要知道当前进程是否作为系统运行。在C++中,我使用 Windows WordStase.GETCurrnor()。ISStase来做这件事,C++中的等价项是什么? 我尽量避免比较用户名,因为不同的操作系统对系统帐户有不同的用户名。我根据Eryk的想法创建了一个示例,它可以工作: BOOL IsSystem() { HANDLE hToken = NULL; BOOL result = false; TOKEN_USER *tokenUser = NULL;

我需要知道当前进程是否作为系统运行。在C++中,我使用<代码> Windows WordStase.GETCurrnor()。ISStase<代码>来做这件事,C++中的等价项是什么?
我尽量避免比较用户名,因为不同的操作系统对系统帐户有不同的用户名。

我根据Eryk的想法创建了一个示例,它可以工作:

BOOL IsSystem()
{
    HANDLE hToken = NULL;
    BOOL result = false;
    TOKEN_USER *tokenUser = NULL;
    DWORD dwLength = 0;

    if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken) == 0)
    {
        DbgPrint("OpenProcessToken(): %d", GetLastError());
        goto cleanup;
    }

    if (GetTokenInformation(hToken, TokenUser, (LPVOID) tokenUser, 0, &dwLength) == 0)
    {
        if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
        {
            DbgPrint("GetTokenInformation(): %d", GetLastError());
            goto cleanup;
        }

        tokenUser = (TOKEN_USER *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwLength);
        if (tokenUser == NULL)
        {
            goto cleanup;
        }

        if (GetTokenInformation(hToken, TokenUser, (LPVOID) tokenUser, dwLength, &dwLength) == 0)
        {
            DbgPrint("GetTokenInformation(): %d", GetLastError());
            goto cleanup;
        }

        result = IsWellKnownSid(tokenUser->User.Sid, WinLocalSystemSid);
    }

cleanup:
    if (tokenUser != NULL)
    {
        HeapFree(GetProcessHeap(), NULL, tokenUser);
    }

    return result;
}

下面是一个可能的调用序列:,:
TokenUser
,:
WinLocalSystemSid
@ErykSun-TokenUser包含SID_和_属性而不是PSID结构,有什么想法吗?如果指向
tokenu用户
记录的指针是
pTokenUser
,然后PSID是
pTokenUser->User.Sid
。这个
goto
看起来没有使用
goto
。你知道,比如用
if
else
代替。或者例如,释放资源(例如句柄、堆分配)的退出/清除标签在C中是正常的,但不是C++。也就是说,尽管被标记为C++,除了使用<代码> false <代码>之外,此代码只是C。