2013-10-02 46 views
0

它看起來像這樣:編譯錯誤「已定義」

error LNK2005: "unsigned long __cdecl GetModuleBase(void *, 
class std::basic_string<char,struct std::char_traits<char>, 
class std::allocator<char> > &)" 
([email protected]@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@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說它找不到這樣的靜態庫。

+0

你的標題是否用'#ifndef'宏守護? –

+0

當然不是雜注。 – Loryan55

+2

這是一個鏈接錯誤,而不是編譯器錯誤。它表明你有兩個名爲'GetModuleBase'的函數具有相同的簽名。也許你連接兩次相同的代碼? –

回答

4

命名空間Microsoft::WRL中有一個GetModuleBase function

您的代碼包含微軟的功能(在項目的另一部分,它是內部的),所以在鏈接階段會產生錯誤。

更改函數的名稱或使用命名空間。

+0

我想知道這一點,但在C++程序中,只要簽名不同,應該可以有兩個具有相同名稱的不同函數。 – john

+0

謝謝,但我已經解決了我的問題(我很抱歉)它是包含錯誤(我把函數代碼添加到頭沒有聲明,可能這是問題)。 – Loryan55

+0

@john:你是對的,但正如錯誤所述,鏈接階段出現了錯誤(C++編譯器說的都可以)。然後如Loryan55所解釋的那樣,宣言失蹤了。 – AndrewQ