![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Professional Programmer
|
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:
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! ▄▄▄▄ |
|
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
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"). |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 242
Rep Power: 3
![]() |
try if (strcmp(ne, notepad.exe) == 0)
|
|
|
|
|
|
#4 | ||
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
Quote:
![]()
__________________
PFO - My daily dose of technology. |
||
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 242
Rep Power: 3
![]() |
|
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
What? Are you reading the posts?
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Seif, you're missing the point. strcmp is used to compare character arrays (char*). Jayme is using the C++ string type, std::string.
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 242
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#9 | |
|
Professional Programmer
|
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);
}
__________________
▄▄▄▄ Quote:
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! ▄▄▄▄ |
|
|
|
|
|
|
#10 |
|
Hobbyist
Join Date: Sep 2005
Posts: 266
Rep Power: 4
![]() |
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*. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|