在 Delphi 中确定 WPD 设备类型

Determine WPD device type in Delphi

我正在尝试确定我的 WPD 设备的类型 Delphi。

在我的应用程序中,我需要知道该设备是 Phone 还是相机,或者它是什么。

根据 this MSDN article,WPD 设备类型是 WPD 设备 属性,可以通过读取设备的属性来读取。

然后根据 this MSDN article 属性和属性定义为 PROPERTYKEY 结构,包含两部分:类别 GUID 和该类别的唯一 ID。

我找到了 WPD_DEVICE_TYPE 的 GUID 和唯一 ID,它们是 WPD_DEVICE_TYPE_FMTID : TGuid = '{26D4979A-E643-4626-9E2B-736DC0C92FDC}'; WPD_DEVICE_TYPE_PID = 15;

我的问题是我在弄清楚如何检索信息时遇到了问题。

我原以为 IPortableDevice 会像 IPortableDeviceContent 一样有一个 .Property 程序,但它没有。

但是,IPortableDeviceManager 确实有一个名为 GetDeviceProperty 的过程。

我有示例代码可以获取 WPD 设备的友好名称(来自 PortableDeviceApiLib_TLB.pas 单元)。

代码:

function GetDeviceFriendlyName(sDeviceId: WideString): WideString;
  var iDevNameLen: LongWord;
      iRes: Integer;
      s: WideString;
begin
  //get length of friendly name:
  iDevNameLen := 0;
  s := '';

  iRes := My_IPortableDeviceManager.GetDeviceFriendlyName(PWideChar(sDeviceId),  Word(nil^),  iDevNameLen);

  if iRes = S_OK then
    if iDevNameLen>0 then
    begin
      SetLength(s, iDevNameLen);
      ZeroMemory(PWideChar(s), iDevNameLen);
      iRes := My_IPortableDevice.GetDeviceFriendlyName(PWideChar(sDeviceId),  PWord(PWideChar(s))^,  iDevNameLen);
      s := Trim(s);
    end;

  result := s;
end;

我获取设备 属性 的测试代码如下(基本相同...几乎...):

function GetDeviceProperty(ADeviceID, APropertyName: WideString): WideString;
  var iDevPropLen: LongWord;
      iRes: Integer;
      s: WideString;
      t: cardinal;
begin
  //get length of property name:
  iDevPropLen := 0;
  s := '';

  iRes := My_IPortableDeviceManager.GetDeviceProperty(PWideChar(ADeviceID), PWideChar(APropertyName), Byte(nil^), iDevPropLen, t);
  showmessage(inttostr(ires)+#13#10+inttostr(iDevPropLen)+#13#10+inttostr(t));
  //just trying to get some useful information…
end;

根据this MSDN article, pData应设置为 NULL 并将 pcbData 设置为零以获得 pcbData 的大小。

我正在做的。

有人可以帮助解释我需要做什么才能正确处理吗?

编辑: I found this code which seems to be in python,获取设备类型。

我正在尝试将其移植到 delphi。

你的 HRESULT070002。那是 COM error code that wraps the Win32 error code, ERROR_FILE_NOT_FOUND。这意味着设备 ID 或 属性 名称不正确。假设您确实获得了正确的设备 ID,显而易见的结论是您正在尝试读取不存在的 属性 的值。

好的,所以最终我想出了如何读取设备的设备类型。

需要做的是读取设备属性。

可以读取一些非常有趣的信息,例如设备的电池电量(如果可用)。

编辑:我使用来源 found here 作为 WPD 编程的参考。

使用外部硬盘驱动器、SD 存储卡 reader、USB 记忆棒、Apple iPhone 和三星 Galaxy phone.

测试的代码

Code is available HERE!!!

只需将代码复制并粘贴到一个新的 VCL 项目中,添加一个名为 DeviceList 的列表框、一个名为 LogMemo 的备忘录、一个名为 Panel1 的面板和一个名为 Button1 的 Panel1 中的按钮。然后双击列表框,双击按钮,最后双击主窗体,一切都应该运行完美。

在 Delphi XE7 中编程。