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

本文档展示了如何使用C#的WPD API来检测连接的便携式设备,获取设备信息,以及进行文件传输。通过PortableDeviceApiLib,实现了获取设备ID、设备友好名,并递归枚举设备中的文件ID。还介绍了如何创建对象并将其数据传输到设备上。
摘要由CSDN通过智能技术生成

最近用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);
           

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是使用 Windows SDK 版本为 8.1 实现枚举 WPD 设备内容的基本步骤: 1. 初始化 WPD API,并获取 WPD 服务的 IUnknown 接口。 ``` CoInitialize(NULL); HRESULT hr = CoCreateInstance( __uuidof(PortableDeviceManager), NULL, CLSCTX_INPROC_SERVER, __uuidof(IPortableDeviceManager), (VOID**)&pPortableDeviceManager ); ``` 2. 使用 IPortableDeviceManager::GetDevices 方法获取连接到计算机上的 WPD 设备。 ``` GUID guid = { 0 }; DWORD dwCount = 0; hr = pPortableDeviceManager->GetDevices(&guid, NULL, &dwCount); if (SUCCEEDED(hr) && dwCount > 0) { PWSTR* pDeviceIDs = new PWSTR[dwCount]; hr = pPortableDeviceManager->GetDevices(&guid, pDeviceIDs, &dwCount); if (SUCCEEDED(hr)) { // 枚举设备内容 for (DWORD i = 0; i < dwCount; i++) { EnumerateDeviceContent(pDeviceIDs[i]); } } // 释放设备 ID 数组 for (DWORD i = 0; i < dwCount; i++) { CoTaskMemFree(pDeviceIDs[i]); } delete[] pDeviceIDs; } ``` 3. 枚举设备内容,使用 IPortableDeviceContent 接口获取设备内容信息。 ``` void EnumerateDeviceContent(PCWSTR pszDeviceID) { IPortableDevice* pPortableDevice = NULL; IPortableDeviceContent* pPortableDeviceContent = NULL; // 连接到设备 HRESULT hr = CoCreateInstance( __uuidof(PortableDevice), NULL, CLSCTX_INPROC_SERVER, __uuidof(IPortableDevice), (VOID**)&pPortableDevice ); if (SUCCEEDED(hr)) { hr = pPortableDevice->Open(pszDeviceID, pPortableDeviceCallback); if (SUCCEEDED(hr)) { hr = pPortableDevice->Content(&pPortableDeviceContent); if (SUCCEEDED(hr)) { // 枚举设备内容 EnumerateContent(pPortableDeviceContent, L""); } } // 关闭设备连接 pPortableDevice->Close(); } // 释放设备对象 if (pPortableDevice) { pPortableDevice->Release(); } if (pPortableDeviceContent) { pPortableDeviceContent->Release(); } } ``` 4. 使用 IPortableDeviceContent::EnumObjects 方法枚举设备内容。 ``` void EnumerateContent(IPortableDeviceContent* pContent, PCWSTR pszObjectID) { IEnumPortableDeviceObjectIDs* pEnum = NULL; // 枚举对象 ID HRESULT hr = pContent->EnumObjects(0, pszObjectID, NULL, &pEnum); if (SUCCEEDED(hr)) { DWORD dwFetched = 0; PWSTR pszObjectIDs[MAX_OBJECTS] = { 0 }; // 获取对象 ID 数组 hr = pEnum->Next(MAX_OBJECTS, pszObjectIDs, &dwFetched); if (SUCCEEDED(hr)) { // 枚举每个对象 for (DWORD i = 0; i < dwFetched; i++) { EnumerateObject(pContent, pszObjectIDs[i]); } } // 释放对象 ID 数组 for (DWORD i = 0; i < dwFetched; i++) { CoTaskMemFree(pszObjectIDs[i]); } } // 释放枚举器对象 if (pEnum) { pEnum->Release(); } } ``` 5. 使用 IPortableDeviceContent::Properties 方法获取对象属性。 ``` void EnumerateObject(IPortableDeviceContent* pContent, PCWSTR pszObjectID) { IPortableDeviceProperties* pProperties = NULL; // 获取对象属性 HRESULT hr = pContent->Properties(&pProperties); if (SUCCEEDED(hr)) { IPortableDeviceValues* pValues = NULL; // 获取对象属性值 hr = pProperties->GetValues(pszObjectID, NULL, &pValues); if (SUCCEEDED(hr)) { // 输出对象属性值 OutputObjectProperties(pValues); } // 释放属性值对象 if (pValues) { pValues->Release(); } } // 释放属性对象 if (pProperties) { pProperties->Release(); } } ``` 以上就是使用 Windows SDK 版本为 8.1 实现枚举 WPD 设备内容的基本步骤。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值