0

插入 USB 闪存驱动器时,Windows 通常会打开自动播放对话框,提供浏览驱动器或如果有多媒体文件提供选择应用程序来打开它们。

我们开发了一个连接到 USB 驱动器并将自身注册为大容量存储设备的媒体播放器。

我需要的是,在插入播放器时,不会显示此对话框,而是启动我自己的应用程序。

理想情况下,该应用程序将位于闪存驱动器本身上,但据我了解,USB 驱动器禁用了自动运行。

如果启动预安装的应用程序就足够了。我已经尝试捕捉 WM_DRIVE_CHANGE 消息,但这仅在我的应用程序是最顶层的窗口时才有效,否则会显示自动播放对话框。

4

2 回答 2

1

经过对谷歌的长期研究,我发现了这个论坛帖子:

http://social.msdn.microsoft.com/Forums/uk-UA/windowssdk/thread/aef929cb-62ac-4371-b7de-2c07adf3c6a7

我遵循了这个,这是工作代码:

[Flags()]
public enum AutorunContent : int
{
    AutorunInf = 2,
    AudioCD = 4,
    DVDMovie = 8,
    BlankCD = 16,
    BlankDVD = 32,
    UnknownContent = 64,
    AutoPlayPictures = 128,
    AutoPlayMusics = 256,
    AutoPlayMovies = 512
}


[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("DDEFE873-6997-4e68-BE26-39B633ADBE12")]
public interface IQueryCancelAutoPlay
{
    [PreserveSig]
    int AllowAutoPlay(
      [MarshalAs(UnmanagedType.LPWStr)] string pszPath,
      [MarshalAs(UnmanagedType.U4)] AutorunContent dwContentType,
      [MarshalAs(UnmanagedType.LPWStr)] string pszLabel,
      [MarshalAs(UnmanagedType.U4)] int dwSerialNumber);
}


public class RunningObjectTableEntry : IDisposable
{
    private int cookie;
    private IRunningObjectTable rot = null;
    private IMoniker monkey = null;

    private RunningObjectTableEntry() { }

    /// <summary>
    /// Creates a new entry for the given object
    /// </summary>
    /// <param name="obj">Object to make an entry for. Only one object per class should ever be registered.</param>
    public RunningObjectTableEntry(object obj)
    {
        int hr = GetRunningObjectTable(0, out rot);
        if (hr != 0)
        {
            throw new COMException("Could not retreive running object table!", hr);
        }

        Guid clsid = obj.GetType().GUID;
        hr = CreateClassMoniker(ref clsid, out monkey);
        if (hr != 0)
        {
            Marshal.ReleaseComObject(rot);
            throw new COMException("Could not create moniker for CLSID/IID \"" + clsid + "\"!", hr);
        }

        cookie = rot.Register(0x01, obj, monkey);   //weak reference, but allow any user
    }

    [DllImport("ole32.dll", ExactSpelling = true)]
    private static extern int GetRunningObjectTable([MarshalAs(UnmanagedType.U4)] int reserved, out IRunningObjectTable pprot);

    [DllImport("ole32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
    private static extern int CreateClassMoniker([In] ref Guid g, [Out] out IMoniker ppmk);

    #region IDisposable Members

    /// <summary>
    /// De-registers the object and class from the Running Object Table
    /// </summary>
    public void Dispose()
    {
        Marshal.ReleaseComObject(monkey);
        rot.Revoke(cookie);
        Marshal.ReleaseComObject(rot);
    }

    #endregion
}



[ComVisible(true)]
[Guid("331F1768-05A9-4ddd-B86E-DAE34DDC998A")]
[ClassInterface(ClassInterfaceType.None)]
public class Autoplay : IQueryCancelAutoPlay, IDisposable
{
    private RunningObjectTableEntry rotEntry;

    public Autoplay()
    {
        rotEntry = new RunningObjectTableEntry(this);
    }

    #region IQueryCancelAutoPlay Members

    public int AllowAutoPlay(string pszPath, AutorunContent dwContentType, string pszLabel, int dwSerialNumber)
    {
        if (pszLabel == "FUNKEYPLAY")  //This is the name of my volume that should not call autoplay
        {


            return 1;
        }
        else
        {
            return 0;
        }
        //Console.WriteLine("QueryCancelAutoPlay:");
        //Console.WriteLine("   " + pszPath);
        //Console.WriteLine("   " + dwContentType.ToString("x"));
        //Console.WriteLine("   " + pszLabel);
        //Console.WriteLine("   " + dwSerialNumber.ToString());
    }

    #endregion

    #region IDisposable Members

    public void Dispose()
    {
        rotEntry.Dispose();
    }

    #endregion
}

}

于 2012-08-28T06:10:22.740 回答
0

尝试观看 USB 中的所有文件。从文件夹首选项中,您可以选择显示/隐藏隐藏文件。您的设备上有一个名为 autorun.inf 的文件,每次插入 USB 时都会应用该文件。因此,如果您打开文件并键入应用程序的名称,它会在您每次插入闪存时自动运行。

于 2012-05-31T10:36:38.663 回答