[英]Getting user name failed from the SID in c++?
When I have tried to get the username using below code, I have successfully get the user name: 当我尝试使用以下代码获取用户名时,我已成功获取用户名:
hres = pSvc->GetObject(L"Win32_SID.SID='S-1-5-82-1036420768-1044797643-1061213386-2937092688-4282445334'", 0, NULL, &pclsObj, NULL);
But when assign the SID into variable as follows 但是当按如下方式将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);
then I got the following error: 然后我得到以下错误:
Connected to root\CIMV2 WMI namespace
GetObject failed Error code = 0x8004103a
IDispatch error #3642
Could you please suggest me the correct input in GetObject method. 您能否建议我在GetObject方法中输入正确的信息。
I believe you're looking for this: 我相信您正在寻找这个:
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);
If the above code (notice I added a forgotten call to c_str()
) doesn't work for you, you could try this instead: 如果以上代码(注意,我对
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);
If this still doesn't work, I'd start suspecting a problem with the compiler. 如果仍然无法解决问题,我将开始怀疑编译器是否存在问题。
You've mentioned in the comments you're using the ancient VC++ 6.0, so I will try something really basic (I assume your goal is to have SID
be a variable): 您已经在使用古老的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;
Please note it's not test it - I don't have access to VC++ 6.0. 请注意,它尚未测试-我无权使用VC ++ 6.0。
In the line 在行中
hres = pSvc->GetObject(L"Win32_SID.SID=SID", 0, NULL, &pclsObj, NULL);
you ask for the username belonging to the String "SID". 您要求输入用户名“ SID”。 That cannot work.
那行不通。 You need to concatenate the "Win32_DID.SID=" and your SID-String.
您需要连接“ Win32_DID.SID =“和您的SID-String。 Also, you need to give it as a WSTRING:
另外,您需要将其作为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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.