检查进程用户是否是管理员c++

发布于 2024-07-24 07:07:07 字数 57 浏览 3 评论 0原文

我想获取进程的用户名并检查它是否是本地管理员。 或者直接检查当前procees用户是否是本地管理员

I want to get the process's user name and check if it is a local administrator . Or check directly if the current procees user is a local administrator

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兮颜 2024-07-31 07:07:07

使用 GetUserName() 获取当前用户名,然后调用 NetUserGetInfo() 以及您刚刚获得的服务器名称(本地为 NULL)和用户名。 向其传递 USER_INFO_1 结构,然后访问结构中的usri1_priv。 如果该值为USER_PRIV_ADMIN,那么您就会知道该用户名是管理员。

Get the current username with GetUserName(), then call NetUserGetInfo() with the server name (NULL for local) and username you just got. Pass it a USER_INFO_1 structure, and then access usri1_priv in the structure. If the value is USER_PRIV_ADMIN, then you'll know that the username is an admin.

鹿港巷口少年归 2024-07-31 07:07:07

在 Windows XP SP3、Windows 7 32 位和 64 位上使用管理员用户和非管理员用户进行了测试。
代码从等效的 C# 移植并使用 ATL Windows 安全包装类。

#include <atlbase.h>
#include <atlsecurity.h>

// The function returns true if the user who is running the
// application is a member of the Administrators group,
// which does not necessarily mean the process has admin privileges.
bool IsAdministrator(HRESULT &rHr)
{
    bool bIsAdmin = false;

    try
    {
        // Open the access token of the current process.
        ATL::CAccessToken aToken;
        if (!aToken.GetProcessToken(TOKEN_QUERY))
        {
            throw MAKE_SCODE(SEVERITY_ERROR, FACILITY_WIN32,
                ::GetLastError());
        }


        // Query for the access token's group information.
        ATL::CTokenGroups groups;
        if (!aToken.GetGroups(&groups))
        {
            throw MAKE_SCODE(SEVERITY_ERROR, FACILITY_WIN32,
                ::GetLastError());
        }

        // Iterate through the access token's groups
        // looking for a match against the builtin admins group.
        ATL::CSid::CSidArray groupSids;
        ATL::CAtlArray<DWORD> groupAttribs;
        groups.GetSidsAndAttributes(&groupSids, &groupAttribs);
        for (UINT i = 0; !bIsAdmin && i < groupSids.GetCount(); ++i)
        {
            bIsAdmin = groupSids.GetAt(i) == ATL::Sids::Admins();
        }
        rHr = S_OK;
    }
    catch (HRESULT hr)
    {
        rHr = hr;
    }

    return bIsAdmin;
}

Tested on Windows XP SP3, Windows 7 32 bit and 64 bit with admin user and non-admin user.
Code ported from equivalent C# and uses ATL windows security wrapper classes.

#include <atlbase.h>
#include <atlsecurity.h>

// The function returns true if the user who is running the
// application is a member of the Administrators group,
// which does not necessarily mean the process has admin privileges.
bool IsAdministrator(HRESULT &rHr)
{
    bool bIsAdmin = false;

    try
    {
        // Open the access token of the current process.
        ATL::CAccessToken aToken;
        if (!aToken.GetProcessToken(TOKEN_QUERY))
        {
            throw MAKE_SCODE(SEVERITY_ERROR, FACILITY_WIN32,
                ::GetLastError());
        }


        // Query for the access token's group information.
        ATL::CTokenGroups groups;
        if (!aToken.GetGroups(&groups))
        {
            throw MAKE_SCODE(SEVERITY_ERROR, FACILITY_WIN32,
                ::GetLastError());
        }

        // Iterate through the access token's groups
        // looking for a match against the builtin admins group.
        ATL::CSid::CSidArray groupSids;
        ATL::CAtlArray<DWORD> groupAttribs;
        groups.GetSidsAndAttributes(&groupSids, &groupAttribs);
        for (UINT i = 0; !bIsAdmin && i < groupSids.GetCount(); ++i)
        {
            bIsAdmin = groupSids.GetAt(i) == ATL::Sids::Admins();
        }
        rHr = S_OK;
    }
    catch (HRESULT hr)
    {
        rHr = hr;
    }

    return bIsAdmin;
}
生生不灭 2024-07-31 07:07:07

假设您使用的是 Window 操作系统,有一个 shell 函数:IsUserAnAdmin

请参阅 MSDN 文章

这篇文章确实建议滚动您自己的函数,使用 CheckTokenMembership。 甚至还有一个代码示例可以帮助您。

Presuming you're on a Window OS there's a shell function: IsUserAnAdmin

See MSDN article

This article does suggest rolling your own function though, use CheckTokenMembership. There is even a code example to help you along.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文