When any of these defines are included in the code, they produce an ‘unresolved external symbol’ error from the linker:
const auto getPropResult = SetupDiGetDevicePropertyW(
hDevInfo,
&did,
&DEVPKEY_Device_RemovalPolicy, // Ask for the "removal policy"
&devPropType,
(BYTE*)buffer,
sizeof(buffer),
&dwSize,
0);
Error LNK2001 unresolved external symbol _DEVPKEY_Device_RemovalPolicy
There is this inconclusive Q/A which does not help:
unresolved external symbol _DEVPKEY_Device_BusReportedDeviceDesc
What is still missing after Devpkey.h
is included?
The linker error LNK2001 unresolved external symbol _DEVPKEY_Device_RemovalPolicy
(or similar for other DEVPKEY_
constants like DEVPKEY_Device_HardwareIds
) typically means the symbol is declared in a header, but not defined in any linked library.
Problem Summary:
- You included
Devpkey.h
, which provides the declaration of DEVPKEY_Device_RemovalPolicy
, but not its definition.
- These property keys are defined as
DEVPROPKEY
structures, and while they’re declared in the header, their actual definitions (the backing storage) are in a library you also need to link against.
Solution:
To resolve this issue, you need to:
Link against Cfgmgr32.lib
This library contains the definitions of many DEVPKEY_
constants.
Additional Notes:
- The
DEVPKEY_
values are extern const
variables in the Windows SDK. The linker needs to resolve them to a compiled library object, not just the header.
- This applies to:
DEVPKEY_Device_RemovalPolicy
DEVPKEY_Device_BusReportedDeviceDesc
DEVPKEY_Device_HardwareIds
, etc.
Example fix in your build:
If you’re using MSVC (Visual Studio), add this to your .cpp
file:
#pragma comment(lib, "Cfgmgr32.lib")
Or, if you’re using a build system like MSBuild or CMake, ensure Cfgmgr32.lib
is in your link settings.