C++ String Comparison

Newbie Member
11Mar2009,14:15   #1
robbycraig's Avatar
I have connected to ROOT\\CIMV2 WMI namespace and after connecting run the following code.

Code:
IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(
        bstr_t("WQL"), 
        bstr_t("Select * from Win32_Process"),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
        NULL,
        &pEnumerator);
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:

Code:
VARIANT vtProp;

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
Mentor
13Mar2009,13:39   #2
xpi0t0s's Avatar
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.
Ambitious contributor
17Mar2009,10:23   #3
imrantechi's Avatar
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..
Go4Expert Founder
17Mar2009,12:04   #4
shabbir's Avatar
Quote:
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.