process id from exe

This is a discussion on process id from exe within the C++ Programming forums, part of the General Programming Boards category; Hello, Ok my problem is that I need a function to find out the process id from a given process ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    process id from exe

    Hello,

    Ok my problem is that I need a function to find out the process id from a given process name.

    e.g.: I want to get the pid for explorer.exe, then the function should give me the pid 1156.

    Can anyone help me with an understandable code example.

    Thank you for every help,
    keeper

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You do realize that there could be multiple instances of the process explorer.exe running, right?

    Search the forums, especially the Windows programming forum. I'm sure it's come up before.

    [edit] Searching for "process id" in the Windows forum (eventually) led me to this: Getting PID from process

    It links to this: Info on specific process

    To be exact: Info on specific process [/edit]
    Last edited by dwks; 06-21-2007 at 07:46 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Quote Originally Posted by dwks View Post
    You do realize that there could be multiple instances of the process explorer.exe running, right?

    Search the forums, especially the Windows programming forum. I'm sure it's come up before.

    [edit] Searching for "process id" in the Windows forum (eventually) led me to this: Getting PID from process

    It links to this: Info on specific process

    To be exact: Info on specific process [/edit]
    Yes I realize that, it was only an example.

    I try the code in the topics you post, but I have some problems with that. I get the error: main.c(13): error C2143: syntax error : missing ';' before 'type'

    An in this line I have:

    l12: num_processes = bytes_returned/sizeof(DWORD);
    l13: for(int i = 0; i<num_processes; i++) {
    l14: hProcess=OpenProcess(PROCESS_ALL_ACCESS,TRUE,proce ss_id_array[i]);


    and this error: main.c(47): error C2275: 'DWORD' : illegal use of this type as an expression

    An in this line I have:

    l47: DWORD dwPID = GetProcessByFileName("Notepad.exe");
    l48: printf(dwPID);


    Whats wrong here?

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    920
    There's a space...
    Code:
    proce ss_id_array[i]
    printf needs a format string. Besides, is this C or C++?
    Code:
    printf(dwPID);
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    > I get the error: main.c(13): error C2143: syntax error : missing ';' before 'type'
    That's because "for ( int i " is a C++ thing, not a C thing (not yet anyway), and you're compiling the code as a C program.

    Choose your language, pick your forum, post your code.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    I use the english version of Visual Studio C++ 2003 with the standard settings.

    Code:
    DWORD GetProcessByFileName(char* name){
        DWORD process_id_array[1024];
        DWORD bytes_returned;
        DWORD num_processes;
        HANDLE hProcess;
        char image_name[256];
        char buffer[256];
    	int i;
        DWORD exitcode;
        EnumProcesses(process_id_array, 256*sizeof(DWORD), &bytes_returned);
        num_processes = (bytes_returned/sizeof(DWORD));
        for (i = 0; i < num_processes; i++) {
            hProcess=OpenProcess(PROCESS_ALL_ACCESS,TRUE,process_id_array[i]);
            if(GetModuleBaseName(hProcess,0,image_name,256)){
                if(!stricmp(image_name,name)){
                    CloseHandle(hProcess);
                    return process_id_array[i];
                }
            }
            CloseHandle(hProcess);
        }
        return 0;
    }
    void __cdecl main(int argc, char *argv[])
    {
    	DWORD dwPID;
    	dwPID = GetProcessByFileName("Notepad.exe");
    	printf(dwPID);
    	return;
    }
    Now I get these errors:

    Inject error LNK2005: __chkstk already defined in ntdll.lib(ntdll.dll)

    Inject fatal error LNK1169: one or more multiply defined symbols found

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Have you read the replies thus far?
    Code:
    printf(dwPID);
    ->
    Code:
    printf("&#37;lu", (unsigned long)dwPID);
    void main() is not standard, you should be using int main(). See the FAQ.

    What are your linker options? Do you have one library repeated twice?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Quote Originally Posted by dwks View Post
    Have you read the replies thus far?
    Code:
    printf(dwPID);
    ->
    Code:
    printf("%lu", (unsigned long)dwPID);
    void main() is not standard, you should be using int main(). See the FAQ.

    What are your linker options? Do you have one library repeated twice?
    Yes I read the replies. And I think I get this working, if I get solve this problem:
    mixing c file with c++ file first. The problem is the same here mixing c and c++ files.
    Have you any ideas for the problem in the other thread?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    Create a new console project, and copy your latest source code to the new project.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Quote Originally Posted by Salem View Post
    Create a new console project, and copy your latest source code to the new project.
    Making a complete new project insert the code from above (06-22-2007 03:58 AM) nothing more I get these errors:

    process.cpp error C2146: syntax error : missing ';' before identifier 'GetProcessByFileName'

    process.cpp error C2501: 'DWORD' : missing storage-class or type specifiers

    process.cpp(2): error C2146: syntax error : missing ';' before identifier 'process_id_array'

    process.cpp(4): error C2065: 'num_processes' : undeclared identifier

    and many more.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    have you #include <windows.h> ?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Quote Originally Posted by vart View Post
    have you #include <windows.h> ?
    Thanks now there are less errors:

    process.cpp(12): error C3861: 'EnumProcesses': identifier not found, even with argument-dependent lookup

    process.cpp(16): error C3861: 'GetModuleBaseName': identifier not found, even with argument-dependent lookup
    Last edited by keeper; 06-24-2007 at 06:07 AM.

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    OK get it working

    Code:
    #include <windows.h>
    #include <tlhelp32.h>
    #include <iostream>	
    #include <string>
    #include "psapi.h"
    
    DWORD GetProcessByFileName(char* name){
        DWORD process_id_array[1024];
        DWORD bytes_returned;
        DWORD num_processes;
        HANDLE hProcess;
        char image_name[256];
        char buffer[256];
    	int i;
        DWORD exitcode;
        EnumProcesses(process_id_array, 256*sizeof(DWORD), &bytes_returned);
        num_processes = (bytes_returned/sizeof(DWORD));
        for (i = 0; i < num_processes; i++) {
            hProcess=OpenProcess(PROCESS_ALL_ACCESS,TRUE,process_id_array[i]);
            if(GetModuleBaseName(hProcess,0,image_name,256)){
                if(!stricmp(image_name,name)){
                    CloseHandle(hProcess);
                    return process_id_array[i];
                }
            }
            CloseHandle(hProcess);
        }
        return 0;
    }
    void __cdecl main(int argc, char *argv[])
    {
    	DWORD dwPID;
    	dwPID = GetProcessByFileName("calc.exe");
    	printf("%lu", (unsigned long)dwPID);
    	return;
    }
    But only for Debug compilation not for release. When I use release I get these linker errors:

    processid error LNK2019: unresolved external symbol _GetModuleBaseNameA@16 referenced in function "unsigned long __cdecl GetProcessByFileName(char *)" (?GetProcessByFileName@@YAKPAD@Z)

    processid error LNK2019: unresolved external symbol _EnumProcesses@12 referenced in function "unsigned long __cdecl GetProcessByFileName(char *)" (?GetProcessByFileName@@YAKPAD@Z)

    Why?

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    Check that both versions have the same list of libraries in the project settings.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Code:
    #include "psapi.h"
    
    to
    
    #include <psapi.h>
    
    and 
    
    	printf("%lu", (unsigned long)dwPID);
    	return;
    }
    
    to
    
    	printf("%lu", (unsigned long)dwPID);
    	return 0;
    }
    getting unresolved external.

Page 1 of 2 12 LastLast
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, 11:05 AM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  3. Replies: 3
    Last Post: 10-15-2008, 10:24 AM
  4. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 02:43 AM
  5. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 08:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21