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;
}
}
}
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;
}
}
}
相关文章推荐
- C# WPD (windows portable devices) 检测WPD设备 获取设备信息
- 重新想象 Windows 8 Store Apps (30) - 信息: 获取包信息, 系统信息, 硬件信息, PnP信息, 常用设备信息
- 【Windows 8 Store App】学习一:获取设备信息
- 【Windows 8 Store App】学习一:获取设备信息
- Windows 下获取USB设备信息的通用浏览器插件实现
- vc获取windows设备枚举设备信息
- 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop
- Windows下USB磁盘开发系列三:枚举系统中U盘、并获取其设备信息
- Windows下USB磁盘开发系列三:枚举系统中U盘、并获取其设备信息
- Windows下如何获取与手机设备有关的进程信息(Python版)
- 重新想象 Windows 8 Store Apps (30) - 信息: 获取包信息, 系统信息, 硬件信息, PnP信息, 常用设备信息
- 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop
- android 设备信息的获取总结
- 史上最全的iOS各种设备信息获取总结
- Windows下硬件信息获取
- 实战DeviceIoControl 之四:获取硬盘的详细信息 分类: windows驱动程序WDM 2013-09-25 14:48 397人阅读 评论(0) 收藏
- javascript获取Android设备版本信息(备忘)
- [Android]获取设备相关信息
- WinPcap学习(三)获取已安装设备的高级信息
- 获取当前Windows系统内帐户信息的函数