Question

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

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.

Was it helpful?

Solution

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:

#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):

#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.

OTHER TIPS

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". That cannot work. You need to concatenate the "Win32_DID.SID=" and your SID-String. Also, you need to give it as a 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);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top