【问题标题】:error: LNK1120: 5 unresolved externals错误:LNK1120:5 个未解决的外部
【发布时间】:2014-05-10 12:58:29
【问题描述】:

我试图在我的帖子中获取有关已安装应用程序的详细信息。而且,我收到以下错误:

代码:

#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 个未解决的外部问题

LNK2019:未解析的外部符号 _imp_RegCloseKey@4 在中引用 函数“void __cdecl RegistryEnum(void)”(?RegistryEnum@@YAXXZ)

LNK2019:引用了未解析的外部符号 _imp_RegEnumKeyExW@32 在函数“void __cdecl RegistryEnum(void)”(?RegistryEnum@@YAXXZ)

LNK2019:引用了未解析的外部符号 _imp_RegOpenKeyExW@20 在函数“void __cdecl RegistryEnum(void)”(?RegistryEnum@@YAXXZ)

LNK2019:未解析的外部符号 imp__RegQueryValueExW@24 在函数 "class std::basic_string,class std::allocator > __cdecl 中引用 RegistryQueryValue(struct HKEY *,wchar_t const *)" (?RegistryQueryValue@@YA?AV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@W@2@@std@@PAUHKEY_@@PB_W@Z)

LNK2019:未解析的外部符号 wWinMain@16 在中引用 函数__tmainCRTStartup

请问我该如何解决这个问题?

【问题讨论】:

  • 他们是链接相关的错误。在附加依赖选项卡中添加 Advapi32.lib(或他们现在命名的任何名称)。错误表明 IDE 无法找到具有上述功能的
  • 我试过这样添加,#pragma comment(lib, "Advapi32.lib")
  • 好的。它在 VC++2010 中构建良好。没什么特别的。
  • @SChepurin,这是来自 VS2013 吗?库链接?
  • 没有别的了。 IDE 找不到 Advapi32.lib。

标签: c++


【解决方案1】:

您必须链接到Advapi32.lib

【讨论】:

  • 您的代码看起来不错,您使用的是什么编程环境?
  • Visua Studio 2013 Ultimate
  • 尝试从头开始创建一个新项目并在其中使用一些 Reg* 函数并尝试它是否可以编译。
猜你喜欢
  • 2014-05-07
  • 1970-01-01
  • 2015-01-11
  • 2017-09-25
  • 2012-07-20
  • 1970-01-01
  • 2011-11-16
  • 1970-01-01
  • 2012-06-03
相关资源
最近更新 更多