如何在Delphi中创建COM DLL(类库)?
如何在 Delphi 中创建 COM DLL(类库)?
它适用于非常旧的 PC,其中未安装、也不会安装 .NET,并将替换并稍微扩展我有源代码的 VB 6 DLL。
How can I create a COM DLL (class library) in Delphi?
It is for use on very old PCs where .NET is not, and will not be, installed and will replace, and slightly extend, a VB 6 DLL for which I have the source code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您创建一个新的 ActiveX 库。您可以在 ActiveX 选项卡中找到它。 (文件-新建-其他-ActiveX-ActiveX 库)
You create a new ActiveX Library. You find it in the ActiveX tab. (File - New - Other - ActiveX - ActiveX Library)
这里有如何做到这一点:
AnIntroduction to COMProgramming with Delphi
那么,什么是接口?
接口是 COM 技术的核心。当我使用“界面”这个词时,不要将它与用户交互的视觉显示混淆。它实际上是 Delphi 中纳入的一个相当新的保留字。接口只不过是一组用于操作对象的抽象方法。
我们在上一课中确定了接口不是类;它是一个类。它是一个接口。因此,我们必须这样声明。让我们探讨一些界面特征。
现在我们更多地了解了接口可以做什么和不能做什么,让我们看看声明接口是什么样的。
关于 GUID
我相信您想知道这一大串疯狂的字符是什么。它称为 GUID,代表全局唯一标识符。有些人将其发音为“gwid”或“Gooey ID”。这里是 GUID 的一些特征:
好的,但是我如何实现一个接口呢?
这是一个好问题。答案是:你不能直接实现接口。您必须创建一个将实现您的接口的类。请参阅下面所示的编码示例。
上面列出的编码示例非常简单,同时仍然具有某种类型的可视化功能。让我们仔细看看过程 TextBtnClick。是不是少了点什么?如果您没有注意到,我们并没有明确释放该接口。这看起来像是内存泄漏,但外表是骗人的。一旦接口超出范围,Delphi 实际上会自动为您释放接口!当过程结束时,在过程或函数中声明的接口自然会超出范围。当对象被释放或程序结束时,在类内声明或全局声明的接口自然会超出范围。
现在我们需要讨论引用计数。每次检索对象的接口时,例如 IMyInterface := MyClass; Delphi 将使用 IUnknown 的 _AddRef 函数自动增加其引用计数。当接口超出范围时,Delphi 自动调用 IUnknown 的 ._Release 函数。如果您还没有弄清楚,IUnkown 也是一个接口,并且由于我们已经知道接口无法实现它定义的方法,那么您可能想知道 AddRef 和 Release 从何而来。答案是TInterfacedObject!
TInterfacedObject 到底是什么?
TInterfacedObject 和 IUnknown 在 system.pas 中定义。在详细说明这一点之前,我们至少需要看一下代码:
SYSTEM.PAS
未知声明
TInterfacedObject 的声明及其实现 该
代码可能看起来有点吓人,但我们将逐步完成此过程,因为了解引用计数对于有效地在 COM 中进行编程至关重要。从上面的代码可以明显看出,TInterfacedObject 实现了 IUnkown 中定义的方法。当您创建一个使用 TInterfacedObject 的类时,您实际上是在告诉该类,如果为它分配了接口,则它将进行引用计数。
让我们看一段简单的代码,以便我可以描述所发生的过程。
当您使用直接分配将 MyInterface 分配给 MyClass 时,Delphi 将自动调用 _AddRef 方法。这对 MyClass 说:“嘿,一个接口正在引用您!我们将增加您的引用计数。”如果您要将上述代码更改为以下内容:
请注意,我添加了一个名为 MyInterface2 的新变量。由于我们现在将 2 个不同的接口设置为等于 MyClass,猜猜 Myclass 的引用计数会发生什么?它变成 2,因为我们现在分配了两个接口!
您可能想知道 TInterfacedObject 实现的 _Release 方法。对于分配给超出范围的类的每个接口,都会自动调用 _Release 方法。显然,在过程 DoThis 结束时,程序执行将返回它来的地方,因此,您的本地类和接口声明超出了它们声明的范围。由于我们定义了两个接口,_Release 将被调用多少次?答案是一个闪亮的“2”!一旦我们的引用计数为零,MyClass 就会被 Delphi 自动释放。多漂亮多干净啊!
答案来源
Here you have how to do it:
An Introduction to COM Programming with Delphi
So, what is an Interface?
An interface is the heart of COM technology. When I use the word interface, do not confuse it with the visual display that the user interacts with. It is actually a fairly new reserved word incorporated into Delphi. An Interface is little more than group of abstract methods used to manipulate an object.
We established during the last lesson that an interface is not a class; it is an interface. Therefore, we must declare it as such. Let us explore some interface characteristics.
Now that we know more about what an interface can and cannot do, let us look at what it is like to declare an interface.
On GUIDs
I am sure you are wondering what the big string of crazy characters are. It is called a GUID and it stands for Globally Unique Identifier. Some people pronounce it as "gwid" or as "Gooey I.D.". Here some characteristics of a GUID:
Okay, but how do I implement an interface?
That is a good question. The answer is: You cannot implement an interface directly. You must create a class that will implement your interface. Refer to the coding example shown below.
The coding example listed above is about as simple as it can get while still having some type of visual functionality. Let's take a closer look at procedure TextBtnClick. Is there something missing? If you haven?t notice, we are not explicitly freeing the interface. This would appear to be a memory leak, but appearances are deceiving. As soon as the interface goes out of scope, Delphi will actually free the interface for you automatically! Interface's declared within a procedure or function will naturally fall out of scope when the procedure ends. Interface's declared within a class or are declared globally will naturally fall out of scope when the object is freed or the program ends.
Now we need to discuss Reference Counting. Every time you retrieve an interface to an object, e.g. IMyInterface := MyClass; Delphi will automatically increment its reference count using IUnknown's _AddRef function. When an interface falls out of scope, Delphi automatically calls IUnknown's ._Release function. If you haven't figured it out yet, IUnkown is an interface as well, and since we already know that an interface cannot implement the methods it defines, then you may be wondering where AddRef and Release come from. The answer is TInterfacedObject!
What in the world is TInterfacedObject?
TInterfacedObject and IUnknown are defined in system.pas. Before I can elaborate on this, we need to at least take a look at the code:
SYSTEM.PAS
Declaration of IUnkown
Declaration of TInterfacedObject and its implementation
The code may look a little scary, but we are going to walk through this step-by-step since it is crucial that you understand reference counting in order to effectively program in COM. It is obvious from the code above that TInterfacedObject implements the methods defined in IUnkown. When you create a class that uses TInterfacedObject, you are essentially telling that class that it is going to be reference counted if an interface is assigned to it.
Let us look at simple piece of code so that I can describe the process that takes place.
When you use Direct Assignment to assign MyInterface to MyClass, the method _AddRef gets called automatically by Delphi. This says to MyClass, "Hey, an interface is referencing you! Were going to increment your reference count." If you were to change the aforementioned code to the following:
Notice that I added a new variable called MyInterface2. Since we are now setting 2 different interfaces equal to MyClass, guess what happens to the Myclass?s reference count? It becomes 2 because we now have two interfaces assigned to it!
You may be wondering about the _Release method that is implemented by TInterfacedObject. The _Release method automatically gets called for each interface assigned to a class that falls out of scope. Obviously, at the end of procedure DoThis, the program execution is going to return whence it came, therefore, your local class and interface declarations fall out of the scope in which they were declared. Since we have two interfaces defined, _Release will get called how many times? The answer is a bright shiny "2"! And once our reference count hits zero, then MyClass is automatically freed by Delphi. How nice and clean!
source of the answer
访问此站点,这是在 Delphi 中开发 COM 的良好起点。它提供有关 Delphi 中的 COM 技术的教程、文章和代码。这可能对你有帮助
Go through this site which is good start point to develop COM in Delphi. It is having tutorials, articles, and code on COM technologies in Delphi. It might be helpful to you