未解析的外部符号 _DEVPKEY_Device_BusReportedDeviceDesc

Aed*_*seh 2 c++ linker driver setupapi visual-studio

对于连接到我的机器的设备,我想检索 device-property Bus Reported Device Description。为此,我使用了Setup API 的SetupDiGetDeviceProperty函数。在devpkey.h 中,我找到了定义DEVPKEY_Device_BusReportedDeviceDesc

但是,如果我使用DEVPKEY_Device_BusReportedDeviceDesc,我会在链接时收到未解析的外部符号 _DEVPKEY_Device_BusReportedDeviceDesc

这是我的代码(仅包含重现问题的最少代码):

#include "stdafx.h"

#include <Windows.h>
#include <devpropdef.h>
#include <devpkey.h>

int main()
{
    DEVPROPKEY x = DEVPKEY_Device_BusReportedDeviceDesc;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是完整的错误代码:

错误 LNK2001:未解析的外部符号 _DEVPKEY_Device_BusReportedDeviceDesc

我该如何解决这个问题?

Aed*_*seh 6

要解决此问题,您需要包含initguid.h。这包括必须在devpropdef.hdevpkey.h之前。

#include "stdafx.h"
#include <initguid.h>   // include before devpropdef.h
#include <Windows.h>
#include <devpropdef.h>
#include <devpkey.h>

int main()
{
    DEVPROPKEY x = DEVPKEY_Device_BusReportedDeviceDesc;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)