Search code examples
c#cominteropocx

OCX component in C# - using and deploying


I am creating C# application that uses OCX component. I have this component from manufacturer of some kind of printer. During installation of their files, foo.ocx was added to my C:\Windows\System32.

The only thing I had to do is add reference in Solution Explorer, and point that library in COM tab. Then I took interface:

[Guid("XXX")]
 [CoClass(typeof(FooClass))]
 public interface Foo : IFoo { }

and I created instance of COM interface:

 class Program
 {
  static void Main(string[] args)
  {
   var foo = new Foo();
  }
 }

and everything works OK. Am I doing this right?

The second thing and more important to me is, that I am not sure how to deploy this. Referenced file in solution explorer has properties: Embed Interop Type: True, Isolated: False. This will lead to only 1 exe file in build directory. I would like to use my application on other machine, so I have to do something, but I am not sure, what. I tried to change properties of that reference, including copy local, but I suspect I have to install this ocx file on other machine. Should I use regsvr32 or regasm? Shoud I register this ocx file or something else?


Solution

  • Hans Passant already answered my question, but I have just find out, that setting Isolated in reference properties to True will do the job - exe, exe.manifest and original ocx file will be put into release folder, but this will only work, if I mark my Main method with [STAThread] attribute:

    class Program
    {
     [STAThread]
     static void Main(string[] args)
     {
      var foo = new Foo();
     }
    }
    

    Without this running the exe will lead to InvalidCastException. After that, I don't really need to register that ocx with regsvr32.