2014-03-31 51 views
1

我試圖在我的文章中獲取有關已安裝應用程序的詳細信息。而且,我發現了以下錯誤:錯誤:LNK1120:5個未解析的外部信息

代碼:

#include <iostream> 
#include <string> 
#include <windows.h> 

using namespace std; 

#ifdef _UNICODE 
#define tcout  wcout 
#define tstring  wstring 
#else 
#define tcout  cout 
#define tstring  string 
#endif 

tstring RegistryQueryValue(HKEY hKey, 
    LPCTSTR szName) 
{ 
    tstring value; 

    DWORD dwType; 
    DWORD dwSize = 0; 

    if (::RegQueryValueEx(
     hKey,     // key handle 
     szName,     // item name 
     NULL,     // reserved 
     &dwType,    // type of data stored 
     NULL,     // no data buffer 
     &dwSize     // required buffer size 
     ) == ERROR_SUCCESS && dwSize > 0) 
    { 
     value.resize(dwSize); 

     ::RegQueryValueEx(
      hKey,     // key handle 
      szName,     // item name 
      NULL,     // reserved 
      &dwType,    // type of data stored 
      (LPBYTE)&value[0],  // data buffer 
      &dwSize     // available buffer size 
      ); 
    } 

    return value; 
} 

void RegistryEnum() 
{ 
    HKEY hKey; 
    LONG ret = ::RegOpenKeyEx(
     HKEY_LOCAL_MACHINE,  // local machine hive 
     __TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall"), // uninstall key 
     0,      // reserved 
     KEY_READ,    // desired access 
     &hKey     // handle to the open key 
     ); 

    if (ret != ERROR_SUCCESS) 
     return; 

    DWORD dwIndex = 0; 
    DWORD cbName = 1024; 
    TCHAR szSubKeyName[1024]; 

    while ((ret = ::RegEnumKeyEx(
     hKey, 
     dwIndex, 
     szSubKeyName, 
     &cbName, 
     NULL, 
     NULL, 
     NULL, 
     NULL)) != ERROR_NO_MORE_ITEMS) 
    { 
     if (ret == ERROR_SUCCESS) 
     { 
      HKEY hItem; 
      if (::RegOpenKeyEx(hKey, szSubKeyName, 0, KEY_READ, &hItem) != ERROR_SUCCESS) 
       continue; 

      tstring name = RegistryQueryValue(hItem, __TEXT("DisplayName")); 
      tstring publisher = RegistryQueryValue(hItem, __TEXT("Publisher")); 
      tstring version = RegistryQueryValue(hItem, __TEXT("DisplayVersion")); 
      tstring location = RegistryQueryValue(hItem, __TEXT("InstallLocation")); 

      if (!name.empty()) 
      { 
       tcout << name << endl; 
       tcout << " - " << publisher << endl; 
       tcout << " - " << version << endl; 
       tcout << " - " << location << endl; 
       tcout << endl; 
      } 

      ::RegCloseKey(hItem); 
     } 
     dwIndex++; 
     cbName = 1024; 
    } 
    ::RegCloseKey(hKey); 
} 

void main(){ 
    RegistryEnum(); 
} 

錯誤:

LNK1120: 5 unresolved externals

LNK2019: unresolved external symbol _imp[email protected] referenced in function "void __cdecl RegistryEnum(void)" ([email protected]@YAXXZ)

LNK2019: unresolved external symbol _imp[email protected] referenced in function "void __cdecl RegistryEnum(void)" ([email protected]@YAXXZ)

LNK2019: unresolved external symbol _imp[email protected] referenced in function "void __cdecl RegistryEnum(void)" ([email protected]@YAXXZ)

LNK2019: unresolved external symbol [email protected] referenced in function "class std::basic_string,class std::allocator > __cdecl RegistryQueryValue(struct HKEY *,wchar_t const *)" ([email protected]@[email protected][email protected][email protected]@@[email protected][email protected]@@[email protected]@PAUHKEY[email protected]@[email protected])

LNK2019: unresolved external symbol [email protected] referenced in function __tmainCRTStartup

如何我可以解決這個問題嗎?

+0

他們正在鏈接相關的錯誤。在其他依賴性選項卡中添加Advapi32.lib(或現在提供的任何名稱)。錯誤表示IDE無法找到提供函數的*庫*。 – SChepurin

+0

我試圖像這樣添加它,'#pragma comment(lib,「Advapi32.lib」)' – user3296338

+0

好吧。它在VC++ 2010中建立良好。沒有什麼花哨。 – SChepurin

回答

2

您必須鏈接到Advapi32.lib

+0

沒有區別。 – user3296338

+0

你的代碼看起來不錯,你使用什麼編程環境? –

+0

Visua Studio 2013 Ultimate – user3296338

相關問題