SetupDiGetClassDevs SetupDiEnumDeviceInfo SetupDiGetDeviceRegistryProperty 枚举设备手机

来源:互联网 发布:王勇java 编辑:程序博客网 时间:2024/06/06 00:23

1函数定义编辑

HDEVINFO
SetupDiGetClassDevs(
const GUID* ClassGuid,
PCTSTR Enumerator,
HWND hwndParent,
DWORD Flags
);

2参数说明编辑

输入参数:
PGUIDClassGuid
在创建设备列表的时候提供一个指向GUID的指针。如果设定了标志DIGCF_ALLCLASSES,则这个参数可以忽略,且列表结果中包括所有已经安装的设备类别。
PCTSTREnumerator
提供包含设备实例的枚举注册表分支下的键名,可以通过它获取设备信息。如果这个参数没有指定,则要从整个枚举树中获取所有设备实例的设备信息。
HWNDhwndParent
提供顶级窗口的句柄,所有用户接口可以使用它来与成员联系。
DWORDFlags
提供在设备信息结构中使用的控制选项。可以是以下数值:
DIGCF_PRESENT - 只返回当前存在的设备。
DIGCF_ALLCLASSES - 返回所有已安装的设备。如果这个标志设置了,ClassGuid参数将被忽略。
DIGCF_PROFILE - 只返回当前硬件配置文件中的设备。
DIGCF_INTERFACEDEVICE - 返回所有支持的设备。
DIGCF_DEFAULT - 只返回与系统默认设备相关的设备。

3返回值编辑

HDEVINFO
如果函数运行成功,返回设备信息结构的句柄,该结构包含与指定参数匹配的所有已安装设备。如果失败,则返回INVALID_HANDLE_VALUE。调用GetLastError可以获得更多错误信息。

4说明编辑

使用此函数,需要包含头文件setupapi.h。
此外,在project setting中的link页面需要添加setupapi.lib。
在setupapi.h中有如下定义:
typedef PVOID HDEVINFO;
即HDEVINFO是个无类型指针
/***********************************************************************************************************************************/


SetupDiEnumDeviceInfo-使用SetupDi系列函数进行设备信息的管理(二)

 

BOOLEAN
SetupDiEnumDeviceInfo(
IN HDEVINFO DeviceInfoSet,
IN DWORD MemberIndex,
OUT PSP_DEVINFO_DATA DeviceInfoData
);

SetupDiEnumDeviceInfo 枚举指定设备信息集合的成员,并将数据放在PSP_DEVINFO_DATA中
参数说明:

DeviceInfoSet
提供一个设备信息集合的句柄

MemberIndex
指定一个要取得的设备信息成员序号,从0开始

DeviceInfoData
指向SP_DEVINFO_DATA结构的指针,关于指定成员的返回信息就放在该结构中

Return ValueThe

成功返回True,否则返回False)

如果要枚举全部设备信息成员,装载者首先应该将MemberIndex设为0调用SetupDiEnumDeviceInfo,然后递增MemberIndex(使用一个for循环),调用SetupDiEnumDeviceInfo,直至所有成员全部遍历(此时函数返回False,并且GetLastError返回ERROR_NO_MORE_ITEMS)

/***********************************************************************************************************************/

SetupDiGetDeviceRegistryProperty-使用SetupDi系列函数进行设备信息的管理(三)

 (2012-08-08 13:56:34)
转载
标签: 

usb

分类: 应用程序与硬件设备
SetupDiGetDeviceRegistryProperty-使用SetupDi系列函数进行设备信息的管理(三)
 

WINSETUPAPI BOOL WINAPI
SetupDiGetDeviceRegistryProperty(
IN HDEVINFO DeviceInfoSet,
IN PSP_DEVINFO_DATA DeviceInfoData,
IN DWORD Property,
OUT PDWORD PropertyRegDataType, OPTIONAL
OUT PBYTE PropertyBuffer,
IN DWORD PropertyBufferSize,
OUT PDWORD RequiredSize OPTIONAL
);

