Wrox Programmer Forums
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 30th, 2004, 02:12 AM
Authorized User
 
Join Date: May 2004
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default Vtable Binding urgent

Could anyone please explain me about what is Vtable and how early
binding maps to Vtable,and also about Disp Binding??

 
Old September 30th, 2004, 03:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

A vTable is basically a lookup table attached to an interface of a COM component. The vTable is an array of pointers to functions that implement the code behind the interface (the properties and methods). In your VB project, when you add a ref to a component (or a type library) VB extracts information about the vTable and compiles it into your program.

By Disp Binding, I assume you're referring to the IDispatch interface, or 'late binding'. Here there is no vTable, so your VB program must find and run the COM component's properties and methods at run-time. To do this it must call multiple methods on the IDispatch interface for each method or function of the COM component - it has to call GetIDsOfNames() to find the method or property, and then it must call Invoke to actually run the method or access the property.

If you use late binding via IDispatch then you can only access the default interface of a component. For instance, if there is a COM class called CFoo which also Implement's interface IBar, then you can only access CFoo properties and methods. You must use early binding, via the vTable, to access the properties and methods of IBar.

If you have VB6 it comes with a useful utility called OLE View which allows you to view the interfaces of COM Components.

hth
Phil
 
Old October 1st, 2004, 11:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Phil,

Actually a vTable is always present in any library, and COM is just a nice wrapper around it.
You are correct about late binding. For example, using early binding for the CFOO class is doing implementing a CFoo class like
dim cf as CFoo
set cf = new CFoo '' at this point the compiler associates the CFoo interface to the vTable
cfoo.DoSomething '' DoSomething is called directly using the vTable

Late binding is done when CFoo is not created directly:

dim cf as Object
set cf = GetFooFromSomePlace
cf.DoSomething

at this point the compiler does not know anything about the CFoo interface, because cf is just an object. When DoSomething is called, the program uses the IDisptach interface of cf (because cf is an object it does have a IDispatch interface) to get the address of the "DoSomething" method. If DoSomething is not found, it throws an error.

Using Implements avoids late binding. Suppose you have a class named CPerson with methods like Name and Age. Other classes can have the same interface (like CMan, CWoman, CManager etc) and if the real class is not known at compile time, it can be defined as Object and call obj.Name But if all classes use Implements to inherit the CPerson class, than the trick is
dim cp as CPerson
set cp = GetPersonFromSomePlace '' can be CMan, CManager etc
cp.Name
the compiler does not case what GetPersonFromSamePlace is, it just cares that that object implements the CPerson interface, and uses it.

Ops, longer than I thought, sorry
Marco
 
Old October 1st, 2004, 12:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Marco,

re-reading my post it seems the part about IDispatch was misleading. I wasn't meaning to imply that the VB'er has to explicitly write code to call GetIdsOfNames and Invoke, I really should have said that VB does that internally. I'm fully aware that declaring a var as object => late binding => IDispatch, whereas delaring it as Library.Class => early binding => vTable.

I don't understand what point you're trying to make in the last para about Implements. In your example you declare cp as an explicit class, hence early binding. So how does that relate to my point about not being able to use an Implemented interface through late binding? What I'm basically saying is that if CMan Implements IPerson, there's no way to call the IPerson_Name method in CMan through late binding, it can only be done through early binding. I believe what you're describing in this para is polymorphism.

rgds
Phil
 
Old October 1st, 2004, 03:21 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Phil.

Yes, I made the first example just to clarify what you said, I did not want to imply that you were not correct. I just thought to be more clear about the fact al the late bind is done internally by VB using the Object keyword.

Regarding Implements I could have been misleading as well :) Pardon my English.

If CManager implements CPerson, and Name is a method of CPerson, there are many ways to call Name of the CManager class:

dim cm as CManger
cm.Name <- early binding

dim cm a Object
cm.Name <- late binding

dim cp as CPerson
CallByName(cp, "Name", vbget) <- late bind

dim cp as CPerson
set cp = SomeCManagerClass
cp.Name <- early binding

My point (that was not clear) is that in VB it is still possible to have early binding even though the class is not known at design time, if that class implements a known interface.
For example, a firm keeps a collection of people entered in the building, each of them can be a CManager, CThirdFloor, CPersonel, CStaff, CGuest and so on. To print all names I can do:

dim cp as Object
for each cp in FirmCollection
debug.print cp.Name <- late bind

or, if all the classes implement CPerson

dim cp as CPerson
for each cp in FirmCollection
debug.print cp.Name

The second case is early bind.

And yes, Implements is (like you said) the backdoor to add (a sort of) polymorphism to VB.

Hope that I was more clear this time...

ciao,
Marco
 
Old October 4th, 2004, 03:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Marco,

Fair enough, I understand what you're saying now.

Just one issue though
Quote:
quote:
If CManager implements CPerson, and Name is a method of CPerson, there are many ways to call Name of the CManager class:

 ...

dim cm a Object
cm.Name <- late binding
This was exactly my point - this won't work. You can't access the CPerson_Name method of CManager with late binding if that Name method exists only through a formal "Implements CPerson" statement in the CManager class. If you try it you'll get err 438 'Object doesn't support this property or method'.
This method will only work if a Name method also exists on the CManager class (i.e. the default interface).

Marco, I've seen many of your previous posts, so I know you know your stuff, but in this instance I believe you're wrong - or maybe just misleading ;)

rgds
Phil
 
Old October 4th, 2004, 12:43 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Phil, You are absolutely right!

Marco
 
Old October 5th, 2004, 07:28 AM
Authorized User
 
Join Date: May 2004
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In Vtable also we have getIDsofName,invoke method . i Guess whenever u use to declare Object as
dim Obj as Object then
then it doesnt have any object refrence in the library so at run time only it can bind. this is an late bind
after that if u call any method means then again it goes to Vtable

set Obj=createObject("prjM.clsM")
obj.add() 'at this point it invokes the Iunknown Interface and calls the Query Interface method which resides in VTable (that is pointer ref)

Now my question is Isthere any diff between Disp Binding Vs Late Binding

 
Old October 5th, 2004, 07:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Disp Binding = Late Binding, no difference.

rgds
Phil





Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple inheritance with VTable subhash_w C# 1 August 2nd, 2005 05:23 AM
Multiple inheritance with VTable subhash_w Visual C++ 2005 1 July 31st, 2005 02:41 PM
Multiple inheritance with VTable subhash_w C++ Programming 0 July 31st, 2005 04:40 AM
Multiple inheritance with VTable subhash_w BOOK: Professional C++ 0 July 31st, 2005 04:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.