Thread: process finding

  1. #1
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50

    process finding

    hi
    I wrote a small code to find a specific process (in this case the notepad.exe).It compiles Ok but it wont identify the notepad. any one know why???
    I'm using VC++ 6.0

    here is the code

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <Tlhelp32.h>
    
    void getprocess();
    
    PROCESSENTRY32 pr;
    HANDLE hpro;
    BOOL test=TRUE;
    
    int main(void){
    getprocess();
    	return 0;
    }
    
    void getprocess(){
    hpro=CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
    pr.dwSize=sizeof(PROCESSENTRY32);
    
    Process32First(hpro,&pr);
    
    while(test==TRUE){
    test=Process32Next(hpro,&pr);
    
    if(pr.szExeFile == "notepad.exe"){
    MessageBox(NULL,"Notepad is running","Messenger",MB_OK);
    }
    
    printf(pr.szExeFile);
    printf("\n");
    }
    
    CloseHandle(hpro);  
    }

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Code:
    if(pr.szExeFile == "notepad.exe")
    
    // should be something like
    if(strcmp(pr.szExeFile, "notepad.exe")
    With the loop set up like it is right now, you aren't going to be very happy if you have several processes running after notepad
    What is C++?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if(pr.szExeFile == "notepad.exe")
    Lookup strcmp()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50
    okay guys its working
    but why the
    Code:
    if(pr.szExeFile == "notepad.exe")
    is not working ??
    thanks

  5. #5
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    I believe that that doesn't work because the "==" compares the addresses of the two items and not the values of them, don't ask why. Also strcmp returns 0 if the two strings are equal so Vicious's code should be:
    Code:
    if(!strcmp(pr.szExeFile, "notepad.exe"))
    //Rather than
    if(strcmp(pr.szExeFile, "notepad.exe")
    //Which is testing if they are not equal
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Quote Originally Posted by P4R4N01D View Post
    I believe that that doesn't work because the "==" compares the addresses of the two items and not the values of them, don't ask why. Also strcmp returns 0 if the two strings are equal so Vicious's code should be:
    Code:
    if(!strcmp(pr.szExeFile, "notepad.exe"))
    //Rather than
    if(strcmp(pr.szExeFile, "notepad.exe")
    //Which is testing if they are not equal
    !!!!!! Yeah, sorry about that. Rough day
    What is C++?

  7. #7
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by P4R4N01D View Post
    I believe that that doesn't work because the "==" compares the addresses of the two items and not the values of them, don't ask why. Also strcmp returns 0 if the two strings are equal so Vicious's code should be:
    Code:
    if(!strcmp(pr.szExeFile, "notepad.exe"))
    //Rather than
    if(strcmp(pr.szExeFile, "notepad.exe")
    //Which is testing if they are not equal
    No, not really.

    However, a character array becomes a pointer, and same with string literals. This means that you're essentially comparing the addresses of the variables, but that's just because that 'variable itself' contains an address(eg. a pointer).

    Any normal variables (int and char, etc) that aren't pointers are compared the same way - the value that's stored is compared.

  8. #8
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50
    hi
    thank you all for the replies

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. Finding out pid of process
    By kotoko in forum Linux Programming
    Replies: 21
    Last Post: 01-10-2009, 08:57 AM
  3. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  4. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  5. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM