IQueryCancelAutoPlay
-
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:
-
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:
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!
-
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!
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]
-
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]
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!
-
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:
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/InvokeGetRunningObjectTable
andCreateClassMoniker
and use theUCOMIRunningObjectTable
andUCOMIMoniker
interfaces defined underSystem.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 callUCOMIRunningObjectTable.Revoke
passing thecookie
I referenced above. [EDIT] I forgot that you must also register the CLSID (the same as for the implementation class) under HKEY_LOCAL_MACHINE\