我试图跟踪一个文件是在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;我只得到垃圾,而不是添加/删除的对象名。我在这里错过了什么?
发布于 2016-03-09 01:14:55
首先,不应将OnEvent()声明为dynamic。它已经是virtual in 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;https://stackoverflow.com/questions/35880406
复制相似问题