Getting user name failed from the SID in c++?

0

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.

c++
visual-c++
wmi
wmi-query
sid
asked on Stack Overflow Oct 11, 2013 by user2499879

2 Answers

0

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.

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". 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);

answered on Stack Overflow Oct 11, 2013 by Henno

User contributions licensed under CC BY-SA 3.0