【问题标题】:Getting user name failed from the SID in c++?从 C++ 中的 SID 获取用户名失败?
【发布时间】:2013-10-11 14:32:34
【问题描述】:

当我尝试使用以下代码获取用户名时,我已成功获取用户名:

hres = pSvc->GetObject(L"Win32_SID.SID='S-1-5-82-1036420768-1044797643-1061213386-2937092688-4282445334'",  0, NULL, &pclsObj, NULL);

但是当将 SID 分配给如下变量时

std::string SID = "S-1-5-82-1036420768-1044797643-1061213386-2937092688-4282445334";

hres = pSvc->GetObject(L"Win32_SID.SID=SID",  0, NULL, &pclsObj, NULL);

然后我收到以下错误:

Connected to root\CIMV2 WMI namespace
GetObject failed Error code = 0x8004103a
IDispatch error #3642

您能否建议我在 GetObject 方法中正确输入。

【问题讨论】:

    标签: c++ visual-c++ wmi wmi-query sid


    【解决方案1】:

    我相信您正在寻找这个:

    std::wstring SID = L"S-1-5-82-1036420768-1044797643-1061213386-2937092688-4282445334";
    
    hres = pSvc->GetObject((L"Win32_SID.SID='" + SID + L"'").c_str(),  0, NULL, &pclsObj, NULL);
    

    如果上面的代码(注意我添加了一个忘记调用c_str())对你不起作用,你可以试试这个:

    #include <sstream>
    
    std::wstring SID = L"S-1-5-82-1036420768-1044797643-1061213386-2937092688-4282445334";
    
    std::wostringstream s;
    s << L"Win32_SID.SID='" << SID << L"'";
    
    hres = pSvc->GetObject(s.str().c_str(),  0, NULL, &pclsObj, NULL);
    

    如果这仍然不起作用,我会开始怀疑编译器有问题。


    你在 cmets 中提到你使用的是古老的 VC++ 6.0,所以我会尝试一些非常基本的东西(我假设你的目标是让 SID 成为一个变量):

    #include <cwchar>
    
    std::wstring SID = L"S-1-5-82-1036420768-1044797643-1061213386-2937092688-4282445334";
    
    const wchar_t *prefix = L"Win32_SID.SID='";
    wchar_t *arg = new wchar_t[wcslen(prefix) + SID.size() + 2]; //2 = terminating ' and NUL
    wcscpy(arg, prefix);
    wcscat(arg, SDI.c_str());
    wcscat(arg, L"'");
    
    hres = pSvc->GetObject(arg,  0, NULL, &pclsObj, NULL);
    
    delete[] arg;
    

    请注意,这不是测试 - 我无权访问 VC++ 6.0。

    【讨论】:

    • 在 std::wstring 中初始化 SID 时,出现错误 error C2440: 'initializing' : cannot convert from 'char [47]' to 'class std::basic_string,class std::allocator >' 没有构造函数可以采用源类型,或者构造函数重载决议不明确
    • @user2499879 抱歉,我错过了字符串文字上的 L 前缀。固定。
    • 现在我得到了另一个错误:error C2679: binary '+' : no operator defined 它采用'class std::basic_string,class std::allocator >' (或没有可接受的转换)
    • @user2499879 我添加了+-less 方法,但如果这也不起作用,那么您的编译器可能有问题。无论如何,您使用的是哪个版本的 VS?
    • @user2499879 哎哟。那是古老的、过时的破旧的。你有机会使用至少远程更新的东西吗?我的意思是,这个编译器比 C++ 标准更老。为其编码基本上只是意味着将 wokrarounds 堆叠在一起。
    【解决方案2】:

    在线
    hres = pSvc-&gt;GetObject(L"Win32_SID.SID=SID", 0, NULL, &amp;pclsObj, NULL);
    您要求输入属于字符串“SID”的用户名。那是行不通的。 您需要连接“Win32_DID.SID=”和您的 SID 字符串。此外,您需要将其作为 WSTRING 提供:
    std::wstring SID = L"S-1-5-82-1036420768-1044797643-1061213386-2937092688-4282445334"; std::wstring query = "Win32_DID.SID='" + SID + "'"; hres = pSvc->GetObject(query, 0, NULL, &pclsObj, NULL);

    【讨论】:

    • 当我尝试你的代码时,我在 std::wstring query = "Win32_DID.SID='" + SID + "'";错误 C2679:二进制“+”:未定义运算符,该运算符采用“类 std::basic_string,class std::allocator 类型的右侧操作数>'(或没有可接受的转换)
    • 和以前一样 - wstring-constants 必须以 L 为前缀:`std::wstring query = L"Win32_DID.SID='" + SID + L"'";
    • 查看@Angew 的解决方案。或者试试`std::wstring query = std::wstring(L"Win32_DID.SID='") + SID + std::wstring(L"'").c_str();
    • 得到以下错误 GetObjectA' : 无法从 'class std::basic_string,class std::allocator 转换参数 1 ' 到 '无符号短 *'
    • 抱歉,c_str() 属于 GetObject 行中 query 的末尾。
    猜你喜欢
    • 1970-01-01
    • 2012-10-22
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多