VB6 对象和数据类型 [英] VB6 Object and data types

查看:32
本文介绍了VB6 对象和数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Object 类在面向对象的编程语言(如 Java)中位于类层次结构的顶部.我也知道您在 .NET 中有引用类型和值类型.我也了解基于 C 的语言中的类型定义.

尽管如此;我正在努力理解 VB6 中的对象是什么(http://msdn.microsoft.com/en-us/library/aa338034%28v=vs.60%29.aspx) 以及究竟什么是变体.什么是变体?以及如何在 VB6 中实现对象?

解决方案

VB6 使用的所有对象都是 COM 对象.COM 对象本质上是一个变长数据结构,其变长头包含任意数量的指向 VTable 的 32 位指针,连续的字节包含对象的实例数据.例如,

字节0-3 VTable1 指针4-7 VTable2 指针8-11 VTable3 指针...实例数据

VTable 是一个由 32 位指针组成的数组,这些指针指向的都是一个this"实例指针.

字节0-3 Func1(this, ..., ...)4-7 Func2(this, ..., ...)8-11 Func3(this, ..., ...)...

唯一的其他规范是所有 VTable 必须继承自 IUnknown,即前三个函数必须是:

QueryInterface()添加引用()释放()

本质上,QueryInterface() 允许您查明 COM 对象是否支持特定接口(由 UUID 表示).AddRef() 允许对象编写器增加内部引用计数.Release() 允许对象编写者递减引用计数器,当计数为零时销毁对象.您永远不会在 VB 中调用这些方法 - 编译器会为您添加这些调用(VB6 的优势之一).

参见 http:///msdn.microsoft.com/en-us/library/windows/desktop/ms680509(v=vs.85).aspx 了解更多详情.

VB对象"类型是对支持 IDispatch 接口的对象的引用(请参阅http://msdn.microsoft.com/en-us/library/windows/desktop/dd318520(v=vs.85).aspx).这就是允许您在 VB 和 VBScript 中进行后期绑定的原因.用 VB6 编写的所有对象都会自动实现一个从 IDispatch 继承的接口.这被称为双接口,因为它支持早绑定和晚绑定.

请注意,COM 中没有内置直接类型系统.但是,您可以选择支持 ITypeInfo 接口,该接口允许您的对象的用户访问您想要添加的有关该对象的信息(使用使用类型库存储此信息的默认实现更容易).

Bob Riemersma 提到的 Variant 类型实际上是一个 16 字节的结构,其中有一个 2 字节的整数 (vt),表示正在封装的自动化类型,后 8 个字节可用于包含值类型最多 8 个字节,或一个 32 位的指向另一种类型的指针.VB 使用其内部函数以及所有必要的内存分配和释放在 VB 类型和变体之间进行所有必要的转换.通过将指向对象的指针复制到 Variant 中,并调用该对象的 AddRef() 方法,Variant 可以包含对 COM 对象的引用.

I understand that the class Object is at the top of the class hiearchy in an Object Oriented programming langauge, like Java. I also understand that you have reference types and value types in .NET. I also understand type definitions in C based languages.

Despite this; I am struggling to understand what an Object is in VB6( http://msdn.microsoft.com/en-us/library/aa338034%28v=vs.60%29.aspx ) and what exactly a variant is. What is a variant? and how is an object implemented in VB6?

解决方案

All objects used by VB6 are COM objects. A COM object is essentially a variable length data structure whose variable length header contains any number of 32 bit pointers to VTables, and sucessive bytes contain the instance data of the object. For instance,

Bytes
0-3    VTable1 pointer
4-7    VTable2 pointer
8-11   VTable3 pointer
...
       Instance data

A VTable is an array of 32 bit pointers to functions which all are passed a "this" instance pointer.

Bytes
0-3    Func1(this, ..., ...)
4-7    Func2(this, ..., ...)
8-11   Func3(this, ..., ...)
...

The only other specification is that all VTables MUST inherit from IUnknown, i.e. the first three functions must be:

QueryInterface()
AddRef()
Release()

Essentially, QueryInterface() allows you to find out whether a COM object supports a specific interface (which is represented by an UUID). AddRef() allows the object writer to increment an internal reference count. Release() allows the object writer to decrement the reference counter, destroying the object when the count is zero. You never call these methods in VB - the compiler adds these calls for you (one of the advantages of VB6).

See http://msdn.microsoft.com/en-us/library/windows/desktop/ms680509(v=vs.85).aspx for more details.

A VB 'Object' type is a reference to an object which supports the IDispatch interface (see http://msdn.microsoft.com/en-us/library/windows/desktop/dd318520(v=vs.85).aspx). This is what allows you to do late binding in VB and VBScript. All objects written in VB6 automatically implement an interface that inherits from IDispatch. This is called a dual interface, because it supports early and late binding.

Note that there is no direct type system built into COM. However, you can opt to support the ITypeInfo interface, which allows the users of your object to access the information you want to add about the object (it is easier to use the default implementation which uses type libraries to store this information).

The Variant type, as mentioned by Bob Riemersma, is actually a 16 byte structure which has a 2 byte integer (vt) which indicates what Automation type is being encapsulated, and the latter 8 bytes can be used to contain value types of up to 8 bytes, or a 32 bit pointer to another type. VB does all the necessary conversion between VB types and Variants using its internal functions, and all the necessary memory allocations and deallocations. Variants can contain references to COM objects by copying the pointer to the object into the Variant, and calling the object's AddRef() method.

这篇关于VB6 对象和数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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