您的位置:首页 > 编程语言 > C#

C# WPD (windows portable devices) 检测WPD设备 获取设备信息

2010-07-12 12:01 267 查看
最近用c#写过一个WPD的列子,主要是参考 c++的实例, 在 windows sdk 中 ( C:/Program Files/Microsoft SDKs/Windows/v7.0/Samples/multimedia/wpd/wpdapisample ) 由于 没有学习过 c++ ,所以看的不是很懂,在数据传输这里卡住了, 传输的时候是用 IStream 但是我不知道 这个 IStream 怎样实例化, c++ 里面是用的 (hr = SHCreateStreamOnFile(szFilePath, STGM_READ, &pFileStream)) ,希望请路过的大侠能指教一下。谢谢了。。。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PortableDeviceApiLib;

namespace MobileIntelligentPlatforms.WPDAPI
{
public class WPDAPP
{
private PortableDeviceManagerClass devMgr;
private PortableDeviceApiLib.PortableDeviceClass ppDevice;

/// <summary>
/// 获得设备ID,用于检查是否有设备接入
/// </summary>
/// <returns></returns>
public string[] getDeviceInfo()
{
devMgr = new PortableDeviceManagerClass();
uint cDevices = 1;
devMgr.GetDevices(null, ref cDevices);
if (cDevices > 0)
{
string[] deviceIDs = new string[cDevices];
devMgr.GetDevices(deviceIDs, ref cDevices);
return deviceIDs;
}
return null;
}

/// <summary>
/// 获得设备信息
/// </summary>
/// <param name="deviceID"></param>
/// <returns></returns>
public List<string> getFriendlyName(string deviceID)
{
List<string> msgs = new List<string>();
uint nameLength = 0;
devMgr.GetDeviceFriendlyName(deviceID, null, ref nameLength);
ushort[] nameBuffer = new ushort[nameLength];
devMgr.GetDeviceFriendlyName(deviceID, nameBuffer, ref nameLength);
string friendlyName = "";
foreach (ushort letter in nameBuffer)
if (letter != 0) friendlyName += (char)letter;
msgs.Add(friendlyName);
return msgs;
}

/// <summary>
/// 连接设备
/// </summary>
/// <param name="deviceID"></param>
/// <param name="ppDevice"></param>
private void ChooseDevice(string deviceID)
{
ppDevice = new PortableDeviceClass();
PortableDeviceApiLib.IPortableDeviceValues pValues =
(PortableDeviceApiLib.IPortableDeviceValues)
new PortableDeviceTypesLib.PortableDeviceValuesClass();
pValues.SetStringValue(
ref PortableDevicePKeys.WPD_CLIENT_NAME, "WPD");//"Sample Client"
pValues.SetUnsignedIntegerValue(
ref PortableDevicePKeys.WPD_CLIENT_MAJOR_VERSION, 1);
pValues.SetUnsignedIntegerValue(
ref PortableDevicePKeys.WPD_CLIENT_MINOR_VERSION, 0);
pValues.SetUnsignedIntegerValue(
ref PortableDevicePKeys.WPD_CLIENT_REVISION, 2);
ppDevice.Open(deviceID, pValues);
}

#region 获得设备中文件ID

/// <summary>
/// 递归设备中的文件/文件夹ID
/// </summary>
/// <param name="deviceID"></param>
/// <returns></returns>
public string getFolderName(string deviceID)
{
//连接设备
ChooseDevice(deviceID);
//递归文件ID
List<string> objeceIDs = StartEnumerate();
string strIDs = "";
for (int i = 0; i < objeceIDs.Count; i++)
{
strIDs += objeceIDs[i];
}
return strIDs;
}

/// <summary>
///递归 枚举出设备中的文件或文件夹ID、设备ID
/// </summary>
/// <param name="pContent"></param>
/// <param name="parentID"></param>
/// <param name="indent"></param>
/// <param name="objectIDs"></param>
private void Enumerate(ref PortableDeviceApiLib.IPortableDeviceContent pContent, string parentID, string indent, ref List<string> objectIDs)
{
indent += " ";
PortableDeviceApiLib.IEnumPortableDeviceObjectIDs pEnum;
pContent.EnumObjects(0, parentID, null, out pEnum);
uint cFetched = 0;
do
{
string objectID;
pEnum.Next(1, out objectID, ref cFetched);
if (objectID != null && !objectID.Equals(""))
{
objectIDs.Add(objectID);
}
if (cFetched > 0)
{
Enumerate(ref pContent, objectID, indent, ref objectIDs);
}
} while (cFetched > 0);
}

/// <summary>
/// 得到第一个设备ID
/// </summary>
/// <param name="pContent"></param>
/// <param name="parentID"></param>
/// <param name="indent"></param>
/// <param name="objectIDs"></param>
private void EnumerateFirst(ref PortableDeviceApiLib.IPortableDeviceContent pContent, string parentID, string indent, ref List<string> objectIDs)
{
indent += " ";
PortableDeviceApiLib.IEnumPortableDeviceObjectIDs pEnum;
pContent.EnumObjects(0, parentID, null, out pEnum);
uint cFetched = 0;
string objectID;
pEnum.Next(1, out objectID, ref cFetched);
if (objectID != null && !objectID.Equals(""))
{
objectIDs.Add(objectID);
}
}

/// <summary>
/// 枚举出设备和文件ID
/// </summary>
/// <param name="pPortableDevice"></param>
/// <returns></returns>
private List<string> StartEnumerate()
{
PortableDeviceApiLib.IPortableDeviceContent pContent;
ppDevice.Content(out pContent);
List<string> objectIds = new List<string>();
//Enumerate(ref pContent, "DEVICE", "", ref objectIds);
EnumerateFirst(ref pContent, "DEVICE", "", ref objectIds);
return objectIds;
}

#endregion

/// <summary>
/// 传输文件
/// </summary>
/// <param name="FilePath"></param>
/// <param name="ParentObjectID"></param>
/// <returns></returns>
public bool transferContentToDevice(string filename, string ParentObjectID)
{
PortableDeviceApiLib.IStream pFileStream;
PortableDeviceApiLib.IPortableDeviceContent pContent;
ppDevice.Content(out pContent);
PortableDeviceApiLib.IPortableDeviceValues pValues = (PortableDeviceApiLib.IPortableDeviceValues)new PortableDeviceTypesLib.PortableDeviceValuesClass();
// PortableDeviceApiLib.tagSTATSTG statstg;
// pFileStream.Stat(out statstg,1);
//pValues.SetUnsignedLargeIntegerValue(ref PortableDevicePKeys.WPD_OBJECT_SIZE, statstg.cbSize.QuadPart);
pValues.SetStringValue(ref PortableDevicePKeys.WPD_OBJECT_PARENT_ID, ParentObjectID);
pValues.SetStringValue(ref PortableDevicePKeys.WPD_OBJECT_ORIGINAL_FILE_NAME, filename);
pValues.SetStringValue(ref PortableDevicePKeys.WPD_OBJECT_NAME, filename);
pValues.SetGuidValue(ref PortableDevicePKeys.WPD_OBJECT_CONTENT_TYPE, ref PortableDevicePKeys.WPD_CONTENT_TYPE_GENERIC_FILE);
pValues.SetGuidValue(ref PortableDevicePKeys.WPD_OBJECT_FORMAT, ref PortableDevicePKeys.WPD_OBJECT_FORMAT_UNSPECIFIED);

uint bufSize = 0;
string cookie = string.Empty;
pContent.CreateObjectWithPropertiesAndData(pValues,
out pFileStream,
ref bufSize,
ref cookie);// last

IPortableDeviceResources resources;

pContent.Transfer(out resources);

// IStream wpdStream;

uint optimalTransferSize = 0;
//WPDPropertyKey
resources.GetStream(filename, ref PortableDevicePKeys.WPD_RESOURCE_DEFAULT, 0, ref optimalTransferSize, out pFileStream);

//convert to a useful stream object

System.Runtime.InteropServices.ComTypes.IStream sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)pFileStream;

return true;
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: