Visual Studio C++ link with psapi.lib

随声附和 提交于 2019-12-05 02:45:45
boleto

Method 1
If you want to compile from the command line with cl.exe you can use the /link option to specify linker options :

cl /TC program.c /link psapi.lib

Method 2
The following pragma directive causes the linker to search in your source file for the psapi.lib library while linking .

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

Possible reason for your errors can be, if psapi.lib is missing in a list of additional libraries of linker.
To resolve this, use the following /LIBPATH option :

cl /TC program.c /link Psapi.Lib /LIBPATH:C:\MyLibFolder\

Where C:\MyLibFolder specifies a path to the folder, that contains your psapi.lib .

Also, you can try to set the proper /SUBSYSTEM option .
For a Console application use :

/SUBSYSTEM:CONSOLE

Solution to similar problem here .

Example on using the GetModuleInformation function :

#include <windows.h>
#include <stdio.h>    
#include <psapi.h>
#pragma comment( lib, "psapi.lib" )

int main(void)
{    
   MODULEINFO minfo = {0};  
   GetModuleInformation( GetCurrentProcess(), GetModuleHandle( "psapi.dll" ), &minfo, sizeof(minfo) );  
   /* printf("%X", minfo.lpBaseOfDll); /* The load address of the module */   
   return 0;
}

The code has been tested on Windows 7 and XP .
The output from linking session is :

program.c

/out:program.exe 
psapi.lib 
/LIBPATH:C:\MyLibFolder\ 
/SUBSYSTEM:CONSOLE 
/VERBOSE 
program.obj 

Starting pass 1
Processed /DEFAULTLIB:uuid.lib
Processed /DEFAULTLIB:LIBCMT
Processed /DEFAULTLIB:OLDNAMES

Searching libraries
    Searching C:\MyLibFolder\psapi.lib:
      Found _GetModuleInformation@16
        Referenced in program.obj
        Loaded psapi.lib(PSAPI.DLL)
      Found __IMPORT_DESCRIPTOR_PSAPI
        Referenced in psapi.lib(PSAPI.DLL)
        Loaded psapi.lib(PSAPI.DLL)
      Found __NULL_IMPORT_DESCRIPTOR
        Referenced in psapi.lib(PSAPI.DLL)
        Loaded psapi.lib(PSAPI.DLL)
        ...

If vsvars32.bat and all appropriate environment variables in your Visual Studio are set correctly the above linker options will produce a valid executable(.exe) file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!