Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   C++ String Comparison (http://www.go4expert.com/forums/cpp-string-comparison-t16507/)

robbycraig 11Mar2009 14:15

C++ String Comparison
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 13Mar2009 13:39

Re: C++ String Comparison
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 17Mar2009 10:23

Re: C++ String Comparison
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 17Mar2009 12:04

Re: C++ String Comparison

Originally Posted by imrantechi (Post 44449)
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.

All times are GMT +5.5. The time now is 20:58.