如何取得系统资源

BuZhang_AP97091 2003-09-11 09:27:46
如何取得系统硬件资源,如串并口、USB口等。
...全文
171 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
AthlonxpX86 2003-09-11
  • 打赏
  • 举报
回复
SetupDiGetDeviceRegistryProperty
SetupDiGetDeviceRegistryProperty retrieves the specified Plug and Play device property.

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
);
Parameters
DeviceInfoSet
Supplies a handle to the device information set containing information about the device instance for which to retrieve a Plug and Play property.
DeviceInfoData
Supplies a pointer to an SP_DEVINFO_DATA structure indicating the device instance for which to retrieve the Plug and Play property.
Property
Supplies one of the following values, indicating the property to be retrieved:
SPDRP_ADDRESS
The function retrieves the device's address.
SPDRP_BUSNUMBER
The function retrieves the device's bus number.
SPDRP_BUSTYPEGUID
The function retrieves the GUID for the device's bus type.
SPDRP_CAPABILITIES
The function retrieves the device's Capabilities registry value, which is a DWORD containing device capability flags. These flags are represented by CM_DEVCAP_xxxx bit masks defined in cfgmgr32.h.
SPDRP_CHARACTERISTICS
The function retrieves the device's Characteristics registry value, which is a DWORD containing the device's characteristics flags. For a description of these flags, which are defined in wdm.h and ntddk.h, see the IoCreateDevice function's DeviceCharacteristics parameter.
SPDRP_CLASS
The function retrieves the device's Class registry value, which is a string respresenting the setup class for the device.
SPDRP_CLASSGUID
The function retrieves the device's ClassGUID registry value, which is a string containing the GUID that represents the device's setup class.
SPDRP_COMPATIBLEIDS
The function retrieves the device's CompatibleID registry value, which is a multi-string containing the device's compatible IDs.
SPDRP_CONFIGFLAGS
The function retrieves the device's ConfigFlags registry value, which is a DWORD value containing configuration flags. These flags are represented by CONFIGFLAG_xxxx bit masks 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 the device's DeviceDesc registry value, which is a string containing a description of the device.
SPDRP_DEVTYPE
The function retrieves a DWORD value representing the device's type (see Specifying Device Types).
SPDRP_DRIVER
The function retrieves the device's Driver registry value, which is 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 the name of the device's Enumerator registry value, which is string representing the name of the device's enumerator.
SPDRP_EXCLUSIVE
The function retrieves the value of the device's Exclusive registry value, which is a DWORD indicating 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 the device's FriendlyName registry value, which is a string respresenting the device's displayable friendly name.
SPDRP_HARDWAREID
The function retrieves the device's HardwareID registry value, which is a multi-string containing the device's hardware IDs.
SPDRP_LEGACYBUSTYPE
The function retrieves the device's legacy bus type as an INTERFACE_TYPE value (defined in wdm.h and ntddk.h).
SPDRP_LOCATION_INFORMATION
The function retrieves the device's LocationInformation registry value, which is a string describing the device's hardware location.
SPDRP_LOWERFILTERS
The function retrieves device's LowerFilters registry value, which is a multi-string representing the names of the device's lower filter drivers.
SPDRP_MFG
The function retrieves the device's Mfg registry value, which is a string representing the device's manufacturer.
SPDRP_PHYSICAL_DEVICE_OBJECT_NAME
The function retrieves the device's PhysicalDeviceObject registry value, which is a string representing the name associated with the drivce'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_Xxxx 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_Xxxx 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_Xxxx values defined in cfgmgr32.h.
SPDRP_SECURITY
The function retrieves the device's Security registry value as a SECURITY_DESCRIPTOR structure (described in Platform SDK documentation).
SPDRP_SECURITY_SDS
The function retrieves the device's Security registry value as a string.
SPDRP_SERVICE
The function retrieves the device's Service registry value, which is a string representing the service name.
SPDRP_UI_NUMBER
The function retrieves the device's UINumber registry value, which is a DWORD value equal to the value contained in the UINumber member of the DEVICE_CAPABILITIES structure.
SPDRP_UI_NUMBER_DESC_FORMAT
The function retrieves the device's UINumberDescFormat registry value, which contains a format string used to display the UINumber value.
SPDRP_UPPERFILTERS
The function retrieves device's UpperFilters registry value, which is a multi-string representing the names of the device's lower upper drivers.
PropertyRegDataType
Supplies a pointer to a variable to receive the data type of the property being retrieved. This is one of the standard registry data types.
PropertyBuffer
Supplies a pointer to a buffer to receive the property being retrieved. If set to NULL, with PropertyBufferSize set to zero, the function just returns a value for RequiredSize.
PropertyBufferSize
Supplies the length, in bytes, of PropertyBuffer.
RequiredSize
Optional. If not NULL, it is a caller-supplied pointer to a variable that receives the required size, in bytes, of the property buffer pointed to by PropertyBuffer, based on the property requested by Property.
Return Value
The function returns TRUE if it is successful. Otherwise, it returns FALSE and the logged error can be retrieved with a call to GetLastError.

Headers
Declared in setupapi.h. Include setupapi.h.

AthlonxpX86 2003-09-11
  • 打赏
  • 举报
回复
可以通过setup API得到你需要的信息,下面是我刚写的得到端口设备名(串口并口)的程序
#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>

#pragma comment(lib,"Setupapi.lib")
int main( )
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
GUID aa;
GUID *ab;
ab=&aa;
i=sizeof(GUID);
DWORD DataT;
char buf[255];
memset(buf,0,256);
LPTSTR buffer = buf;
DWORD buffersize = 0;
if(!SetupDiClassGuidsFromName("Ports",ab,i,&i))//通过设备类名得到设备类型的GUID
{
printf("false");
return 1;
}
printf("端口设备GUID=%d-%d-%d-%d\n",ab->Data1,ab->Data2,ab->Data3,ab->Data4);

hDevInfo = SetupDiGetClassDevs(ab, 0, 0, 0);//通过设备类型GUID得到这类设备信息的句柄
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return 1;
}

// Enumerate through all devices in Set.

DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)//枚举这类设备中所有的设备
{

for(int s=0;s<2;s++)//因为有些时候第一次调用SetupDiGetDeviceRegistryProperty可能会失败,所以调用两次,
{
SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC,
&DataT, (PBYTE)buffer, buffersize, &buffersize);//得到设备属性
}

printf("设备: [ %s size=%d ]\n",buffer,buffersize);
memset(buf,0,256);


}
if ( GetLastError()!=NO_ERROR && GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return 1;
}

// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);

return 0;
}

你可以通过SetupDiClassGuidsFromName得到一类设备的GUID,比如端口类型名是Prots USB设备类型名是USB,
然后用SetupDiGetDeviceRegistryProperty可以得到你需要的信息

2,640

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