Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 6th, 2006, 10:09 AM   #1
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
String comparison

I'm trying to create a small(very small) task manager. The processes are being seen properly and I can print the names of the processes to the screen, but I'm having a problem with the new task portion. Here's a bit of code.

for(;;)
    {
        if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
        return 1;

        // Calculate how many process identifiers were returned.

        cProcesses = cbNeeded / sizeof(DWORD);

        // Print the name and process identifier for each process.

        for ( i = 0; i < cProcesses; i++ ){
        PrintProcessNameAndID( aProcesses[i] );
        strcpy(name, szProcessName);
        MessageBox(NULL, name, szProcessName,NULL);
        vec.push_back(*name);
        }
        
      /* Here's the more important code that's having problems */
        for(i = 0; i <= vec.size(); i++){
            ne=vec.at(i);
            if(ne=="notepad.exe"){ //If data in location is == to taskmgr.exe
            // do nothing since the program's already running
            break;
            }
            else

            if(vec.back()){ // If we're at the end of the vector and no notepad.exe was found...
                ShellExecute(NULL, "open", "notepad.exe", NULL, NULL, SW_SHOW); //open notepad.exe
                break;
            }
        }
    }

The problem I'm having is that "if(ne=="notepad.exe")" acts as if it's not even there. When I loop through the process list and create a messagebox displaying each process running, it will display that notepad.exe is running, but it still ends up opening another instance of the notepad. It's most likely just a small problem since I just woke up, but does anyone see anything suspicious?

By the way, the "ne" string is declared as string ne;, not a character array.
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old May 6th, 2006, 10:20 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Your problem has nothing to do with string comparisons; the basic logic is wrong. The test of "if (vec.back())" is within the for loop. Which has the effect that, if the first entry in vec is not "notepad.exe", then notepad is launched. Thanks to the "break" statements your code will only ever look at the first entry in vec as well.

If the entries in vec are obtained from the system, there may also be some path information (eg notepad.exe might be something like "C:\\WINNT\\notepad.exe").
grumpy is offline   Reply With Quote
Old May 6th, 2006, 10:23 AM   #3
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 242
Rep Power: 3 Seif is on a distinguished road
try if (strcmp(ne, notepad.exe) == 0)
Seif is offline   Reply With Quote
Old May 6th, 2006, 10:28 AM   #4
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Quote:
Originally Posted by jayme
By the way, the "ne" string is declared as string ne;, not a character array.
Quote:
Originally Posted by seif
try if (strcmp(ne, notepad.exe) == 0)
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old May 6th, 2006, 10:34 AM   #5
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 242
Rep Power: 3 Seif is on a distinguished road
forgot the ""'s sorry.

if (strcmp(ne, "notepad.exe") == 0)
Seif is offline   Reply With Quote
Old May 6th, 2006, 10:39 AM   #6
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
What? Are you reading the posts?
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old May 6th, 2006, 10:40 AM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Seif, you're missing the point. strcmp is used to compare character arrays (char*). Jayme is using the C++ string type, std::string.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 6th, 2006, 11:45 AM   #8
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 242
Rep Power: 3 Seif is on a distinguished road
yeah my bad. have a massive hangover and didnt really read post properly or take a good look at the code. sorta just took a quick scim n see if there was any suggestions i could do to help. sorry.

just read the final line... what a dumbass i am lol.
Seif is offline   Reply With Quote
Old May 6th, 2006, 12:31 PM   #9
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
I've decided I don't need vectors, but still, I can't seem to figure out why this isn't working, since it's more simple than using vectors. It almost seems too simple to even come across a problem, but hmm.

for ( i = 0; i < cProcesses; i++ ){
        PrintProcessNameAndID( aProcesses[i] );
        name = szProcessName;
        MessageBox(NULL, name.c_str(), szProcessName,NULL);

        if(i==cProcesses) p=0;
        if(name=="taskmgr.exe"){
        p=1;
        continue;
        }
        else
        if(name!="taskmgr.exe" && i==cProcesses && p==0)
            ShellExecute(NULL, "open", "notepad.exe", NULL, NULL, SW_SHOW);
        }
This looks like it should work, but notepad doesn't run at all with this code, wether taskmgr.exe is open or not.
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old May 6th, 2006, 6:38 PM   #10
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
I'd suggets using vectors, again. Then you can make use of iterators and algorithms. Assuming I'm following this thread right, I think this should do the trick:
( requires #include <algorithm> )
std::vector<char*>::const_iterator it = find( vec.begin(), vec.end(), "taskmgr.exe" );

if ( it != vec.end() ) 
{ 
	::ShellExecute( NULL, "open", "notepad.exe", NULL, NULL, SW_SHOW );
}

Just the change the type in the vector if you're not using char*.
Cache is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:42 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC