Hi,

Recently I started working on a project (it's called 'fd') that needs to retrieve the current processes running. It is written in C using the WinAPI. I am running Windows XP SP3 and am using MinGW compiler with Code::Blocks. After much searching I found the PSAPI to provide the solution to my problem. It is added below, direct copy from http://msdn.microsoft.com/en-us/library/ms682623%28v=VS.85%29.aspx:

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <psapi.h>

// To ensure correct resolution of symbols, add Psapi.lib to TARGETLIBS
// and compile with -DPSAPI_VERSION=1

void PrintProcessNameAndID( DWORD processID )
{
    TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");

    // Get a handle to the process.

    HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                   PROCESS_VM_READ,
                                   FALSE, processID );

    // Get the process name.

    if (NULL != hProcess )
    {
        HMODULE hMod;
        DWORD cbNeeded;

        if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
             &cbNeeded) )
        {
            GetModuleBaseName( hProcess, hMod, szProcessName, 
                               sizeof(szProcessName)/sizeof(TCHAR) );
        }
    }

    // Print the process name and identifier.

    _tprintf( TEXT("%s  (PID: %u)\n"), szProcessName, processID );

    // Release the handle to the process.

    CloseHandle( hProcess );
}

int main( void )
{
    // Get the list of process identifiers.

    DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i;

    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++ )
    {
        if( aProcesses[i] != 0 )
        {
            PrintProcessNameAndID( aProcesses[i] );
        }
    }

    return 0;
}

However, when I tried out the code provided by MSDN, I received the following errors:

D:\CodeBlocksProjects\fd\main.c||In function `PrintProcessNameAndID':|
D:\CodeBlocksProjects\fd\main.c|36|warning: unsigned int format, DWORD arg (arg 3)|
obj\Debug\main.o||In function `PrintProcessNameAndID':|
D:\CodeBlocksProjects\fd\main.c|26|undefined reference to `EnumProcessModules@16'|
D:\CodeBlocksProjects\fd\main.c|29|undefined reference to `GetModuleBaseNameA@16'|
obj\Debug\main.o||In function `main':|
D:\CodeBlocksProjects\fd\main.c|50|undefined reference to `EnumProcesses@12'|
||=== Build finished: 3 errors, 1 warnings ===|

So I thought "Oh the psapi library (psapi.lib) is not added in the build" and started searching for it in the MinGW compiler. After my long search, both in the folders and on internet, I found out that those were included within the Windows SDK. So I downloaded the latest version (3.5) and installed that. And in the Libs folder the file psapi.lib existed. So I did the following:

Right mouse click on the project -> Build Options -> Linker Settings -> Add -> browsed using the file browser to psapi.lib -> double clicked -> Pressed Ok

Then it was linked to the project. Afterwards I Build&Run the application, showing no errors (except the D:\CodeBlocksProjects\fd\main.c|36|warning: unsigned int format, DWORD arg (arg 3)| obj\Debug\main.o||In function `PrintProcessNameAndID':|), but that can be neglected. When the build was finished, it ran however It resulted in a crash (Process terminated with status -1073741819 (0 minutes, 2 seconds)). When I isolated the code:

DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i;
printf("Here");
    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
    {
        return 1;
    }

printf("Here too");
    // Calculate how many process identifiers were returned.

    cProcesses = cbNeeded / sizeof(DWORD);

Only the first "Here" appeared on the console window. What am I doing wrong?

Thanks in advance,
~G

Recommended Answers

All 3 Replies

Are you sure the library doesn't exist in your MinGW folders?

I have the latest version of Code::Blocks/MinGW and I was able
to locate it here -> ...CodeBlocks\MinGW\lib\libpsapi.a See if you can find it, link to it and see if the problem persists.

Thank you, that worked! I was so fixed on finding a .lib file on the MinGW folder that I did not realize that MinGW uses other types of libraries.

You're welcome :)

Note that the psapi.lib you got from Windows SDK should work too, but
you probably also need a psapi.dll next to your executable in this case.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.