我正在尝试跟踪何时在WPD兼容设备上创建文件,例如数码相机或Android手机。我注册接收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;
我只获取垃圾而不是添加/删除的对象名称。我在这里缺少什么?
答案 0 :(得分:1)
首先,OnEvent()
不应声明为dynamic
。 virtual
已经IPortableDeviceEventCallback
。
其次,您没有对IPortableDeviceValues.GetStringValue()
执行任何错误处理,也没有释放它返回的内存。看起来应该更像这样:
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;
$80070490: // 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;