SetupDiGetDeviceRegistryProperty:获得单个装置的详细资料

参数说明:

DeviceInfoSet

设备信息句柄

DeviceInfoData

SP_DEVINFO_DATA结构体,包含DeviceInfoSet 中的设备信息

Property

取以下的值:

SPDRP_ADDRESS 查询设备的地址

SPDRP_BUSNUMBER 查询设备的bus号

SPDRP_BUSTYPEGUID 查询设备的GUID号

SPDRP_CAPABILITIES

The function retrieves a bitwise OR of the following CM_DEVCAP_Xxx flags in a DWORD. The device capabilities that are represented by these flags correspond to the device capabilities that are represented by the members of the DEVICE_CAPABILITIES structure. The CM_DEVCAP_Xxx constants are defined in cfgmgr32.h.

CM_DEVCAP_Xxx flagCorresponding DEVICE_CAPABILITIES structure memberCM_DEVCAP_LOCKSUPPORTEDLockSupportedCM_DEVCAP_EJECTSUPPORTEDEjectSupportedCM_DEVCAP_REMOVABLERemovableCM_DEVCAP_DOCKDEVICEDockDeviceCM_DEVCAP_UNIQUEIDUniqueIDCM_DEVCAP_SILENTINSTALLSilentInstallCM_DEVCAP_RAWDEVICEOKRawDeviceOKCM_DEVCAP_SURPRISEREMOVALOKSurpriseRemovalOKCM_DEVCAP_HARDWAREDISABLEDHardwareDisabledCM_DEVCAP_NONDYNAMICNonDynamic
SPDRP_CHARACTERISTICS
The function retrieves a bitwise OR of a device's characteristics flags in a DWORD. For a description of these flags, which are defined in wdm.h and ntddk.h, see the IoCreateDevicefunction's DeviceCharacteristics parameter.
SPDRP_CLASS
The function retrieves a REG_SZ string that contains the device setup class of a device.
SPDRP_CLASSGUID
The function retrieves a REG_SZ string that contains the GUID that represents the device setup class of a device.
SPDRP_COMPATIBLEIDS
The function retrieves a REG_MULTI_SZ string containing the list of compatible IDs for a device. For information about compatible IDs, see Device Identification Strings.
SPDRP_CONFIGFLAGS
The function retrieves a bitwise OR of a device's configuration flags in a DWORD value. The configuration flags are represented by the CONFIGFLAG_Xxx bitmasks that are defined in regstr.h.
SPDRP_DEVICE_POWER_DATA
(Windows XP and later) The function retrieves a CM_POWER_DATA structure containing the device's power management information.
SPDRP_DEVICEDESC
The function retrieves a REG_SZ string containing the description of a device.
SPDRP_DEVTYPE
The function retrieves a DWORD value representing the device's type (see Specifying Device Types).
SPDRP_DRIVER
The function retrieves a string identifying the device's software key (sometimes called the driver key). See Driver Information in the Registry.
SPDRP_ENUMERATOR_NAME
The function retrieves a REG_SZ string containing the name of the device's enumerator.
SPDRP_EXCLUSIVE
The function retrieves a DWORD value that indicates whether a user can obtain exclusive use of the device. The returned value is one if exclusive use is allowed, or zero otherwise. For more information, see IoCreateDevice.
SPDRP_FRIENDLYNAME
The function retrieves a REG_SZ string that contains the friendly name of a device.
SPDRP_HARDWAREID
The function retrieves a REG_MULTI_SZ string containing the list of hardware IDs for a device. For information about hardware IDs, see Device Identification Strings.
SPDRP_INSTALL_STATE
(Windows XP and later) The function retrieves a DWORD value that indicates the installation state of a device. The installation state is represented by one of the CM_INSTALL_STATE_Xxx values that are defined in Cfgmgr32.h. The CM_INSTALL_STATE_Xxx values correspond to the DEVICE_INSTALL_STATE enumeration values.
SPDRP_LEGACYBUSTYPE
The function retrieves the device's legacy bus type as an INTERFACE_TYPE value (defined in wdm.hand ntddk.h).
SPDRP_LOCATION_INFORMATION
The function retrieves a REG_SZ string that contains the hardware location of a device.
SPDRP_LOCATION_PATHS
(Windows Server 2003 and later) The function retrieves a REG_MULTI_SZ string that represents the location of the device in the device tree.
SPDRP_LOWERFILTERS
The function retrieves a REG_MULTI_SZ string that contains the names of a device's lower-filter drivers.
SPDRP_MFG
The function retrieves a REG_SZ string that contains the name of the device manufacturer.
SPDRP_PHYSICAL_DEVICE_OBJECT_NAME
The function retrieves a REG_SZ string that contains the name that is associated with the device's PDO. For more information, see IoCreateDevice.
SPDRP_REMOVAL_POLICY
(Windows XP and later) The function retrieves the device's current removal policy as a DWORD containing one of the CM_REMOVAL_POLICY_Xxx values defined in cfgmgr32.h.
SPDRP_REMOVAL_POLICY_HW_DEFAULT
(Windows XP and later) The function retrieves the device's hardware-specified default removal policy as a DWORD containing one of the CM_REMOVAL_POLICY_Xxx values defined in cfgmgr32.h.
SPDRP_REMOVAL_POLICY_OVERRIDE
(Windows XP and later) The function retrieves the device's override removal policy (if it exists) from the registry, as a DWORD containing one of the CM_REMOVAL_POLICY_Xxx values defined incfgmgr32.h.
SPDRP_SECURITY
The function retrieves a SECURITY_DESCRIPTOR structure for a device.
SPDRP_SECURITY_SDS
The function retrieves a REG_SZ string that contains the device's security descriptor. The format of security descriptor strings is described in Microsoft Windows SDK documentation.
SPDRP_SERVICE
The function retrieves a REG_SZ string containing the service name for a device.
SPDRP_UI_NUMBER
The function retrieves a DWORD value set to the value of the UINumber member of the device's DEVICE_CAPABILITIES structure.
SPDRP_UI_NUMBER_DESC_FORMAT
The function retrieves a format string (REG_SZ) used to display the UINumber value.
SPDRP_UPPERFILTERS
The function retrieves a REG_MULTI_SZ string that contains the names of a device's upper filter drivers.
PropertyRegDataType
A pointer to a variable that receives the data type of the property that is being retrieved. This is one of the standard registry data types. This parameter is optional and can be NULL.
PropertyBuffer
A pointer to a buffer that receives the property that is being retrieved. If this parameter is set to NULL, and PropertyBufferSize is also set to zero, the function returns the required size for the buffer in RequiredSize.
PropertyBufferSize
The size, in bytes, of the PropertyBuffer buffer.
RequiredSize
A pointer to a variable of type DWORD that receives the required size, in bytes, of thePropertyBuffer buffer that is required to hold the data for the requested property. This parameter is optional and can be NULL.

