windows枚举MTP设备
为什么WM_DEVICECHANGE无法捕捉到MTP设备的接入信息?
是我SetupNotifications没有注册吗?但我找不到MTP的GUID
还是有其他方法处理这个问题?
谢谢各位!
WM_DEVICECHANGE消息可以用于捕获USB设备插入和拔出的消息,但是MTP设备在插入时不会触发此消息。这是因为MTP设备不是一个标准的USB存储设备,而是使用基于USB的MTP协议进行通信。
如果您想枚举MTP设备,可以使用Windows Portable Devices(WPD)API。WPD API提供了一组函数,可以用于检测和枚举连接到计算机上的MTP设备。您可以使用WPD API中的IPortableDeviceManager::GetDevices方法获取连接到计算机上的所有MTP设备的设备ID。
以下是一个示例代码片段,使用WPD API检测和枚举连接到计算机上的MTP设备:
#include <Windows.h>
#include <PortableDeviceApi.h>
#include <PortableDevice.h>
#include <vector>
int main()
{
HRESULT hr;
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
// Create the IPortableDeviceManager interface
IPortableDeviceManager* pPortableDeviceManager;
hr = CoCreateInstance(CLSID_PortableDeviceManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pPortableDeviceManager));
// Get the list of devices
DWORD dwNumDevices = 0;
WCHAR** ppDevices = NULL;
hr = pPortableDeviceManager->GetDevices(NULL, &dwNumDevices);
if (hr == S_OK && dwNumDevices > 0)
{
ppDevices = new WCHAR*[dwNumDevices];
hr = pPortableDeviceManager->GetDevices(ppDevices, &dwNumDevices);
for (DWORD dwIndex = 0; dwIndex < dwNumDevices; dwIndex++)
{
// Create the IPortableDevice interface
IPortableDevice* pPortableDevice;
hr = CoCreateInstance(CLSID_PortableDeviceFTM, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pPortableDevice));
// Open the device
hr = pPortableDevice->Open(ppDevices[dwIndex], NULL);
// Use the device
// Close the device
pPortableDevice->Close();
pPortableDevice->Release();
}
}
// Clean up
if (ppDevices != NULL)
{
for (DWORD dwIndex = 0; dwIndex < dwNumDevices; dwIndex++)
{
CoTaskMemFree(ppDevices[dwIndex]);
}
delete[] ppDevices;
}
pPortableDeviceManager->Release();
CoUninitialize();
return 0;
}
您需要在代码中添加相应的头文件和库,以及在连接到MTP设备之前初始化COM库和释放COM库。此外,需要添加错误检查和错误处理代码来处理潜在的错误。有用望采纳
在Windows中,如果要枚举MTP设备,可以使用Windows Portable Devices(WPD)API。WPD API可用于与MTP设备进行通信,并执行各种任务,例如浏览文件、传输文件、创建文件夹等。以下是WPD API的一些基本步骤:
1.初始化WPD API
2.枚举可用的MTP设备
3.打开所需的MTP设备
4.执行所需的任务
5.关闭MTP设备
6.释放WPD API资源
以下是一个简单的示例代码,用于枚举MTP设备:
#include <windows.h>
#include <PortableDeviceApi.h>
#include <PortableDevice.h>
#include <vector>
int main()
{
// 初始化WPD API
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
// 枚举可用的MTP设备
HRESULT hr;
DWORD dwCount = 0;
LPWSTR* pnpDeviceIDs = NULL;
hr = CoCreateInstance(CLSID_PortableDeviceManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pPortableDeviceManager));
if (SUCCEEDED(hr))
{
hr = pPortableDeviceManager->GetDevices(NULL, &dwCount);
if (SUCCEEDED(hr))
{
if (dwCount > 0)
{
pnpDeviceIDs = new LPWSTR[dwCount];
hr = pPortableDeviceManager->GetDevices(pnpDeviceIDs, &dwCount);
if (SUCCEEDED(hr))
{
for (DWORD i = 0; i < dwCount; i++)
{
// 打开所需的MTP设备
LPWSTR pnpDeviceID = pnpDeviceIDs[i];
IPortableDevice* pPortableDevice = NULL;
hr = CoCreateInstance(CLSID_PortableDeviceFTM, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pPortableDevice));
if (SUCCEEDED(hr))
{
hr = pPortableDevice->Open(pnpDeviceID, NULL);
if (SUCCEEDED(hr))
{
// 执行所需的任务
// ...
// 关闭MTP设备
pPortableDevice->Close();
}
pPortableDevice->Release();
}
}
}
}
}
pPortableDeviceManager->Release();
}
// 释放WPD API资源
CoUninitialize();
return 0;
}
通过使用WPD API,您可以在Windows中枚举和访问MTP设备。