C++ String Comparison

robbycraig's Avatar, Join Date: Mar 2009
Newbie Member
I have connected to ROOT\\CIMV2 WMI namespace and after connecting run the following code.

IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(
        bstr_t("Select * from Win32_Process"),
After this I used while (pEnumerator) to get the name of each of the running processes.

I want to get the process id for just the explorer process but I can not get the string comparison to work.

I have tried the following:


hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
string procToFind = "explorer.exe";
string currentProc = (char *)vtProp.bstrVal;

wcout << vtProp.bstrVal << " = " << procToFind.c_str() << " " << procToFind.compare(currentProc) << endl;
This prints out "explorer.exe = explorer.exe 1".

Why are these two not equal? I am new to C and am confused by all the different types of strings.

The only modification is the query I ran. Thanks for your help
xpi0t0s's Avatar, Join Date: Aug 2004
They certainly look equal. Maybe one is plain ASCII and the other is Unicode? Have a look in memory* where the strings are stored, if one is effectively 'e' '\0' 'x' '\0'... and the other is 'e' 'x' 'p'... then you're trying to compare Unicode (the former) with ASCII (the latter) and this would correctly return "not equal", while appearing equal with both strings printed side by side.

*Either in the debugger, or get a void* pointer for each location and do a quick, say 32-byte, hex dump.
imrantechi's Avatar, Join Date: Feb 2008
Ambitious contributor
whenever string code is like _T( " ") it means it's unicode.
So your code is using unicode so it's different than ASCII (As told by xpiOtOs) because in unicode one character's size will be 2 byte and in ASCII it's 1 byte..
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Originally Posted by imrantechi View Post
whenever string code is like _T( " ") it means it's unicode.
This is wrong assumption. _T means it would redirect to unicode if unicdoe compiled else to ASCII if no unicode compilation specified. Or to be more specific it would be based on the compile time flag macro decide if its unicode or non-unicode.

Where as _w is more specific macro specifying unicode.