tags:

views:

66

answers:

3

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?

A: 

This might help.

KMan
I don't want to expose anything as ActiveX control. I want to use existing library. The thing is, this lib is not even a control.
prostynick
+1  A: 

Using the [CoClass] attribute is quite unusual, you'd usually rely on Tlbimp.exe or adding a COM reference to get the type library converted into an interop class automatically. Avoids mistakes and versioning problems. But if it works, what the hey.

Yes, you'll need to get that COM server deployed and installed on the target machine. That could be as simple as just copying the .ocx file and registering it with Regsvr32.exe. But it is rarely that simple, the .ocx file might have a dependency on other DLLs that would have to be copied and/or registered as well. It is best to use the vendor's installer. Find out what is needed with a virgin test machine or a VM.

Hans Passant
The interface I pasted is generated after adding a COM reference. The only thing that I'm doing is creating the object and using it, as showed in `Program` class.
prostynick
Oh, okay. Then just worry about getting it deployed.
Hans Passant
@Hans Passant, what do you think about my answer?
prostynick
Using the [STAThread] attribute is necessary 99.9% of the time when you use COM. The vast majority of COM servers only support single threaded apartments. Not sure why using reg-free COM has an effect.
Hans Passant
A: 

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.

prostynick