Nitro for VB

Let it breathe

Now we have a library without any objects in it. Let's add our first object (in this tutorial, we will only use one object, but added more objects works analogically to this).


Move your mouse over the MyLib classes root node and press the right mouse-button. From the context menu choose "New ATL Object...".


In the next dialog choose the "objects" category and mark "Simple Object" as object. Press next.


Enter a short name for your object (this is VC++ internal, you won't see this in your VB app). I choosed "MyObject". VC++ will create a C++ class names "CMyObject" from this COM-object. You could rename it, but we will keep that name. You can specify the name of the CoClass at the "CoClass" textbox. This will be the name of the COM-object as it appears in the interface description. That means, this will be the name of the object as you will see it in VB. Press "OK".


Here is our object. As you see, it only has a constructor "CMyObject()" and a interface "IMyObject". We will now add a function to our object. This function should implement a loop, that adds x times a value to 0 and then return the result. So this is a multiply realised by adding a value. Later on we will use this function to measure the time that our C++ function takes and compare it with VB.

The function should get 2 parameters.

  1. lValue as Long ' this is the value that is always added  
  2. lCount as Long ' lCount times will the loop be executed  
  4. rc as Long ' this should be the return value  


Move your mouse over the interface node and right-click it. From the popup-menu choose the option "Add Method...".


Give our method a name. I took "Calculate". This will also the name as you will see it in the VB IDE. Type in our parameter list:

  1. long lvalue, long lcount, [out, retval] long* rc

O.K., I think the first 2 parameters are clear if you understand a littlebit about C++. But why is there a third input parameter with such a funny declaration? And where is our return parameter?

COM interface functions always have the same format: They have a variable list of parameters and also return a return-code as HRESULT. If the HRESULT isn't S_OK the function failed and a COM error is raised. The VC++ IDL (Interface Decription Language) compiler interpretes the parameters [out, retval] and declares the parameter as an out-parameter and as the (one and only) return value. You must pass the variable as a pointer, because this is the only way to get data out of this function. That's the whole magic.


This is what our function looks like. Double-click it and the VC++ IDE brings you directly to the source of that function. Enter the following code:

  1. STDMETHODIMP CMyObject::Calculate( long lvalue, long lcount, long *rc)  
  2. {  
  3.     long lresult = 0;  
  4.     for ( long i = 0; i < lcount; i++)  
  5.     {  
  6.         lresult = lresult + lvalue;  
  7.     }
  9.     *rc = lresult;  
  11.     return S_OK;  
  12. }

Next: Compiling | 1 | 2 | 3 | 4 | 5

1 comment

Tutorial Console

Tutorial by:

Torsten Damberg

Date: 2000 May 11


1 comment

Latest comment

by: Soulmartyr

I'm not sure about anyone else, but in VB 2005 Express Edition, things seem to run just as fast as VC++ when you compile them and run the stand-alone.

Is anyone else seeing this? I made two different .DLL libraries, one in VB2k5, one in VC++. The method in the libraries did a calculation of the SIN(30) *2 in a for-next loop for five-thousand repitions. I saw an average difference of .0000002 seconds in running the two different versions. I used QueryPerformanceCounter to time everything.

Post a Comment

Printer Friendly

Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.
There have been 47 visitors within the last 20 minutes
RSS News Feed