错误 LNK2019:函数"private: 结构_DISPLAY_DEVICEA __thiscall"中引用未解析的外部符号__imp__EnumDisplayDevicesA@16

error LNK2019: unresolved external symbol __imp__EnumDisplayDevicesA@16 referenced in function "private: struct _DISPLAY_DEVICEA __thiscall

本文关键字:符号 EnumDisplayDevicesA@16 imp 引用 外部 thiscall private 函数 LNK2019 结构 DISPLAY      更新时间:2023-10-16

我想做的是获取我的屏幕信息,如分辨率/刷新率等:

#pragma once
#include < windows.h >
#include <string>
#include <sstream>
struct DesktopScreenInfo
{
    int Width;
    int Height;
    int ScreenDepth;
    int FrameRate;
    std::string ScreenInfoString;
};
class DesktopScreen
{
public:
    DesktopScreen(void);
    ~DesktopScreen(void);
    DesktopScreenInfo GetScreenInfo();
private:
    DISPLAY_DEVICE GetPrimaryDevice();
};
#include "DesktopScreen.h"
DesktopScreen::DesktopScreen(void)
{
}
DesktopScreen::~DesktopScreen(void)
{
}
DISPLAY_DEVICE DesktopScreen::GetPrimaryDevice(){
    int index=0;
    DISPLAY_DEVICE dd;
    dd.cb = sizeof(DISPLAY_DEVICE);
    while (EnumDisplayDevices(NULL, index++, &dd, 0))
    {
        if (dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) return dd;
    }
    return dd;
}
DesktopScreenInfo DesktopScreen::GetScreenInfo(){
    DISPLAY_DEVICE dd = GetPrimaryDevice();
    DesktopScreenInfo info;
    DEVMODE dm;
    dm.dmSize = sizeof(DEVMODE);
    if (!EnumDisplaySettings(dd.DeviceName, ENUM_CURRENT_SETTINGS, &dm))
    {
        //printf("EnumDisplaySettings failed:%dn", GetLastError());
        return info;
    }
    info.Width = dm.dmPelsWidth;
    info.Height = dm.dmPelsHeight;
    info.ScreenDepth = dm.dmBitsPerPel;
    info.FrameRate = dm.dmDisplayFrequency;
    std::stringstream ss;
    ss << info.Width << "x" << info.Height << ":" << info.ScreenDepth << "@" << info.FrameRate;
    info.ScreenInfoString = ss.str();
}

但当我试图构建它时,它会给我这个错误

Error   2   error LNK2019: unresolved external symbol __imp__EnumDisplayDevicesA@16 referenced in function "private: struct _DISPLAY_DEVICEA __thiscall DesktopScreen::GetPrimaryDevice(void)" (?GetPrimaryDevice@DesktopScreen@@AAE?AU_DISPLAY_DEVICEA@@XZ)
Error   3   error LNK2019: unresolved external symbol __imp__EnumDisplaySettingsA@12 referenced in function "public: struct DesktopScreenInfo __thiscall DesktopScreen::GetScreenInfo(void)" (?GetScreenInfo@DesktopScreen@@QAE?AUDesktopScreenInfo@@XZ)

我不知道出了什么问题:S

您需要链接到user32.lib

(这些API的文档在页面底部告诉您要链接到哪个库。)

它找不到EnumDisplayDevices或EnumDisplaySettings。

弄清楚这些是在哪里定义的,如果你在.h中定义了它们,请确保其中包含这些。我没有看到任何#include"EnumDisplayDevices.h"或类似的东西,在您发布的代码中,我没有看到声明的枚举。

无论如何,之所以会出现错误,是因为链接器不知道EnumDisplayDevices或EnumDisplaySettings在哪里声明,因此无法正确链接。