问题
I'm trying to determine what type my WPD device is in Delphi.
In my application I need to know if the device is a Phone or Camera or what ever it is.
According to this MSDN article the WPD Device Type is a WPD Device Property which can be read by reading the properties of the device.
Then according to this MSDN article properties and attributes are defined as PROPERTYKEY structures with two parts: a category GUID, and a unique ID for that category.
I've found GUID and Uinique ID for WPD_DEVICE_TYPE
which are
WPD_DEVICE_TYPE_FMTID : TGuid = '{26D4979A-E643-4626-9E2B-736DC0C92FDC}';
WPD_DEVICE_TYPE_PID = 15;
My problem is that I'm having isses figuring out how to retrieve the information.
I was expecting that IPortableDevice
would have a .Property
procedure just like IPortableDeviceContent
, but it doesn't.
However, IPortableDeviceManager
does have a procedure called GetDeviceProperty
.
I have sample code which can get the friendly name of a WPD device (from the unit PortableDeviceApiLib_TLB.pas).
Code:
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;
My test code for getting a property of a device looks as following (basically the same... almost...):
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;
According to this MSDN article, pData
should be set to NULL and pcbData set to zero in order to get the size of pcbData.
Which I am doing.
Could someone help explaining what I need to do in order to get it right?
EDIT: I found this code which seems to be in python, that gets the device type.
I'm trying to port it to delphi.
回答1:
Your HRESULT
is $80070002
. That's a COM error code that wraps the Win32 error code, ERROR_FILE_NOT_FOUND. And that means that either the device ID or property name are incorrect. Assuming that you have indeed got the correct device ID, the obvious conclusion is that you are attempting to read the value of a property that does not exist.
回答2:
Ok, so eventually I figured out how to read the device type of a device.
What needed to be done was to read the device properties.
Some very interesting information could be read, such as the battery level of the device, if available.
EDIT: I used the source found here as a reference to WPD programming.
Code tested with external harddrives, SD memory card reader, USB sticks, Apple iPhone, and Samsung Galaxy phone.
Code is available HERE!!!
Just copy and paste the code in to a new VCL project, add a listbox called DeviceList, a memo called LogMemo, a panel called Panel1, and a button inside Panel1 called Button1. Then doubleclick on the Listbox, and doubleclick on the button, and finally doubleclick on the main form, and everything should run flawlsessly.
Programmed in Delphi XE7.
来源:https://stackoverflow.com/questions/29144536/determine-wpd-device-type-in-delphi