#include "stdafx.h"
#include <windows.h>
/*
* where use the typedef keyword, you can use the originality instead such as use DWORD instead of DEVINST
*/
typedef DWORD DEVINST;
typedef DEVINST *PDEVINST;
typedef DWORD RETURN_TYPE;
typedef RETURN_TYPE CONFIGRET;
typedef CHAR *DEVINSTID_A; // Device ID ANSI name.
#define CM_LOCATE_DEVNODE_NORMAL 0x00000000
#define CR_SUCCESS 0x00000000
/*
* this function prototype for get CM_Locate_DevNodeW function address
* how to use CM_Locate_DevNodeW function, you may see about DDK Documentation
*/
typedef CONFIGRET (__stdcall *CM_LOCATE_DEVNODE_FUNC) (
OUT PDEVINST pdnDevInst,
IN DEVINSTID_A pDeviceID, OPTIONAL
IN DWORD ulFlags
);
/*
* this function prototype for get CM_Reenumerate_DevNode function address
* how to use CM_Reenumerate_DevNode function, you may see about DDK Documentation
*/
typedef CONFIGRET (__stdcall *CM_Reenumerate_DevNode_FUNC) (
IN DEVINST dnDevInst,
IN DWORD ulFlags
);
CM_LOCATE_DEVNODE_FUNC _Get_Locate_DevNode = NULL;
CM_Reenumerate_DevNode_FUNC _Get_Reenumerate_DevNode = NULL;
HINSTANCE handle;
DEVINST devInst;
CONFIGRET status;
handle = LoadLibrary("cfgmgr32.dll");
if ( handle == 0 ) {
printf("LoadLibrary failed!");
}
_Get_Locate_DevNode = (CM_LOCATE_DEVNODE_FUNC)GetProcAddress(handle, "CM_Locate_DevNodeW");
if ( _Get_Locate_DevNode != NULL ) {
status = _Get_Locate_DevNode(&devInst, NULL, CM_LOCATE_DEVNODE_NORMAL);
if ( status != CR_SUCCESS) {
printf("_Get_Locate_DevNode failed!");
}
}
_Get_Reenumerate_DevNode = (CM_Reenumerate_DevNode_FUNC)GetProcAddress(handle, "CM_Reenumerate_DevNode");
if ( _Get_Reenumerate_DevNode != NULL ) {
status = _Get_Reenumerate_DevNode(devInst, 0);
if ( status != CR_SUCCESS) {
printf("_Get_Reenumerate_DevNode failed!");
}
}
FreeLibrary(handle);
本文介绍如何通过Windows API函数动态加载cfgmgr32.dll,并调用CM_Locate_DevNodeW与CM_Reenumerate_DevNode函数来定位设备实例和重新枚举设备。这些操作对于开发涉及硬件管理和诊断的应用程序非常有用。
1万+





