C++ String Comparison

Discussion in 'C++' started by robbycraig, Mar 11, 2009.

  1. robbycraig

    robbycraig New Member

    Joined:
    Mar 11, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  3. imrantechi

    imrantechi New Member

    Joined:
    Feb 12, 2008
    Messages:
    116
    Likes Received:
    4
    Trophy Points:
    0
    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..
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice