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?
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 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.
精彩评论