0

它看起来像这样:

error LNK2005: "unsigned long __cdecl GetModuleBase(void *,
class std::basic_string<char,struct std::char_traits<char>,
class std::allocator<char> > &)" 
(?GetModuleBase@@YAKPAXAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) 
already defined

我最近添加的代码:

#include "Windows.h"
#include <TlHelp32.h>
#include <psapi.h>
#include <string>
#pragma comment(lib, "psapi")
//#pragma comment(lib, "TlHelp32") i could not find where this lib located
using namespace std;

DWORD GetModuleBase(HANDLE hProc, string &sModuleName) 
{ 
   HMODULE *hModules; 
   char szBuf[50]; 
   DWORD cModules; 
   DWORD dwBase = -1; 
   //------ 

   EnumProcessModules(hProc, hModules, 0, &cModules); 
   hModules = new HMODULE[cModules/sizeof(HMODULE)]; 

   if(EnumProcessModules(hProc, hModules, cModules/sizeof(HMODULE), &cModules)) { 
      for(int i = 0; i < cModules/sizeof(HMODULE); i++) { 
         if(GetModuleBaseName(hProc, hModules[i], szBuf, sizeof(szBuf))) { 
            if(sModuleName.compare(szBuf) == 0) { 
               dwBase = (DWORD)hModules[i]; 
               break; 
            } 
         } 
      } 
   } 

   delete[] hModules; 

   return dwBase; 
}

我不明白这是什么,也许我使用了错误的代码?或者需要 TlHelp32.lib,但是 VS 说它找不到这样的静态库。

4

2 回答 2

4

命名空间中有一个GetModuleBase函数Microsoft::WRL

您的代码包含 Microsoft 的功能(在项目的另一部分,它是内部的),因此在链接阶段会引发错误。

更改函数的名称或使用命名空间。

于 2013-10-02T13:27:32.757 回答
0

您的 GetModuleBase() 函数定义了两次,链接器无法解决冲突。搜索“GetModuleBase”,你会找到它。

理想情况下,您将在标头中声明一次函数原型,如下所示:

DWORD GetModuleBase(HANDLE hProc, string &sModuleName);

在头文件的顶部使用标头保护或至少此预处理器指令:

#pragma once

然后在 .cpp 文件中定义一次 GetModuleBase() ,在这个 .cpp 文件中你需要包含头文件。删除此函数的任何其他声明或定义,您的问题应该得到解决。

任何时候遇到这个问题,快速的解决方法是按CTRL-F打开查找提示并搜索函数名,你会很快识别出与这个方法的冲突。

于 2019-01-05T22:02:01.997 回答