Delphi WPD 事件回调 - 获取文件名

Delphi WPD Event Callback - Get Filename

我正在尝试跟踪在 WPD 兼容设备(例如数码相机或 Android phone 上创建文件的时间。我通过 Advice 注册接收事件,我的回调被正确调用,但我无法正确获取文件名(可能是 OBJECT_NAME)。这是我拥有的:

TPortableDeviceEventsCallback = class(TInterfacedObject, IPortableDeviceEventCallback)
 public
  function OnEvent(const pEventParameters: IPortableDeviceValues): HResult; dynamic; stdcall;
 end;
.
.
.
function TPortableDeviceEventsCallback.OnEvent(const pEventParameters: IPortableDeviceValues): HResult;
var
ObjName: PWideChar;
begin
  pEventParameters.GetStringValue(WPD_EVENT_PARAMETER_OBJECT_NAME, ObjName);
  Log(string(ObjName));
end;

我只得到垃圾而不是 added/removed 对象名称。我在这里错过了什么?

首先,OnEvent() 不应声明为 dynamicIPortableDeviceEventCallback.

已经是 virtual

其次,您没有对 IPortableDeviceValues.GetStringValue() 进行任何错误处理,也没有释放它的内存 returns。它应该看起来更像这样:

function TPortableDeviceEventsCallback.OnEvent(const pEventParameters: IPortableDeviceValues): HResult;
var
  Hr: HResult;
  ObjName: PWideChar;
begin
  Hr := pEventParameters.GetStringValue(WPD_EVENT_PARAMETER_OBJECT_NAME, ObjName);
  case Hr of
    S_OK: begin
      try
        Log('Object Name: ' + String(ObjName));
      finally
        CoTaskMemFree(ObjName);
      end;
    end;
    DISP_E_TYPEMISMATCH: begin
      Log('Object Name is not a string!');
    end;
    070490: // HRESULT_FROM_WIN32(ERROR_NOT_FOUND)
    begin
      Log('Object Name is not found!');
    end;
  else
    // some other error
    Log('Error getting Object Name: $' + IntToHex(Hr, 8));
  end;
  Result := S_OK;
end;

第三,您没有通过查看 WPD_EVENT_PARAMETER_EVENT_ID 参数(这是唯一必需的参数)的值来了解您正在接收什么事件,从而了解它可以使用哪些参数。不同的事件有不同的参数值。

尝试枚举可用值以查看您在每个事件中实际收到的内容:

function TPortableDeviceEventsCallback.OnEvent(const pEventParameters: IPortableDeviceValues): HResult;
var
  Hr: HResult;
  Count, I: DWORD;
  Key: PROPERTYKEY;
  Value: PROPVARIANT;
begin
  Log('Event received');

  Hr := pEventParameters.GetCount(Count);
  if FAILED(Hr) or (Count = 0) then Exit;

  Log('Param count: ' + IntToStr(Count));

  for I := 0 to Count-1 do
  begin
    Hr := pEventParameters.GetAt(I, Key, Value);
    if FAILED(Hr) then
    begin
      Log('Cant get parameter at index ' + IntToStr(I));
      Continue;
    end;
    try
      Log('Param Key: ' + GuidToString(Key.fmtid) + ', Value type: $' + IntToHex(Value.vt, 4));
      // log content of Value based on its particular data type as needed...
    finally
      PropVariantClear(Value);
    end;
  end;
  Result := S_OK;
end;