如何从一个设备使用USB获取文件文件、设备、USB

2023-09-08 08:47:57 作者:萝莉脸爷们心

我写一个WPF应用程序,希望得到进入一台数码相机,并且已经疯狂地周围的Googling解决方案和Im pretty的高兴如何将其所有的准备。

Im writing a WPF application that wants to get access to a digital camera, and have been madly Googling around for solutions and Im pretty happy with how its all going.

这是我目前所面对的:

    private const decimal WM_DEVICECHANGE = 0x0219;
    private const int DBT_DEVTYP_HANDLE = 6;
    private const int DBT_DEVNODES_CHANGED = 7;
    private const int BROADCAST_QUERY_DENY = 0x424D5144;
    private const int DBT_DEVICEARRIVAL = 0x8000; // system detected a new device
    private const int DBT_DEVICEQUERYREMOVE = 0x8001;   // Preparing to remove (any program can disable the removal)
    private const int DBT_DEVICEREMOVECOMPLETE = 0x8004; // removed 
    private const int DBT_DEVTYP_VOLUME = 0x00000002; // drive type is logical volume

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        var mainFormWinInteropHelper = new System.Windows.Interop.WindowInteropHelper(this);
        System.Windows.Interop.HwndSource.FromHwnd(mainFormWinInteropHelper.Handle).AddHook(HwndHandler);
    }

    private IntPtr HwndHandler(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
    {
        ProcessWinMessage(msg, wparam, lparam);
        handled = false;
        return IntPtr.Zero;
    }

    private void ProcessWinMessage(int msg, IntPtr wparam, IntPtr lparam)
    {
        int deviceType;
        char driveLetter;

        if (msg == WM_DEVICECHANGE)
        {
            var int32 = wparam.ToInt32();

            switch (int32)
            {
                case DBT_DEVICEARRIVAL:
                    Console.WriteLine("Device Arrival");
                    break;
                case DBT_DEVICEQUERYREMOVE:
                    Console.WriteLine("Device Query Removed");
                    break;
                case DBT_DEVICEREMOVECOMPLETE:
                    Console.WriteLine("Device Removed");
                    break;
                case DBT_DEVNODES_CHANGED:
                    Console.WriteLine("Device added or removed");
                    break;
            }
        }
    }

这其实都是对我的数码相机的伟大工程,我能得到的照片关闭它。我要指出,在这个时候,当我插上我的相机,它出现在Windows下的可移动存储设备,它具有分配给它一个驱动器号。

This actually all works great for my digital camera and I can get the photos off it. I should note at this time that when I plug in my camera, it appears in windows under the "devices with removable storage", and it has a drive letter allocated to it.

然而,当我插在我的iPhone进行测试,iPhone不出现在该节中,并没有出现有关联的驱动器号...

However, when I plugged in my iPhone to test it, the iPhone does not appears under that section, and does not appear to have a drive letter associated to it...

但这里是我的主要问题,为什么Windows消息DBT_DEVNODES_CHANGED火的时候我加入了iPhone,但DBT_DEVICEARRIVAL消息触发时我添加了摄像头?

But here is my main question, why does the windows message DBT_DEVNODES_CHANGED fire when I add the iPhone, but the DBT_DEVICEARRIVAL message fires when I add the camera?

我将理想希望能够从任何USB设备获得文件,当用户它插入。

I would ideally like to be able to "get" files from any USB device when the user plugs it in.

有没有人有这样的经验吗?我的Win32编程是非常有限的...

Does anyone have any experience with this? My Win32 programming is very limited...

干杯, 马克

推荐答案

有两种类型的USB媒体提供商。其作用就像驱动器和Windows Media设备进行简单的相机和便携式媒体播放器的USB大容量存储设备。

There are two kinds of USB media providers. The USB Mass Storage devices that act just like drives and Windows Media Devices for simple cameras and portable media players.

有些摄像头(通常是便宜的),只实现了Windows Media设备模型,而其他实现USB大容量存储模式。

Some camera (usually the cheap ones) only implement the Windows Media Device model while others implement the USB Mass Storage model.

从你解释一下,在iPhone被视为Windows媒体设备而相机是USB大容量存储。我知道有一些苹果设备(尤其是触摸代)突然切换到大规模杀伤性武器模型中的问题时,有设备上的图片或截屏。尝试删除你已经采取了与iPhone的任何截图或图片,看看它是否解决了这个问题,如果是这样,这是因为你的code不支持Windows媒体设备模型。

From what you explain, the iPhone is treated as a Windows Media Device while the camera is a USB Mass Storage. I know there is an "issues" with some Apple devices (notably the touch generation) suddenly switching to the WMD model when there are pictures or screenshots on the device. Try deleting any screenshots or pictures you've taken with the iPhone and see if it fixes the problem, if so, this is because your code doesn't support the Windows Media Devices model.

借助 MSDN库可以帮助你通过梳理那些乱七八糟。他们更新了API几次,停止它的某些部分,增加了更多的其他地方,现在看来他们有一个新的版本,将修复它,所有TM

The MSDN library can help you sort through that mess. They updated the API several times, discontinued some parts of it, added more elsewhere and now it seems they have a new version that will fix-it-all TM.