Return Value

SetupDiGetDeviceRegistryProperty returns TRUE if the call was successful. Otherwise, it returns FALSE and the logged error can be retrieved with a call to GetLastError.SetupDiGetDeviceRegistryProperty returns the ERROR_INVALID_DATA error code if the requested property does not exist for a device or if the property data is not valid.


函数更改

/*
函数说明: DeviceCheck
参数 pOut_Type  代表手机的品牌类型
参数 pOut_mode  代表启动模式 fastboot(刷机模式)   还有一个正常模式


返回值:  1代表已安装驱动   2代表未安装驱动
*/


BOOL CDriver::DeviceCheck(int *pOut_Type, int *pOut_mode)
{  
HDEVINFO hDevInfo;  
SP_DEVINFO_DATA DeviceInfoData;  
DWORD i;  
BOOL re = FALSE;


//获取系统设备信息集合
hDevInfo = SetupDiGetClassDevsA(NULL,  "USB",  0,  DIGCF_PRESENT | DIGCF_ALLCLASSES ); 
if (hDevInfo == INVALID_HANDLE_VALUE)
{
return 1;
}


DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);  
for (i=0; SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData);i++)  //枚举出集合中的每一个设备
{  
DWORD DataT;  
char* buffer = NULL;  
DWORD buffersize = 0;  
//SPDRP_COMPATIBLEIDS  兼容ID
//SPDRP_HARDWAREID硬件ID
while (!SetupDiGetDeviceRegistryPropertyA( hDevInfo,  &DeviceInfoData,  SPDRP_COMPATIBLEIDS,  &DataT,  (PBYTE)buffer,  buffersize,  &buffersize))  
{  
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)//第一次应该是失败的。然后分配空间
{  
if (buffer) LocalFree(buffer);  
buffer = (char*)LocalAlloc(LPTR,buffersize);
}  
else  
break;  



int PhoneType=0;
BOOL Be= FALSE;


//检测硬件ID
if (buffer!=NULL)
{
CStringA sHaraid = buffer;
sHaraid.MakeUpper();
//if( sHaraid.Find( "&MI_01") != -1)//是不是一个正常模式下的手机usb设备?
{
*pOut_mode = 1;
if (sHaraid.Find("CLASS_FF&SUBCLASS_42&PROT")!=-1)//相等
{
CStringA guid111;
buffer =NULL;  
buffersize=0; 
while (!SetupDiGetDeviceRegistryPropertyA(  hDevInfo, &DeviceInfoData, SPDRP_HARDWAREID,  &DataT,  (PBYTE)buffer,  buffersize,  &buffersize))  
{  
if (GetLastError() ==  ERROR_INSUFFICIENT_BUFFER)  
{  
if (buffer) LocalFree(buffer);  
buffer = (char*)LocalAlloc(LPTR,buffersize);  
}  
else  
break;  
}
g_HardID = buffer;
PhoneType=1;
*pOut_Type = PhoneType;
Be = TRUE;
//AfxMessageBox(L"发现手机");
}
}
}
if (buffer) LocalFree(buffer);




//找到手机了,检测是否有驱动
if (Be)
{
//检测硬件ID
CStringA guid, ser;
buffer =NULL;  
buffersize=0; 
while (!SetupDiGetDeviceRegistryProperty(  hDevInfo, &DeviceInfoData, SPDRP_CLASSGUID,  &DataT,  (PBYTE)buffer,  buffersize,  &buffersize))  
{  
if (GetLastError() ==  ERROR_INSUFFICIENT_BUFFER)  
{  
if (buffer) LocalFree(buffer);  
buffer = (char*)LocalAlloc(LPTR,buffersize);  
}  
else  
break;  
}
guid = buffer;//获得硬件ID
if (buffer) LocalFree(buffer);




//检测是否有驱动
buffer =NULL;  
buffersize=0; 
while (!SetupDiGetDeviceRegistryProperty(  hDevInfo, &DeviceInfoData, SPDRP_SERVICE,  &DataT,  (PBYTE)buffer,  buffersize,  &buffersize))  
{  
if (GetLastError() ==  ERROR_INSUFFICIENT_BUFFER)  
{  
if (buffer) LocalFree(buffer);  
buffer = (char*)LocalAlloc(LPTR,buffersize);  
}  
else  
break;  
}
ser = buffer; //获得驱动
if (buffer) LocalFree(buffer);




//判断是否有硬件ID和驱动
if (!guid.IsEmpty() && !ser.IsEmpty())
{
re = TRUE; //有驱动和硬件ID返回True
return re;
}
else
{
re = FALSE; //没有驱动
return re;
}
}
else
re =FALSE;
  }  


return re;
}


0 0
原创粉丝点击