Thread: Comparing String Question

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    10

    Question Comparing String Question

    Sorry if I sound like an idoit, but when I call a function that puts information into a buffer, such as Process32First, Why is it that when I compare it somthing like "Explorer.exe" it doesnt work? Ive checked the buffer, pe32.szExeFile, and its prints "Explorer.exe". Is there something appended to the end? Is there a trim function like in VB? Im just a bit conufsed, Im more experienced in VB but Im moving on to C++.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Can you post a short example of the code that shows us how you are trying to do the comparison?
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    How are doing the comparison and what exactly are you comparing? Is it a C-style char array or a C++ string container? Are you using the equality operator ==? Let me take a guess and say you are using C-style char arrays and the equality operator.

    Code:
    char buffer[] = "Explorer.exe"
    ...
    if( buffer == "Explorer.exe" )
    {
        // They are equal, do something
    }
    The reason that does not work is that both the array name (the part in blue) and the string literal (the part in red) are interpreted as addresses by the compiler in the code above. The if statement above is essentially seen as:

    Code:
    if( address of buffer == address of string literal "Explorer.exe" )
    The addresses of these two things are NEVER going to be equal. The compiler will allocate different memory addresses to them.

    If you want to compare these you need to use the strcmp function:

    Code:
    char buffer[] = "Explorer.exe";
    ...
    if( strcmp(buffer,"Explorer.exe") == 0 )
    {
        // They are equal, do something
    }
    Or, you can just use a C++ string container which has an overloaded == operator and can correctly handle this case:

    Code:
    string buffer = "Explorer.exe";
    ...
    if( buffer == "Explorer.exe" )
    {
        // They are equal, do something
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    10
    here is an example

    Code:
     
      // Set the size of the structure before using it.
      pe32.dwSize = sizeof( PROCESSENTRY32 );
    
      // Retrieve information about the first process,
      // and exit if unsuccessful
      if( !Process32First( hProcessSnap, &pe32 ) )
      {
        printError( "Process32First" );  // Show cause of failure
        CloseHandle( hProcessSnap );     // Must clean up the snapshot object!
        return( FALSE );
      }
    
        if (pe32.szExeFile=="Explorer.exe")    <---------- HERE
        {
            printf("\n FOUND!!!!!!!!!!!!!!");
            }

    EDIT: StrCmp worked. Thanks
    Last edited by ew16301; 03-16-2005 at 04:07 PM.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Not sure if it makes a difference, but the buffer in the structure is of type TCHAR, so he should probably use the _tcscmp function instead, and the _T() macro for string literals.

    Code:
    if (_tcscmp(pe32.szExeFile,_T("Explorer.exe")) == 0) {
        // equal, do whatever
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM