title: C# - Hook into existing COM object
body: <p>Say we have an existing process (or application) that calls a COM object from an ocx file such as "MyCOMLibrary.ocx".</p>
  
  <p>Is there a way to write a C# library to exactly replicate the ocx file?  So that the original application can call your C# code rather than the original COM object?</p>
  
  <p>You would, of course, have to use identical CLSID and ProgIDs as the original ocx.  And assuming there is no signing involved, such as a SNK in the .Net world.</p>
  
  <p>Also, are there any tools that exist to automate this?  Something that takes in an ocx and spits out a C# file with methods to implement.</p>
  
  <p>EDIT: I want to add that the original application is VB6, and does not use .Net at all.  They are most likely loading the ocx as a VB6 app would (ProgId or Guid).  Does this cause any issues?</p>
  
  <p>We also have no problem with completely rewriting the ocx--we will most likely just return success error codes for all methods and only use methods/events required by our situation.</p>
  
  <p>EDIT: You would think this would not be too difficult to accomplish.  Can we make a VB6 ocx file that could replace the old ocx, and just pass all calls to a .Net assembly?</p>
  
  <p>EDIT: I tried using the following open source library: <a href="http://easyhook.codeplex.com" rel="nofollow noreferrer">EasyHook</a></p>
  
  <p>But it seems like this question should still be viable.  VB6 seems to load COM objects in a way that prevents hooking.  I don't see a way to hook instance methods on a class/interface or a class's constructor with EasyHook.</p>