简体   繁体   English

使用GetUserNameEx获取计算机主体名称

[英]Get computer principal name with `GetUserNameEx`

I tried to get computer principal name of a service running as Network Service with the following code. 我尝试使用以下代码获取作为Network Service服务运行的服务的计算机主体名称。

BOOLEAN bError = GetUserNameEx(NameUserPrincipal, buffer, &buf_len);

Since the domain name of my computer is Service-Windows@example.com , I expect the result in buffer is the same. 由于我的计算机的域名是Service-Windows@example.com ,我希望buffer的结果是相同的。 However, the returned service principal name is WIN-SC4RKQUKM4D@example.com . 但是,返回的服务主体名称为WIN-SC4RKQUKM4D@example.com I'm sure I've changed my computer and add it into the domain example.com . 我确定我已经更换了计算机,并将其添加到example.com域中。

I've no idea about the abnormal principal name. 我不知道异常的主体名称。 What may be the problem? 可能是什么问题? Any hints will be highly appreciated. 任何提示将不胜感激。

UPDATE UPDATE


I'm running the service on domain controller, don't know whether this will interfere the result. 我正在域控制器上运行服务,不知道这是否会干扰结果。

Thank you and best regards! 感谢你并致以真诚的问候!

I would not expect to get the computer name when looking up the user name for the Network Service user account. 查找网络服务用户帐户的用户名时,我不希望获得计算机名。 If you look it up with LookupAccountSid, I'd expect to see NT AUTHORITy\\NETWORK SERVICE : 如果您使用LookupAccountSid查找它,我希望看到NT AUTHORITy\\NETWORK SERVICE

#include <windows.h>
#include <iostream>

int main(){
    SID nss;
    DWORD size = sizeof(nss);

    CreateWellKnownSid(WinNetworkServiceSid, NULL, &nss, &size);

    char name[256];
    DWORD n_size = sizeof(name);
    char domain[256];
    DWORD d_size = sizeof(domain);
    SID_NAME_USE type;

    LookupAccountSid(NULL, &nss, name, &n_size, domain, &d_size, &type);

    std::cout << domain << "\\" << name << "\n";
    return 0;
}

[And that's what I get]. [这就是我得到的]。

I'd guess the user name you're seeing it some remnant of an automatically created user account. 我猜您会在用户名中看到它是自动创建的用户帐户的一部分。 There's apparently some remnant of that original name long after you've changed names to other things. 在您将名称更改为其他名称后很长时间,该原始名称显然还有一些残留。 I've also seen them now and again, though never seen a dependable enough pattern to predict when they would or wouldn't show up (and have never found any way to be sure they're entirely gone either). 我也一次又一次地看到它们,尽管从未见过足够可靠的模式来预测它们何时会出现或不会出现(并且从未找到任何方法来确保它们完全消失了)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM