简体   繁体   English

在C#中使用COM dll

[英]Using COM dll in C#

We have COM dll which was written in C++ and has been used by the apps written in vb 6.0. 我们有COM dll,它是用C ++编写的,并且已被vb 6.0中编写的应用程序使用。 My company plans write the newer versions of apps in .Net platform. 我的公司计划在.Net平台上编写更新版本的应用程序。

As far as the performance is concerned, when using a COM dll in a C# project, what should I choose from the 3 options listed below 就性能而言,在C#项目中使用COM dll时,我应该从下面列出的3个选项中选择什么

  1. Just adding the dll as a com reference 只需将dll添加为com参考
  2. Writing a wrapper dll with C++/Cli 用C ++ / Cli编写包装器DLL
  3. Generating a wrapper dll using TlbImp.exe 使用TlbImp.exe生成包装器DLL

Or are there any other options? 或者还有其他选择吗?
Thanks. 谢谢。

Writing a wrapper in C++/CLI isn't that likely to be faster, the COM interop marshaller in the CLR is heavily optimized. 在C ++ / CLI中编写包装器的速度可能不会更快,CLR中的COM互操作编组器已经过大量优化。 It auto-generates machine code stubs from the interop library that you create when you add a reference to the COM server. 它从您添加对COM服务器的引用时创建的互操作库自动生成机器代码存根。 A does a lot more work that's pretty invisible and very hard to do yourself, related to exceptions. A做了很多与异常相关的非常隐形且很难做的工作。

It makes sure that failure HRESULTs are properly converted to managed exceptions and that managed exceptions cannot leak into the COM server code. 它确保将故障HRESULT正确转换为托管异常,并且托管异常不会泄漏到COM服务器代码中。 The "make it fast" resolve you'll have when you do this will make you cut corners like this. 当你这样做时,你会有“快速”的决心,这会让你像这样偷工减料。 Now you've got something that's fast but unreliable. 现在你有了快速但不可靠的东西。 Getting a managed exception in unmanaged code is brutally hard to diagnose, all the context is gone. 在非托管代码中获取托管异常非常难以诊断,所有上下文都消失了。

Options 1 and 3 are the same thing. 选项1和3是相同的。 Both generate the interop library, the IDE simply runs the equivalent of Tlbimp for you. 两者都生成互操作库,IDE只为您运行等效的Tlbimp。

The usual guidance applies here. 通常的指导适用于此。 Do the simple thing first, the interop library is incredibly simple. 首先,简单的事情是,互操作库非常简单。 Only contemplate doing the really hard thing when you can actually measure perf problems and have a realistic idea what to do about it. 只有在你能够真正测量性能问题并且有一个现实的想法如何处理它时才考虑做真正的困难。 I've never once seen anybody decide that a C++/CLI wrapper was necessary. 我从来没有见过任何人认为必须使用C ++ / CLI包装器。

Option 2 is more performant, but not much, especially considering the DLL itself is in VB6. 选项2性能更高,但并不多,特别是考虑到DLL本身在VB6中。

Not sure if option 3 works at all. 不确定选项3是否有效。

I would personally use option 1, but just keep the interop somewhere safe so that I just keep reusing the same interop and not creating it everytime I add the reference. 我个人会使用选项1,但只是保持interop安全,以便我只是继续重用相同的互操作,而不是每次添加引用时都不创建它。

Another option is to use new dynamic features and late binding (using Activator to create the object) but that is definitely less performant of all. 另一种选择是使用新的dynamic特性和后期绑定(使用Activator来创建对象),但这绝对不是所有的。

Since the component is using COM, it will be easiest to add it as a reference and let visual studio build the proxies. 由于组件使用COM,因此最简单的方法是将其添加为引用,并让visual studio构建代理。 This will be very strait forward and transparent to the .net code. 对于.net代码,这将是非常直接和透明的。 It will not be quite as performant, but most likely it will suit your needs. 它不会那么高效,但很可能它会满足您的需求。 I would do this first, since it is so easy, and then see how it performs. 我会先这样做,因为它很容易,然后看看它是如何表现的。

If the component was not a COM component, and just a standard c++ dll, then the other two method would probably be a better choice. 如果组件不是COM组件,而只是标准的c ++ dll,那么另外两种方法可能是更好的选择。

A call to COM is slow because of the marshalling of the data. 由于数据的编组,对COM的调用很慢。 With slow I mean, compared to a call where you do not cross a Managed or COM boundary. 我的意思是缓慢,与您不跨越Managed或COM边界的调用相比。

If you need to do a lot of small calls to your COM component, in a performance critical piece of your application, you could wrap (and combine) them with C++. 如果您需要对COM组件进行大量小调用,那么在应用程序的性能关键部分中,您可以使用C ++包装(并组合)它们。

If the number of calls is minimal, or when they are not performance critical (but aren't all calls performance critical?) I would simply add a reference to the COM dll. 如果调用次数最少,或者它们不是性能关键(但并非所有调用性能都很关键?)我只需添加对COM dll的引用。

Summary Go for the refence to the COM dll, and test the performance. 总结转到COM dll的refence,并测试性能。 Since you migrate from VB6, you will get an enourmous performance boost already (string handling in .Net is sooooo much faster). 从VB6迁移后,您将获得极大的性能提升(.Net中的字符串处理速度更快)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM