在Delphi中是否需要使用GUID? [英] Are GUIDs necessary to use interfaces in Delphi?

查看:208
本文介绍了在Delphi中是否需要使用GUID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

官方文件说它们是可选的。我知道COM interop需要每个接口的唯一标识符,但是我看到的每个接口示例都有一个GUID是否与COM一起使用?是否有任何好处包括一个GUID,如果它不会被用于COM?

解决方案

我注意到一些方法例如 支持 (以确定类是否符合特定接口)要求您在使用GUID之前定义GUID。



此页面通过以下信息确认:


注意:SysUtils单元提供一个
重载的函数,称为Supports
,当
类型和实例支持
特定接口时,返回true或false,
GUID。
支持功能在
中以Delphi的方式和
运算符使用。显着差异
是支持函数可以将
作为正确的操作数 GUID或

GUID
相关联的接口类型,而是和以名称
的类型。有关
的更多信息,请参阅类参考。


这里有一些有关界面的有趣信息,其中指出:


为什么界面需要
唯一标识?答案是
简单:因为Delphi类可以
实现多个接口。当一个
应用程序正在运行时,必须有
是一个机制,将
的指针从
实现中获取到适当的接口。 找到
的唯一方法是,如果一个对象实现
接口,并获得一个指向
的指针,该接口的实现是
通过GUIDs
。 / p>

以两个引号加重。



阅读整篇文章也让您意识到 QueryInterface (需要GUID)在幕后使用,原因如引用计数。


The official documentation says they are optional. I know COM interop requires a unique identifier for each interface but every interface example I see has a GUID whether it's used with COM or not? Is there any benefit to including a GUID if its not going to be used with COM?

解决方案

I've noticed that some methods such as Supports (to determine if a class conforms to a specific interface) require that you define a GUID before you can use them.

This page confirms it with the following information:

Note: The SysUtils unit provides an overloaded function called Supports that returns true or false when class types and instances support a particular interface represented by a GUID. The Supports function is used in the manner of the Delphi is and as operators. The significant difference is that the Supports function can take as the right operand either a GUID or an interface type associated with a GUID, whereas is and as take the name of a type. For more information about is and as, see Class References.

Here's some interesting information about interfaces, which states:

Why does an interface need to be uniquely identifiable? The answer is simple: because Delphi classes can implement multiple interfaces. When an application is running, there has to be a mechanism that will get pointer to an appropriate interface from an implementation. The only way to find out if an object implements an interface and to get a pointer to implementation of that interface is through GUIDs.

Emphasis added in both quotes.

Reading this entire article also makes you realize that QueryInterface (which requires a GUID) is used behind the scenes for reasons such as reference counting.

这篇关于在Delphi中是否需要使用GUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