Click here to Skip to main content
16,005,222 members
Home / Discussions / C#
   

C#

 
GeneralRe: how to translation matrix element Pin
montu33777-Dec-04 20:31
montu33777-Dec-04 20:31 
GeneralDynamic Button Pin
picasso27-Dec-04 19:35
picasso27-Dec-04 19:35 
GeneralRe: Dynamic Button Pin
Daniel Turini7-Dec-04 20:05
Daniel Turini7-Dec-04 20:05 
GeneralIQueryCancelAutoPlay Pin
myker7-Dec-04 18:26
myker7-Dec-04 18:26 
GeneralRe: IQueryCancelAutoPlay Pin
Daniel Turini7-Dec-04 20:23
Daniel Turini7-Dec-04 20:23 
GeneralRe: IQueryCancelAutoPlay Pin
Heath Stewart8-Dec-04 7:40
protectorHeath Stewart8-Dec-04 7:40 
GeneralRe: IQueryCancelAutoPlay Pin
Daniel Turini8-Dec-04 8:00
Daniel Turini8-Dec-04 8:00 
GeneralRe: IQueryCancelAutoPlay Pin
Heath Stewart8-Dec-04 8:07
protectorHeath Stewart8-Dec-04 8:07 
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\Software\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers\CancelAutoPlay\CLSID\{YOUR CLSID}. To do that and still support registration via regasm.exe, define a couple of static functions that use the Registry and RegistryKey classes and attribute those functions with the ComRegisterFunctionAttribute and ComUnregisterFunctionAttribute. regasm.exe will execute these appropriate in addition to normal registration.

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]
Questionhow make a mirror printing in c# Pin
bigmega7-Dec-04 17:27
bigmega7-Dec-04 17:27 
AnswerRe: how make a mirror printing in c# Pin
Christian Graus7-Dec-04 18:33
protectorChristian Graus7-Dec-04 18:33 
GeneralProblem with SendMessage() using P/Invoke Pin
Liu Shuai7-Dec-04 17:15
Liu Shuai7-Dec-04 17:15 
GeneralRe: Problem with SendMessage() using P/Invoke Pin
Daniel Turini7-Dec-04 20:02
Daniel Turini7-Dec-04 20:02 
GeneralRe: Problem with SendMessage() using P/Invoke Pin
Liu Shuai8-Dec-04 3:35
Liu Shuai8-Dec-04 3:35 
GeneralRe: Problem with SendMessage() using P/Invoke Pin
Daniel Turini8-Dec-04 4:00
Daniel Turini8-Dec-04 4:00 
GeneralRe: Problem with SendMessage() using P/Invoke Pin
Liu Shuai8-Dec-04 5:22
Liu Shuai8-Dec-04 5:22 
GeneralRe: Problem with SendMessage() using P/Invoke Pin
Mike Dimmick8-Dec-04 0:10
Mike Dimmick8-Dec-04 0:10 
GeneralRe: Problem with SendMessage() using P/Invoke Pin
Liu Shuai8-Dec-04 4:09
Liu Shuai8-Dec-04 4:09 
GeneralHELP ME!!! Pin
Jacob Graves7-Dec-04 13:44
Jacob Graves7-Dec-04 13:44 
GeneralRe: HELP ME!!! Pin
Heath Stewart7-Dec-04 14:51
protectorHeath Stewart7-Dec-04 14:51 
GeneralSorry.. Pin
Jacob Graves7-Dec-04 15:10
Jacob Graves7-Dec-04 15:10 
GeneralRe: Sorry.. Pin
Jitesh Patil7-Dec-04 21:53
Jitesh Patil7-Dec-04 21:53 
GeneralRe: Sorry.. Pin
Heath Stewart8-Dec-04 5:51
protectorHeath Stewart8-Dec-04 5:51 
GeneralThank you! Pin
Jacob Graves8-Dec-04 7:29
Jacob Graves8-Dec-04 7:29 
GeneralRe: Installing my program on other computers... Pin
Skynyrd7-Dec-04 13:21
Skynyrd7-Dec-04 13:21 
GeneralRe: Installing my program on other computers... Pin
Heath Stewart7-Dec-04 13:32
protectorHeath Stewart7-Dec-04 13:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.