• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

VB COM instantiation/technical details - "behind the scenes" information desired

I have some questions regarding VB COM objects and was wondering if anyone who really understands this could answer the following questions:

1.  When I instantiate a COM object (using the new keyword or CreateObject) what really happens?  Object is created in memory?  Multiple objects are created in memory? For a COM built in VB, does the VB runtime get loaded into memory?

2.  If one client instantiates multiple object (more than one OF THE SAME OBJECT or multiple VB based COM objects) does the vb runtimes get loaded into memory multiple times?
         eg.  Set obj1 = New cObject
              Set obj2 = New cObject
              Set obj3 = New cObject
              In this scenario, what is actually loaded into memory?

3.  When a component is hosted in COM+, does the answer to question #2 change?  I know that VB is not multi-threaded and cannot have multiple objects loaded on individual threads, but can have multiple instances of an object on a single thread.  What really is the difference between multiple instances in a single thread -vs- multiple objects on multiple threads?  Just the ability to asynchronously execute methods?

I have been trying to find any documentation that can help me better understand the internals of what is going on but have been unsuccessful in my search.  Any help in better understanding the above questions would be most aprpeciated.

2 Solutions
Ok, VB always needs it's runtime.

For each new obj you'll get a new obj regardless of the class

VB is single- and appartment-threaded. All threads run in the same memory space.

If one object fails and the failure is fatal then all objects on the thread fail. The applications end's

In com or com+ the object are created in the process of the transaction manager in there own memoryspace so failure of one object is not fatal for the rest. (if created in own process)

Your assumption on asynchronously executing methods is correct. However you have to use a callback method to catch succes

Regards PhiliP
For question 1:

When you create an object with CreateObject() or the "New" statement several ole api calls come into action, the most important of which is CoCreateInstance(). That is the api call that really creates the ole object. Ancillary ole support routines such as CLSIDFromProgID may be called when you use CreateObject.

The Visual basic loads when your VB exe file runs. If it isnt present then you will get a missing dll error of some type typically.

For Question 2:
No it is not instantiating an object that causes the VB runtime to load. The runtime loads per process, anotherwards per VB exe file. The Runtime will be loaded once for the current process regardless of whether you instantiate objects or not. When you create an object a data table of pointers is created in memory that point to the address of each method in each interface. And if the object resides in an exe (out-of-process) then the object server is loaded. I am not clear if (in-process) servers are loaded on demand or when the VB exe file loads.

Here is a link to a short Microsoft document on the subject:


Question 3:
Don't know about COM+. But it is possible to create objects that each run on their own thread from a visual basic program. They must reside in an out-of-process exe module, typically they will have the "object per thread"  checked (if created with VB) and you must use createobject to instantiate them.

You can find some good articles on the subject at:

Look for articles by Francesco Balena, Matthew Curland and William Storage. In the archives mostly 97-2000.

This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.
Moderator, my recommended disposition is:

    Split points between: PhiliP and Marshawk

DanRollins -- EE database cleanup volunteer

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now