Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. IQueryCancelAutoPlay

IQueryCancelAutoPlay

Scheduled Pinned Locked Moved C#
questioncsharphtmlcom
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    myker
    wrote on last edited by
    #1

    I wish to prevent AutoPlay from happening when my .NET application is running. According to MSDN (http://msdn.microsoft.com/msdnmag/issues/01/11/autoplay/default.aspx[^], there are two methods for accomplishing this: 1) Handle the "QueryCancelAutoplay" windows message. This only works when your application is in the foreground and activated. 2) Implement the IQueryCancelAutoPlay COM interface and register with the ROT (Running Object Table) I found an article for the first method at http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20695207.html[^]. However, my application isn't always in the foreground. Therefore, I am left with option 2. The big question I have is, how do I implement the IQueryCancelAutoPlay COM interface and register it at runtime with the ROT using C#? ~mykey What do you get when the devil goes bald? Hell Toupee. :laugh:

    D H 2 Replies Last reply
    0
    • M myker

      I wish to prevent AutoPlay from happening when my .NET application is running. According to MSDN (http://msdn.microsoft.com/msdnmag/issues/01/11/autoplay/default.aspx[^], there are two methods for accomplishing this: 1) Handle the "QueryCancelAutoplay" windows message. This only works when your application is in the foreground and activated. 2) Implement the IQueryCancelAutoPlay COM interface and register with the ROT (Running Object Table) I found an article for the first method at http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20695207.html[^]. However, my application isn't always in the foreground. Therefore, I am left with option 2. The big question I have is, how do I implement the IQueryCancelAutoPlay COM interface and register it at runtime with the ROT using C#? ~mykey What do you get when the devil goes bald? Hell Toupee. :laugh:

      D Offline
      D Offline
      Daniel Turini
      wrote on last edited by
      #2

      myker wrote: The big question I have is, how do I implement the IQueryCancelAutoPlay COM interface That's the easy part: Compile the shobjidl.idl from the Platform SDK (where IQueryCancelAutoPlay is defined) to a .tlb file with the IDL compiler. Then, add a reference from your project to this .tlb file. myker wrote: register it at runtime with the ROT using C#? That's the hard part, but I found a sample for you here: http://www.dotnet247.com/247reference/msgs/49/245964.aspx[^] Yes, even I am blogging now!

      H 1 Reply Last reply
      0
      • D Daniel Turini

        myker wrote: The big question I have is, how do I implement the IQueryCancelAutoPlay COM interface That's the easy part: Compile the shobjidl.idl from the Platform SDK (where IQueryCancelAutoPlay is defined) to a .tlb file with the IDL compiler. Then, add a reference from your project to this .tlb file. myker wrote: register it at runtime with the ROT using C#? That's the hard part, but I found a sample for you here: http://www.dotnet247.com/247reference/msgs/49/245964.aspx[^] Yes, even I am blogging now!

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        Wow, take a dependency on an interop assembly just ot implement the interface? It's a simple interface to implement and I'll post the solution in my direct reply if you're interested. It's a good approach for larger interfaces or many interfaces, however. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

        D 1 Reply Last reply
        0
        • H Heath Stewart

          Wow, take a dependency on an interop assembly just ot implement the interface? It's a simple interface to implement and I'll post the solution in my direct reply if you're interested. It's a good approach for larger interfaces or many interfaces, however. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

          D Offline
          D Offline
          Daniel Turini
          wrote on last edited by
          #4

          Heath Stewart wrote: Wow, take a dependency on an interop assembly just ot implement the interface? It's a simple interface to implement and I'll post the solution in my direct reply if you're interested. It's a good approach for larger interfaces or many interfaces, however. Ok, ok, I didn't use the [ComImport()] and [Guid()], but I was lazy and didn't want to convert the IDL to C# :-O Also, the point was to show him how to implement the interface, and tlbimp.exe is easily accesible from the IDE, I avoid explaining more than one thing per answer, because this can lead to confusion. Yes, even I am blogging now!

          1 Reply Last reply
          0
          • M myker

            I wish to prevent AutoPlay from happening when my .NET application is running. According to MSDN (http://msdn.microsoft.com/msdnmag/issues/01/11/autoplay/default.aspx[^], there are two methods for accomplishing this: 1) Handle the "QueryCancelAutoplay" windows message. This only works when your application is in the foreground and activated. 2) Implement the IQueryCancelAutoPlay COM interface and register with the ROT (Running Object Table) I found an article for the first method at http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20695207.html[^]. However, my application isn't always in the foreground. Therefore, I am left with option 2. The big question I have is, how do I implement the IQueryCancelAutoPlay COM interface and register it at runtime with the ROT using C#? ~mykey What do you get when the devil goes bald? Hell Toupee. :laugh:

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            The first reply will lead to an extra, rather large assembly that not necessary for this one simple interface with a single method. Just declare it in your project like so:

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

            Implement that in a class with its own GuidAttribute (always hard-code your GUIDs, be they CLSIDs or IIDs) and [ClassInterface(ClassInterfaceType.None)] so that the CLR does not auto-generate a class interface for you. You should always follow good COM practices when coding COM interop in .NET. You'll then need to register an instance of your implementation class in the Running Object Table. To do this you'll need to P/Invoke GetRunningObjectTable and CreateClassMoniker and use the UCOMIRunningObjectTable and UCOMIMoniker interfaces defined under System.Runtime.InteropServices:

            [DllImport("ole32.dll")]
            static extern int GetRunningObjectTable(
            [MarshalAs(UnmanagedType.U4)] int reserved,
            ref UCOMIRunningObjectTable);
             
            [DllImport("ole32.dll")]
            static extern int CreateClassMoniker(
            Guid g,
            ref UCOMIMoniker);

            (Note that neither of these functions gets defined when you create an interop assembly like how the first reply states). To register your implementation class, then, instantiate it. In the following code, I assume the reference variable is called qcap:

            int cookie = 0;
            UCOMIRunningObjectTable rot;
            if (GetRunningObjectTable(0, ref rot) == 0)
            {
            IMoniker mk;
            if (CreateClassMoniker(new Guid("your class's GuidAttribute value"),
            ref mk) == 0)
            {
            rot.Register(0, qcap, mk, ref cookie);
            }
            }

            Because this is a weak reference, you should not need to explicitly revoke your registration, but it's not a bad idea to do anyway. What I recommend is implementing the IDisposable pattern on your class and call UCOMIRunningObjectTable.Revoke passing the cookie I referenced above. [EDIT] I forgot that you must also register the CLSID (the same as for the implementation class) under HKEY_LOCAL_MACHINE\

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups