WinAPI NetUserGetInfo() 在 Active Directory 域上失败并显示 NERR_UserNotFound 错误代码

发布于 2024-12-16 12:37:44 字数 1373 浏览 0 评论 0原文

我正在从本地服务应用程序运行以下代码。目的是在调用 LoadUserProfile() 加载该用户的配置文件之前获取用户配置文件的路径,然后再调用 CreateProcessAsUser() 代表该用户运行用户模式进程。

请注意,这个问题与 LoadUserProfile() 或 CreateProcessAsUser() 无关。

发生的事情是这样的。当下面的代码在属于 Active Directory 域的 Windows XP w/SP3 上运行时,单个用户通过本地控制台登录(下面使用该用户的会话 ID),NetUserGetInfo() API 将失败。另请注意,它在任何其他情况下都可以正常工作:

//'dwSessID' = session ID of the user to retrieve a user profile path for
LPTSTR pUserName = NULL;
DWORD dwcbSzUserName = 0;
if(!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessID, WTSUserName, &pUserName, &dwcbSzUserName))
{
    //Error
    return false;
}

USER_INFO_4* pUI4 = NULL;
DWORD dwNetStatus;
if((dwNetStatus = NetUserGetInfo(NULL, pUserName, 4, (BYTE**)&pUI4)) == NERR_Success)
{
    PROFILEINFO pfi = {0};
    pfi.dwSize = sizeof(pfi);
    pfi.lpUserName = pUserName;
    pfi.dwFlags = PI_NOUI;
    pfi.lpProfilePath = pUI4->usri4_profile;

    LoadUserProfile(hToken, &pfi);

    //And so on
}
else
{
    //On that specific machine I get here with 'dwNetStatus' = 2221, 
    //or NERR_UserNotFound, that according to MSDN is 
    //"The user name could not be found."
    //Also note that GetLastError is not used for this API.
}

有人可以建议为什么 NetUserGetInfo() 在该特定计算机上失败,以及如何修复此代码吗?

附言。我知道 NetUserGetInfo 的 MSDN 指出 Active Directory 域上的 ACL 可能存在问题,但它没有指定如何设置...

I'm running the following piece of code from a local service application. The purpose is to obtain the path to a user's profile before calling LoadUserProfile() to load that user's profile before calling CreateProcessAsUser() to run a user-mode process on behalf of that user.

Note that this question is not about LoadUserProfile(), or CreateProcessAsUser().

What happens is this. When the code below is run on Windows XP w/SP3 that is a part of the Active Directory domain, with a single user logged in via a local console (that user's session ID is used below) the NetUserGetInfo() API fails. Also note that it works fine in any other circumstance:

//'dwSessID' = session ID of the user to retrieve a user profile path for
LPTSTR pUserName = NULL;
DWORD dwcbSzUserName = 0;
if(!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessID, WTSUserName, &pUserName, &dwcbSzUserName))
{
    //Error
    return false;
}

USER_INFO_4* pUI4 = NULL;
DWORD dwNetStatus;
if((dwNetStatus = NetUserGetInfo(NULL, pUserName, 4, (BYTE**)&pUI4)) == NERR_Success)
{
    PROFILEINFO pfi = {0};
    pfi.dwSize = sizeof(pfi);
    pfi.lpUserName = pUserName;
    pfi.dwFlags = PI_NOUI;
    pfi.lpProfilePath = pUI4->usri4_profile;

    LoadUserProfile(hToken, &pfi);

    //And so on
}
else
{
    //On that specific machine I get here with 'dwNetStatus' = 2221, 
    //or NERR_UserNotFound, that according to MSDN is 
    //"The user name could not be found."
    //Also note that GetLastError is not used for this API.
}

Can some suggest why can NetUserGetInfo() fail on that particular machine, and how to fix this code?

PS. I know that MSDN for NetUserGetInfo states that there might be issues with a ACL on Active Directory domain, but it doesn't specify how to set one...

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

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

发布评论

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

评论(3

旧情勿念 2024-12-23 12:37:44

如果我阅读 NetUserGetInfo 的文档,用于您编码的数据的信息级别 4 。它被写为 Level 4 返回有关用户帐户的详细信息和附加属性。 此级别仅在服务器上有效。据我了解,这不是你的情况。您是否验证 WTSQuerySessionInformation 返回的 pUserName 值。

If I read the documentation for NetUserGetInfo, for the information level of the data you code 4 . It's written Level 4 Return detailed information and additional attributes about the user account. This level is valid only on servers. As far as I understand it's not your case. Do you verify the value of pUserName returned by WTSQuerySessionInformation.

長街聽風 2024-12-23 12:37:44

正如 JPBlanc 所说,级别 4 的 NetUserGetInfo 仅在服务器上有效。

另一个问题是您检索登录用户的名称,而不是该用户所属的域。

As JPBlanc stated NetUserGetInfo with level 4 is valid only on servers.

Another problem is that you retrieve the name of the logged on user, but not the domain the user belongs to.

无远思近则忧 2024-12-23 12:37:44

请注意,您正在使用 LPTSTR 类型的 pUserName 调用 NetUserGetInfo。
有时它不起作用(如果您将编译项目以默认使用 ANSII 字符串)。

考虑将字符串类型更改为 LPWSTR。

Noticed you are calling NetUserGetInfo with pUserName the type of LPTSTR.
Sometimes it won't work (if you will compile your project to use ANSII strings by default).

Consider changing you string types to LPWSTR.

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