Retrieving PCI coordinates by Windows' API (user mode)

≯℡__Kan透↙ 提交于 2019-12-05 10:41:45

I've eventually found a simple solution (it was just a matter of digging into MSDN).

This minimal code finds the device's PCI coordinates in terms of bus/slot/function:

DWORD bus, addr, slot, func;
HDEVINFO h; // Obtained by SetupDiGetClassDevs
SP_DEVINFO_DATA d; // Filled by SetupDiGetDeviceInterfaceDetail

SetupDiGetDeviceRegistryProperty(h,&d,SPDRP_BUSNUMBER,NULL,&bus,sizeof(bus),NULL);
SetupDiGetDeviceRegistryProperty(h,&d,SPDRP_ADDRESS,NULL,&addr,sizeof(addr),NULL);
slot = (addr >> 16) & 0xFFFF;
func = addr & 0xFFFF;

Note: for real production the output buffer's size must be obtained by a previous call of the API function in order to allocate it dynamically, and error checks must be added, of course.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!