简体   繁体   English

标记为COM互操作的C#DLL如何在vb6应用程序中工作

[英]How does a C# DLL marked for COM interop work in a vb6 application

I have a simple C# library that I have registered for COM interop. 我有一个简单的C#库,已为COM互操作注册。 I have added a reference for this to my vb6 app. 我已经在vb6应用中添加了对此的参考。 I ran my vb application and everything works fine. 我运行了vb应用程序,一切正常。 What I would like to know is how does this work. 我想知道这是如何工作的。 I checked the task mamager and I see VB6.exe in the processes but I cannot see anything relating to .net. 我检查了mamager任务,并在进程中看到VB6.exe,但看不到任何与.net相关的信息。

code: vb6 代码:vb6

Dim a As CsharpdllForVBHack.ComAdder
Private Sub Command1_Click()
    Set a = New CsharpdllForVBHack.ComAdder
    a.Add 1, 4
End Sub

code: C#.net 代码:C#.net

 [ComVisible(true)]
 public class ComAdder
 {
    [ComVisible(true)]
    public void add (int a,int b)
    {
        TestForm testForm = new TestForm(a+b);
        testForm.ShowDialog();
    }
 }

I would also like to know how would I handle disposing of this com object once I am done 我也想知道完成后如何处理该com对象

We noticed that each time we click on the button and close the form the memory used goes up by a few 100 kb even adding set a= Nothing 我们注意到,每次单击按钮并关闭表格时,即使添加set a = Nothing,使用的内存也会增加几百kb

In your case, VB instantiates a COM Callable wrapper class (CCW) which lives inside the .NET assembly. 在您的情况下,VB实例化一个驻留在.NET程序集中的COM可调用包装器类(CCW)。 The usual COM-type things happen here. 通常的COM类型的事情在这里发生。 First of all, COM looks up the GUID for the class in the registry, and finds the assembly DLL, which it loads into the process of the VB component. 首先,COM在注册表中查找类的GUID,并找到程序集DLL,然后将其加载到VB组件的过程中。 COM tries to find a function which retrieves a pointer to a standard COM interface, which you use to instantiate the COM class. COM试图找到一个函数,该函数检索指向标准COM接口的指针,您可以使用该接口实例化COM类。 You now have a COM object. 您现在有了一个COM对象。

But that is not the whole story. 但这还不是全部。 When you instantiate the CCW, it also ensures that the .NET runtime is loaded, and then creates an instance of the .NET class. 实例化CCW时,它还确保加载.NET运行时,然后创建.NET类的实例。 Your COM object has an interface which is based on the .NET interface. 您的COM对象具有一个基于.NET接口的接口。 The CCW essentially forwards all calls from the COM interface to the .NET interface, converting the COM data types to .NET data types, and back again if you have return values and out parameters. CCW本质上将所有调用从COM接口转发到.NET接口,将COM数据类型转换为.NET数据类型,如果有返回值和out参数,则再次返回。

As for your second point - in this particular case, don't bother. 至于第二点-在这种情况下,请不要打扰。 When VB gets to the end of the procedure (or Exit Sub, or raises an error), it jumps to a subroutine which clears down all procedure level variables. 当VB到达过程的末尾(或Exit Sub或引发错误)时,它跳到一个子例程,该子例程清除了所有过程级变量。 If object variables are cleared, the reference count to the COM object is decremented. 如果清除了对象变量,则对COM对象的引用计数将减少。 If the reference count is zero, the COM instance kills itself. 如果引用计数为零,则COM实例将自行终止。

In your case, when the COM class kills itself, it takes extreme measures to ensure that the .NET object is destroyed, but you cannot rely on this behaviour, as with all .NET objects. 在您的情况下,当COM类自行终止时,它会采取极端措施来确保.NET对象被销毁,但您不能像所有.NET对象一样依赖此行为。

The first part of your question is too broad to be answered here have a look at COM Interop for much more information on this subject. 您的问题的第一部分过于广泛,无法在此处回答。有关此主题的更多信息,请访问COM Interop

The second part of your question is answered as follows: 问题的第二部分回答如下:

To dispose of the object in VB6 you do the following: 要在VB6中处理该对象,请执行以下操作:

Set a = Nothing

Making sure there are no other references left around. 确保周围没有其他参考。

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

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