0
votes

This code will read the running process from OS and display it (C++). Specifically, the OS here is Windows XP. The Problem(error) is in (i think) prototype. By the way, it displays following errors.

Error 1 : error LNK2019: unresolved external symbol _EnumProcesses@12 referenced in function _main

Error 2 : error LNK2019: unresolved external symbol _GetModuleBaseNameA@16 referenced in function "void __cdecl DisplayProcessNameAndID(unsigned long)" (?DisplayProcessNameAndID@@YAXK@Z)

Error 3 : error LNK2019: unresolved external symbol _EnumProcessModules@16 referenced in function "void __cdecl DisplayProcessNameAndID(unsigned long)" (?DisplayProcessNameAndID@@YAXK@Z)

Error 4 : fatal error LNK1120: 3 unresolved externals C:\Documents and Settings\Windows\My Documents\Visual Studio 2008\Projects\a\Debug\a.exe

#include <afxwin.h>

#include <iostream>
#include <string.h>

#include "psapi.h"
unsigned int i;

using namespace std;
void DisplayProcessNameAndID(DWORD processID);
void main()
{

   DWORD aProcesses[1024], cbNeeded, cProcesses;
   if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
      return;
   cProcesses = cbNeeded / sizeof(DWORD);


   for ( i = 0; i < cProcesses; i++ )
   {
   if( aProcesses[i] != 0 )
          DisplayProcessNameAndID( aProcesses[i] );
   }
};
void DisplayProcessNameAndID( DWORD processID )
{
   TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
  HANDLE hProcess = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID ) ;`                                                                     

   if (NULL != hProcess )
   {
      HMODULE hMod;
      DWORD cbNeeded;
      if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) )
      {
         GetModuleBaseName( hProcess, hMod, szProcessName,
            sizeof(szProcessName)/sizeof(TCHAR) );
      }
   };
   CString str;
   str.Format("Text:%s, PID : %u", szProcessName, processID );
   AfxMessageBox(str);
   CloseHandle( hProcess );
   }
1
Your windows.h is C? If so, you may need an extern "C" to prevent C++ name-mangling from screwing up your linker. - Martin James
I have to do nothing with windows.h. Hence i removed it. It make no difference. - Jamil
Any help would be appreciated. - Jamil
You'll have to add "psapi.lib" to the linker's Additional Dependencies setting. - Hans Passant

1 Answers

2
votes

No your issue is not with the prototype - the prototypes in windows.h are fine. If you notice your error message start with LNK which means that the linker is giving the error. This means that the linker can't find those functions.

If you look at the documentation for one of the functions that is giving the error, EnumProcessModules at the very bottom in the Library section you'll see that on Windows XP it requires linking to Psapi.lib. Visual C++ doesn't link to that library by default like it does for Kernel32.lib, which is why any functions defined in that library are fine.

You can add the Psapi.lib to the Additional Libraries section of your project, or just add the line:

#pragma comment(lib, "Psapi.lib")

To the top of the file, which will instruct the linker to use Psapi.lib.