PID

I am just beginning learn C++, and I need to find the process ID of a program. I just learned about functions, and I am trying this out. I finally got all of my errors gone, and a new one popped up.

I haven't got a clue to what this is, and google didn't help either. Please help me figure out what is happening.

undefined reference to `GetProcessId(void*)@4'

I would assume that it means that there is no prototype of GetProcessId, but I really need help.


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <windows.h>
using namespace std;
DWORD WINAPI GetProcessId(HANDLE Process);
int main()
{
    char Process[100];
    int pid;
    cout << "Process: ";
    cin >> Process;
    pid = GetProcessId(Process);
    cout << pid;
}
Last edited on
GetProcessId function takes a process handle as a parameter.
I've done something like this before:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#define _WIN32_WINNT 0x501

#include <iostream>
#include <windows.h>
#include <tlhelp32.h>
using namespace std;

// Get process handle by process name
HANDLE GetProcessHandle(const char *process_name, DWORD dwAccess);


int main(int argc, char *argv[])
{
    char process_name[128];
    cout <<"Enter process name: ";
    cin >>process_name;

    HANDLE hProcess=GetProcessHandle(process_name,PROCESS_QUERY_INFORMATION);
    DWORD pid=GetProcessId(hProcess);
    cout <<"PID: "<<pid<<endl;

    CloseHandle(hProcess);


    return EXIT_SUCCESS;
}



HANDLE GetProcessHandle(const char *process_name, DWORD dwAccess)
{
HANDLE hProcessSnap;
HANDLE hProcess;
PROCESSENTRY32 pe32;


hProcessSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);

if(hProcessSnap==INVALID_HANDLE_VALUE)
  {
    cerr <<"Failed to create process snapshot!";
    return INVALID_HANDLE_VALUE;
  }

pe32.dwSize=sizeof(PROCESSENTRY32);

if(!Process32First(hProcessSnap,&pe32))
  {
      cerr <<"Process32First() failed\n";
      return INVALID_HANDLE_VALUE;
  }

do
  {
    if(strcmp(pe32.szExeFile,process_name)==0)
        return OpenProcess(dwAccess,0,pe32.th32ProcessID);

  }while(Process32Next(hProcessSnap,&pe32));

}

You can also modify my GetProcessHandle function to return pe32.th32ProcessID

GetProcessId: http://msdn.microsoft.com/en-us/library/ms683215(VS.85).aspx
CreateToolhelp32Snapshot: http://msdn.microsoft.com/en-us/library/ms682489(VS.85).aspx

I am only a couple days into learning this awesome stuff, and that made no sense to me. For my purposes it will do, as I am after a task and not the understanding of this specific code. Thanks for posting, I will hopefully learn to understand it soon.
Note that GetProcessId is a WinAPI function. On other systems, you'd have to use different functions.
So when you have more WinAPI-related questions, you should post in the Windows programming subforum.

You shouldn't declare WinAPI functions yourselves, instead you should make sure the correct header is included. windows.h already does that, however GetProcessId was not available in old versions of Windows, so you need to explicitly specify that you want to use those newer functions. You can do that by defining a global macro or manually for each file by adding the line #define _WIN32_WINNT 0x501 before including windows.h. That would let you use all functions that were available in Windows XP and before. See here for other possible values:
http://msdn.microsoft.com/en-us/library/6sehtctf.aspx
Last edited on
Topic archived. No new replies allowed.