importing psapi.dll functionality

I have been attempting to use __cdecspl(dllimport) to use psapi functionality with no luck.  According to what documentation I have been able to gather this "should" work.  Any Ideas guys?
#include <afxwin.h>
#include <stdio.h>

typedef struct _MODULEINFO {
    LPVOID lpBaseOfDll;
    DWORD SizeOfImage;
    LPVOID EntryPoint;
} MODULEINFO, *LPMODULEINFO;

typedef struct _PSAPI_WS_WATCH_INFORMATION {
    LPVOID FaultingPc;
    LPVOID FaultingVa;
} PSAPI_WS_WATCH_INFORMATION, *PPSAPI_WS_WATCH_INFORMATION;

// Structure for GetProcessMemoryInfo()
typedef struct _PROCESS_MEMORY_COUNTERS {
    DWORD cb;
    DWORD PageFaultCount;
    DWORD PeakWorkingSetSize;
    DWORD WorkingSetSize;
    DWORD QuotaPeakPagedPoolUsage;
    DWORD QuotaPagedPoolUsage;
    DWORD QuotaPeakNonPagedPoolUsage;
    DWORD QuotaNonPagedPoolUsage;
    DWORD PagefileUsage;
    DWORD PeakPagefileUsage;
} PROCESS_MEMORY_COUNTERS;
typedef PROCESS_MEMORY_COUNTERS *PPROCESS_MEMORY_COUNTERS;

#ifdef UNICODE
#define GetModuleBaseName  GetModuleBaseNameW
#define GetModuleFileNameEx  GetModuleFileNameExW
#define GetMappedFilenameEx  GetMappedFilenameExW
#define GetDeviceDriverBaseName  GetDeviceDriverBaseNameW
#define GetDeviceDriverFileName  GetDeviceDriverFileNameW
#else
#define GetModuleBaseName  GetModuleBaseNameA
#define GetModuleFileNameEx  GetModuleFileNameExA
#define GetMappedFilenameEx  GetMappedFilenameExA
#define GetDeviceDriverBaseName  GetDeviceDriverBaseNameA
#define GetDeviceDriverFileName  GetDeviceDriverFileNameA
#endif // !UNICODE

__declspec(dllimport) BOOL EnumProcesses(DWORD *lpidProcess, DWORD bb, DWORD *cbNeeded);
__declspec(dllimport) BOOL EnumProcessModules(HANDLE hProcess, HMODULE *lphModule, DWORD cb, LPDWORD lpcbNeeded);
__declspec(dllimport) DWORD GetModuleBaseNameA(HANDLE hProcess, HMODULE hModule, LPSTR lpBaseName, DWORD nSize);
__declspec(dllimport) DWORD GetModuleBaseNameW(HANDLE hProcess, HMODULE hModule, LPWSTR lpBaseName, DWORD nSize);
__declspec(dllimport) DWORD GetModuleFileNameExA(HANDLE hProcess, HMODULE hModule, LPSTR lpFilename, DWORD nSize);
__declspec(dllimport) DWORD GetModuleFileNameExW(HANDLE hProcess, HMODULE hModule, LPWSTR lpFilename, DWORD nSize);
__declspec(dllimport) BOOL GetModuleInformation(HANDLE hProcess, HMODULE hModule, LPMODULEINFO lpmodinfo, DWORD cb);
__declspec(dllimport) BOOL EmptyWorkingSet(HANDLE hProcess);
__declspec(dllimport) BOOL QueryWorkingSet(HANDLE hProcess, PVOID pv, DWORD cb);
__declspec(dllimport) BOOL InitializeProcessForWsWatch(HANDLE hProcess);
__declspec(dllimport) BOOL GetWsChanges(HANDLE hProcess, PPSAPI_WS_WATCH_INFORMATION lpWatchInfo, DWORD cb);
__declspec(dllimport) DWORD GetMappedFileNameW(HANDLE hProcess, LPVOID lpv, LPWSTR lpFilename, DWORD nSize);
__declspec(dllimport) DWORD GetMappedFileNameA(HANDLE hProcess, LPVOID lpv, LPSTR lpFilename, DWORD nSize);
__declspec(dllimport) BOOL EnumDeviceDrivers(LPVOID *lpImageBase, DWORD cb, LPDWORD lpcbNeeded);
__declspec(dllimport) DWORD GetDeviceDriverBaseNameA(LPVOID ImageBase, LPSTR lpBaseName, DWORD nSize);
__declspec(dllimport) DWORD GetDeviceDriverBaseNameW(LPVOID ImageBase,LPWSTR lpBaseName,DWORD nSize);
__declspec(dllimport) DWORD GetDeviceDriverFileNameA(LPVOID ImageBase, LPSTR lpFilename, DWORD nSize);
__declspec(dllimport) DWORD GetDeviceDriverFileNameW(LPVOID ImageBase, LPWSTR lpFilename, DWORD nSize);
__declspec(dllimport) BOOL GetProcessMemoryInfo(HANDLE Process, PPROCESS_MEMORY_COUNTERS ppsmemCounters, DWORD cb);


/*void main()
{
      DWORD aDword[1024], cbNeeded, cProcesses;
      HINSTANCE hInstance=LoadLibrary("psapi");
      CString sProcess="EnumProcesses";
      FARPROC fProc=GetProcAddress(hInstance, sProcess);
}*/


void PrintModules( DWORD processID )
{
    HMODULE hMods[1024];
    HANDLE hProcess;
    DWORD cbNeeded;
    unsigned int i;

    // Print the process identifier.

    printf( "\nProcess ID: %u\n", processID );

    // Get a list of all the modules in this process.

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

    if( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
    {
        for ( i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )
        {
            char szModName[MAX_PATH];

            // Get the full path to the module's file.

            if ( GetModuleFileNameEx( hProcess, hMods[i], szModName,
                                      sizeof(szModName)))
            {
                // Print the module name and handle value.

                printf("\t%s (0x%08X)\n", szModName, hMods[i] );
            }
        }
    }

    CloseHandle( hProcess );
}

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

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

    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
        return;

    // Calculate how many process identifiers were returned.

    cProcesses = cbNeeded / sizeof(DWORD);

    // Print the name of the modules for each process.

    for ( i = 0; i < cProcesses; i++ )
        PrintModules( aProcesses[i] );
}
Linking...
main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) unsigned long __cdecl GetModuleFileNameExA(void *,struct HINSTANCE__ *,char *,unsigned long)" (__imp_?GetModuleFileNameExA@@YAKPAXPAUHINSTANCE__@@PADK@Z)
main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) int __cdecl EnumProcessModules(void *,struct HINSTANCE__ * *,unsigned long,unsigned long *)" (__imp_?EnumProcessModules@@YAHPAXPAPAUHINSTANCE__@@KPAK@Z)
main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) int __cdecl EnumProcesses(unsigned long *,unsigned long,unsigned long *)" (__imp_?EnumProcesses@@YAHPAKK0@Z)
Debug/EnumProcess.exe : fatal error LNK1120: 3 unresolved externals
LVL 2
graberAsked:
Who is Participating?
 
ZizzerConnect With a Mentor Commented:
I think you need to add psapi.lib to your linker options.  If you're in VC++, open Project Settings and click on the Link tab.  Add psapi.lib to the "Object/library modules" field.  Make sure to do this for all your configurations (using the "Settings For" combo box).

-- Zizzer
0
 
graberAuthor Commented:
Well that was easy points.  I searched for that lib and must have overlooked it.  Just the same zizzer thanks.  I thought I was goint to have to do it with out a lib!
0
All Courses

From novice to tech pro — start learning today.