unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, Registry, Dynlibs;
type
{ TForm1 }
ULONG = cardinal;
RETURN_TYPE = DWORD;
{$EXTERNALSYM RETURN_TYPE}
// Handle to a loaded DLL
//TModuleHandle = HINST;
CONFIGRET = RETURN_TYPE;
{$EXTERNALSYM CONFIGRET}
DEVINST = DWORD;
{$EXTERNALSYM DEVINST}
PDEVINST = ^DEVINST;
{$EXTERNALSYM PDEVINST}
DEVINSTID_A = PAnsiChar; // Device ID ANSI name.
{$EXTERNALSYM DEVINSTID_A}
TCM_Locate_DevNodeA = function(var pdnDevInst: PDEVINST;
pDeviceID: DEVINSTID_A; // OPTIONAL
ulFlags: ULONG): CONFIGRET; stdcall;
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
function DeviceToCRcode(ADevice:pChar): CONFIGRET;
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
const
CfgMgrDllName = 'cfgmgr32.dll';
CM_LOCATE_DEVNODE_NORMAL = $00000000;
{$EXTERNALSYM CM_LOCATE_DEVNODE_NORMAL}
CM_LOCATE_DEVNODE_PHANTOM = $00000001;
{$EXTERNALSYM CM_LOCATE_DEVNODE_PHANTOM}
CM_LOCATE_DEVNODE_CANCELREMOVE = $00000002;
{$EXTERNALSYM CM_LOCATE_DEVNODE_CANCELREMOVE}
CM_LOCATE_DEVNODE_NOVALIDATION = $00000004;
{$EXTERNALSYM CM_LOCATE_DEVNODE_NOVALIDATION}
CM_LOCATE_DEVNODE_BITS = $00000007;
{$EXTERNALSYM CM_LOCATE_DEVNODE_BITS}
procedure TForm1.Button1Click(Sender: TObject);
var
Registry: TRegistry;
Key: String;
DeviceList: TStringList;
InstanceList: TStringList;
i: integer;
j: integer;
DeviceID: AnsiString;
DeviceInstanceID: AnsiString;
begin
DeviceList:=TStringList.Create;
InstanceList:=TStringList.Create;
memo1.Clear;
Key:='\SYSTEM\CurrentControlSet\Enum\PCI';
memo1.Append(key);
DeviceList.Clear;
Registry := TRegistry.Create;
try
Registry.RootKey := HKEY_LOCAL_MACHINE;
if Registry.OpenKeyReadOnly(Key) then
begin
Registry.GetKeyNames(DeviceList);
end;
finally
Registry.Free;
end;
for i:=0 to DeviceList.Count-1 do
begin
DeviceID:='PCI\'+DeviceList[i];
memo1.Append(DeviceID);
Key:='\SYSTEM\CurrentControlSet\Enum\PCI\'+DeviceList[i];
memo1.Append(key);
InstanceList.Clear;
Registry := TRegistry.Create;
try
Registry.RootKey := HKEY_LOCAL_MACHINE;
if Registry.OpenKeyReadOnly(Key) then
begin
Registry.GetKeyNames(InstanceList);
end;
finally
Registry.Free;
end;
for j:=0 to InstanceList.Count-1 do
begin
DeviceInstanceID:=DeviceID+'\'+InstanceList[j];
memo1.Append(DeviceInstanceID);
if length(DeviceInstanceID)>0 then
memo1.Append(IntToStr(DeviceToCRcode(@DeviceInstanceID[1])));
end;
end;
end;
function TForm1.DeviceToCRcode(ADevice:pChar): CONFIGRET;
var
CM_Locate_DevNodeA: TCM_Locate_DevNodeA;
CfgMgrApiLib: TLibHandle;
Flags: ULong;
ADevInst: PDEVINST;
begin
CfgMgrApiLib:=Loadlibrary(CfgMgrDllName);
if CfgMgrApiLib<>dynlibs.NilHandle then
begin
CM_Locate_DevNodeA:=TCM_Locate_DevNodeA(GetProcedureAddress(CfgMgrApiLib,'CM_Locate_DevNodeA'));
Flags:=CM_LOCATE_DEVNODE_NORMAL;
Result:=CM_Locate_DevNodeA(ADevInst,ADevice,Flags);
end;
end;
end.