C# 中的 OCX 组件 - 使用和部署

发布于 2024-09-09 03:13:42 字数 643 浏览 7 评论 0原文

我正在创建使用 OCX 组件的 C# 应用程序。我有某种打印机制造商提供的这个组件。在安装他们的文件期间,foo.ocx 被添加到我的 C:\Windows\System32 中。

我唯一要做的就是在解决方案资源管理器中添加引用,并在 COM 选项卡中指向该库。然后我获取了接口:

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

并创建了 COM 接口的实例:

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

并且一切正常。我这样做对吗?

第二件事对我来说更重要的是,我不确定如何部署它。解决方案资源管理器中引用的文件具有属性:嵌入互操作类型:True,隔离:False。这将导致构建目录中只有 1 个 exe 文件。我想在其他机器上使用我的应用程序,所以我必须做一些事情,但我不确定,做什么。我尝试更改该引用的属性,包括复制本地,但我怀疑我必须在其他计算机上安装此 ocx 文件。我应该使用 regsvr32 还是 regasm?我应该注册这个 ocx 文件还是其他文件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满意归宿 2024-09-16 03:13:42

使用 [CoClass] 属性非常不寻常,您通常依赖 Tlbimp.exe 或添加 COM 引用来自动将类型库转换为互操作类。避免错误和版本控制问题。但如果它有效的话,嘿嘿。

是的,您需要在目标计算机上部署并安装 COM 服务器。这可能很简单,只需复制 .ocx 文件并将其注册到 Regsvr32.exe 即可。但事情很少这么简单,.ocx 文件可能依赖于其他也必须复制和/或注册的 DLL。最好使用供应商的安装程序。了解原始测试机或虚拟机需要什么。

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.

ゝ偶尔ゞ 2024-09-16 03:13:42

Hans Passant 已经回答了我的问题,但我刚刚发现,将引用属性中的 Isolated 设置为 True 即可完成这项工作 - exe、exe.manifest 和原始 ocx 文件将被放入发布文件夹中,但这只有在我用 [STAThread] 属性标记我的 Main 方法时才有效:

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

如果没有这个,运行 exe 将导致 InvalidCastException。之后,我真的不需要使用 regsvr32 注册该 ocx。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文