简体   繁体   English

错误:LNK1120:5个未解析的外部

[英]error: LNK1120: 5 unresolved externals

I'm trying to get details about installed applications in my post. 我正在尝试在我的帖子中获取有关已安装应用程序的详细信息。 And, I'm getting the below errors: 而且,我得到以下错误:

Code: 码:

#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();
}

Errors: 错误:

LNK1120: 5 unresolved externals LNK1120:5个未解决的外部因素

LNK2019: unresolved external symbol _ imp _RegCloseKey@4 referenced in function "void __cdecl RegistryEnum(void)" (?RegistryEnum@@YAXXZ) LNK2019:函数“void __cdecl RegistryEnum(void)”中引用了未解析的外部符号_ imp _RegCloseKey @ 4(?RegistryEnum @@ YAXXZ)

LNK2019: unresolved external symbol _ imp _RegEnumKeyExW@32 referenced in function "void __cdecl RegistryEnum(void)" (?RegistryEnum@@YAXXZ) LNK2019:函数“void __cdecl RegistryEnum(void)”中引用了未解析的外部符号_ imp _RegEnumKeyExW @ 32(?RegistryEnum @@ YAXXZ)

LNK2019: unresolved external symbol _ imp _RegOpenKeyExW@20 referenced in function "void __cdecl RegistryEnum(void)" (?RegistryEnum@@YAXXZ) LNK2019:函数“void __cdecl RegistryEnum(void)”中引用了未解析的外部符号_ imp _RegOpenKeyExW @ 20(?RegistryEnum @@ YAXXZ)

LNK2019: unresolved external symbol imp__RegQueryValueExW@24 referenced in function "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:未解析的外部符号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: unresolved external symbol wWinMain@16 referenced in function _ _tmainCRTStartup LNK2019: 函数 __tmainCRTStartup中引用的未解析的外部符号wWinMain @ 16

How may I fix this please? 我该如何解决这个问题?

您必须与Advapi32.lib链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM