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.

user2499879
  • 673
  • 3
  • 10
  • 16

2 Answers2

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.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • While initializing the SID in std::wstring, I got the error error C2440: 'initializing' : cannot convert from 'char [47]' to 'class std::basic_string,class std::allocator >' No constructor could take the source type, or constructor overload resolution was ambiguous – user2499879 Oct 11 '13 at 14:52
  • @user2499879 Sorry, I missed the `L` prefix on the string literal. Fixed. – Angew is no longer proud of SO Oct 11 '13 at 14:55
  • Now I got the another error :error C2679: binary '+' : no operator defined which takes a right-hand operand of type 'class std::basic_string,class std::allocator >' (or there is no acceptable conversion) – user2499879 Oct 11 '13 at 14:57
  • @user2499879 I've added a `+`-less approach, but if that doesn't work either, there's probably something wrong with your compiler. Which version of VS are you using, anyway? – Angew is no longer proud of SO Oct 11 '13 at 15:12
  • @user2499879 Ouch. That's *ancient, outdated* and **broken.** Any chance for you to use something at least remotely more recent? I mean, this compiler is older than the C++ standard. Coding for it basically just means piling wokrarounds on top of each other. – Angew is no longer proud of SO Oct 11 '13 at 15:22
  • Anyother solution to execute on v.s 6.0 compiler – user2499879 Oct 11 '13 at 15:29
  • I have tried with your changes,,, and got another error GetObjectA' : cannot convert parameter 1 from 'const unsigned short *' to 'unsigned short *' – user2499879 Oct 11 '13 at 15:36
  • @user2499879 That's just a simple matter of adding `const_cast`. – Angew is no longer proud of SO Oct 11 '13 at 15:43
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);

Henno
  • 397
  • 1
  • 7
  • When I tried your code I got the following error in line std::wstring query = "Win32_DID.SID='" + SID + "'"; error C2679: binary '+' : no operator defined which takes a right-hand operand of type 'class std::basic_string,class std::allocator >' (or there is no acceptable conversion) – user2499879 Oct 11 '13 at 14:56
  • As before - wstring-constants must be prefixed with a L: `std::wstring query = L"Win32_DID.SID='" + SID + L"'"; – Henno Oct 11 '13 at 15:02
  • See the solution of @Angew. Or try `std::wstring query = std::wstring(L"Win32_DID.SID='") + SID + std::wstring(L"'").c_str(); – Henno Oct 11 '13 at 15:27
  • Got the follwoing error GetObjectA' : cannot convert parameter 1 from 'class std::basic_string,class std::allocator >' to 'unsigned sh ort *' – user2499879 Oct 11 '13 at 15:39
  • Sorry, the `c_str()` belongs at the end of `query` in the GetObject-line. – Henno Oct 11 '13 at 15:52