思路分析
从Windows Vista开始SetupDi和WMI不再是唯一枚举和接收关于硬件信息的应用程序接口。随着Discovery的引入,你可以使用统一的API和接口访问已安装的硬件设备,用于收集功能,属性和NetBIOS,PNP,PNP-X等多种不同设备类型的信息。
Delphi XE3的Winapi.Functiondiscovery单元则封装了DiscoveryAPI,下面的基本代码用于枚举硬件设备。
调用收集设备信息(函数实例),必须要用到IFunctionDiscovery.GetInstanceCollection方法, 使用 IFunctionInstanceCollection.Item方法从集合中得到每个函数的实例,最后使用IFunctionInstance.OpenPropertyStore 和 IPropertyStore.GetValue方法检索每个属性值。
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.Win.ComObj,
Winapi.Windows,
Winapi.Activex,
Winapi.PropSys,
Winapi.Functiondiscovery,
System.SysUtils;
procedure Enumerate;
var
LFunctionDiscovery : IFunctionDiscovery;
hr : HResult;
i : integer;
LFunctionInstance : IFunctionInstance;
ppIFunctionInstanceCollection : IFunctionInstanceCollection;
ppIPropertyStore : IPropertyStore;
pv : TPropVariant;
pdwCount : DWORD;
pszCategory: PWCHAR;
begin
//create an instance to the IFunctionDiscovery interface
LFunctionDiscovery := CreateComObject(CLSID_FunctionDiscovery) as IFunctionDiscovery;
try
//set the provider to search
pszCategory:=FCTN_CATEGORY_PNP;
//get the devices collection
hr := LFunctionDiscovery.GetInstanceCollection(pszCategory, nil, true, ppIFunctionInstanceCollection);
//get the collection count
if Succeeded(hr) and Succeeded(ppIFunctionInstanceCollection.GetCount(pdwCount)) then
begin
if pdwCount=0 then
Writeln(Format('No items was found for the %s category',[pszCategory]))
else
for i := 0 to pdwCount - 1 do begin
//get the n Item of the collection
if Succeeded(ppIFunctionInstanceCollection.Item(i, LFunctionInstance)) then
begin
//init the propertiess store
LFunctionInstance.OpenPropertyStore(STGM_READ, ppIPropertyStore);
//read the properties values
if Succeeded(ppIPropertyStore.GetValue(PKEY_NAME, pv)) then
Writeln(Format('Name %s',[pv.pwszVal]));
if Succeeded(ppIPropertyStore.GetValue(PKEY_Device_InstanceId, pv)) then
Writeln(Format('Instance Id %s',[pv.pwszVal]));
if Succeeded(ppIPropertyStore.GetValue(PKEY_Device_Driver, pv)) then
Writeln(Format('Device Driver %s',[pv.pwszVal]));
if Succeeded(ppIPropertyStore.GetValue(PKEY_Device_Model, pv)) then
Writeln(Format('Model %s',[pv.pwszVal]));
if Succeeded(ppIPropertyStore.GetValue(PKEY_Device_Manufacturer, pv)) then
Writeln(Format('Manufacturer %s',[pv.pwszVal]));
if Succeeded(ppIPropertyStore.GetValue(PKEY_Device_LocationInfo, pv)) then
Writeln(Format('Location %s',[pv.pwszVal]));
Writeln;
end
else
RaiseLastOSError;
end;
end
else
RaiseLastOSError;
finally
LFunctionDiscovery:=nil;
end;
end;
begin
try
if (Win32MajorVersion >= 6) then // available on Vista (or later)
begin
if Succeeded(CoInitializeEx(nil, COINIT_MULTITHREADED)) then
try
Enumerate;
finally
CoUninitialize;
end;
end
else
Writeln('Windows version not compatible');
except
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Writeln('Press Enter to exit');
Readln;
end.
1 2
转载需保留链接来源:软件玩家 » Delphi XE3探索:Winapi.Functiondiscovery (一)
微软Edge浏览器如何分享网页到手机上查看?
迅雷下载磁力链接提示“种子下载失败”解决办法!
Microsoft Edge浏览器隐藏网页标题栏文字
Edge浏览器怎样网页整页全部截图?