Thread: C++/Qt5 - Problem with psapi.h & compiler flag

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    56

    C++/Qt5 - Problem with psapi.h & compiler flag

    Windows 10 (32bit)
    C++ Qt5.6.0 - minGW49_32

    Hello,

    I'm trying to get some code working to get a list of running processes.
    I'm working from within QtCreator.
    MinGW-users - Problem using <psapi.h>
    c++ - Adding external library into Qt Creator project - Stack Overflow

    The sites I've looked at all seem to say add psapi.lib to the the compiler flags.
    add "-lpsapi" to your compiler command
    LIBS += -L/path/to -lpsapi

    I don't seem to have psapi.lib on my system. A search of my Qt directory shows:-
    libpsapi.a
    dhvpsapi.h
    psapi.h

    Any suggestions, Please!

    Regards
    Code:
    void MainWindow::getProcesses()
    {
        // Get the list of process identifiers.
    
        DWORD aProcesses[1024], cbNeeded, cProcesses;
        //unsigned int i;
    
        if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            return; //err msg
    
        // Calculate how many process identifiers were returned.
        cProcesses = cbNeeded / sizeof(DWORD);
    
        // Print the name and process identifier for each process.
        for ( uint i = 0; i < cProcesses; i++ )
            if( aProcesses[i] != 0 )
                PrintProcessNameAndID( aProcesses[i] );
    }
    Code:
    void MainWindow::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 );
    
        CloseHandle( hProcess );
    }
    Code:
    compile errors:-
    error: undefined reference to `EnumProcessModules@16'
    error: undefined reference to `GetModuleBaseNameA@16'
    error: undefined reference to `EnumProcesses@12'
    :-1: error: release/mainwindow.o: bad reloc address 0xc88 in section `.rdata'

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The directions you found looks correct did you try them?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-04-2008, 02:34 AM
  2. /gs flag
    By mystic-d in forum Tech Board
    Replies: 5
    Last Post: 01-20-2007, 04:38 AM
  3. __restrict compiler flag?
    By Kleid-0 in forum C Programming
    Replies: 7
    Last Post: 05-28-2005, 01:23 AM
  4. How to get psapi.h working?
    By Lord CyKill in forum C++ Programming
    Replies: 14
    Last Post: 03-22-2005, 11:43 AM
  5. Looking for a gcc flag
    By JLWinsett in forum Linux Programming
    Replies: 4
    Last Post: 12-26-2002, 11:39 AM

Tags for this Thread