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\