Forum > Windows

is a device really there?

<< < (2/3) > >>

dieselnutjob:
I can invoke the function but it is returning error code 30 ( #define CR_INVALID_DEVICE_ID              0x0000001E ).

The definition of the device ID is here https://docs.microsoft.com/en-us/windows-hardware/drivers/install/device-instance-ids

I think that the problem is that my previously shared code is returning the Device ID but not the Instance ID.  I am aware that my Device IDs have to have "PCI\" appended to the front.

dieselnutjob:
ok maybe this is the answer from https://docs.microsoft.com/it-it/windows-hardware/drivers/install/instance-ids


--- Quote ---To obtain the bus-supplied instance ID for a device, use an IRP_MN_QUERY_ID request and set the Parameters.QueryId.IdType member to BusQueryInstanceID.
--- End quote ---

dieselnutjob:
I'm getting close.
If I start up Windows Device Manager, open up my Intel Chipset Damily PCI Express Root Port, Right click properties, Details, Select Device instance path.
Copy the result.
Send it to the this function:-

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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;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}     function TForm1.DeviceToHandle(ADevice:AnsiString):  PDEVINST;var  CM_Locate_DevNodeA: TCM_Locate_DevNodeA;  CfgMgrApiLib: TLibHandle;  Flags: ULong;  pDevice: pChar;begin  CfgMgrApiLib:=Loadlibrary(CfgMgrDllName);  if CfgMgrApiLib<>dynlibs.NilHandle then  begin    CM_Locate_DevNodeA:=TCM_Locate_DevNodeA(GetProcedureAddress(CfgMgrApiLib,'CM_Locate_DevNodeA'));    pDevice:=@ADevice[1];    Flags:=CM_LOCATE_DEVNODE_NORMAL;    memo1.Append(inttostr(CM_Locate_DevNodeA(Result,pDevice,Flags)));  end;end;    

It prints a zero into the Memo1, in other words it's happy.

dieselnutjob:
and, if I tell Device Manager to display "hidden" devices and find a USB device that's greyed out then send the Device instance path I get a 13

USB\VID_0CE9&PID_1012\6&1CEBE3FB&0&3
13

#define CR_NO_SUCH_DEVNODE                0x0000000D

So this API works, and does tell me whether something is there or not.

I just need to find a way to pull the Device instance out of the registry (or somewhere) programatically for a Device ID that I'm interesting in.

dieselnutjob:
Hooray I got it working  :D


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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.  

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version