Click here to Skip to main content
12,299,397 members (55,428 online)
Click here to Skip to main content
Add your own
alternative version


102 bookmarked

Beginner's Tutorial: Calling Visual Basic ActiveX DLLs from Visual C++

, 18 Nov 1999
Rate this:
Please Sign up or sign in to vote.
<!-- STEP 2. Include download and sample image information -->
  • Download source - 8 Kb
  • This tutorial is based off of the MSDN Article #ID: Q194873. But, for a beginner, following these MSDN articles can be intimidating to say the least. One of the most often asked questions I see as a Visual C++ and Visual Basic programmer is how to call a VB DLL from VC++. Well, I am hoping to show you exactly that today. I am not going to go over the basic details of COM as this would take too long, so I am assuming you have an understanding of VB, VC++ and a little COM knowledge. It's not too hard to learn; just takes a little time. So let's get started.

    The first thing you need to do is fire up Visual Basic 6 (VB 5 should work as well). With VB running, create a new "ActiveX DLL" project. Rename the project to "vbTestCOM" and the class to "clsTestClass". You can do this by clicking in the VB Project Explorer Window on the Project1 item (Step 1), then clicking in the Properties window and selecting the name property (Step 2). Do the same for the Class. Click on the class (Step 3), then the name property and enter the name mentioned above (Step 4). Your project so far should look like the folowing right hand side picture:

    Naming the Project

    Ok, now we are ready to add some code to the VB Class. Click on the "Tools" menu, then select the "Add Procedure" menu item. The Add Procedure window will open up. In this window we need to add some information. First (Step 1) make sure the type is set to Function. Second (Step 2) enter a Function name called "CountStringLength". Finally hit the Ok button and VB will generate the new function in the class.

    Add Procedure Window

    You should have an empty function with which to work. The first thing we will do is specify a return type and an input parameter. Edit your code to look like this:

    Public Function CountStringLength(ByVal strValue As String) As Long
    End Function

    What are we doing here? We are taking one parameter, as a String type in this case, then returning the length through the return type, which is a Long. We specify the input parameter as ByVal, meaning VB will make a copy of this variable and use the copy in the function, rather than the default ByRef, which passes the variable by reference. This way we can be sure that we do not modify the string by accident that was passed to us by the calling program. Let's add the code now.

    Public Function CountStringLength(ByVal strValue As String) As Long
    	If strValue = vbNullString Then
            	CountStringLength = 0
            	CountStringLength = Len(strValue)
        	End If
    End Function

    In the first line of code we are checking to see if the calling program passed us an empty, or NULL, string. If so we return 0 as the length. If the user did pass something other than an empty string, then we count it's length and return the length back to the calling program.

    Now would be a good time to save your project. Accept the default names and put it in a safe directory. We need to compile this project now. Go to the File menu and select the "Make vbCOMTest.dll..." menu item.

    Compiling the ActiveX DLL

    The compiler will produce a file called surprisingly enough: vbCOMTest.dll. The compiler will also do us the favor of entering this new DLL into the system registry. We have finished the VB side of this project, so let's start the VC++ side of it. Fire up a copy of VC++, then select from the menu, "New Project". The New Project window should appear. Select a "Win32 Console Application" (Step 1), then give it a name of "TestVBCOM" (Step 2). Finally, enter a directory you want to build this project in (Step 3 - your directory will vary from what I have entered).

    VC++ New Project Window

    Click on the "OK" button and the "Win32 Console Application - Step 1 of 1" window will appear. Leave everything on this page as the default, and click the "Finish" button. One final window will appear after this titled "New Project Information". Simply click the "Ok" button here. You should now have an empty Win32 Console project. Press the "Ctrl" and hit the "N" key. Another window titled "New" will appear. Select the "C++ Source File" (Step 1), then enter the new name for this file called, "TestVBCOM.cpp" (Step 2 - make sure the Add to Project checkbox is checked and the correct project name is in the drop down combo box), then click the "Ok" button to finish.

    Adding a New C++ Source File

    Now we are going to get fancy! You need to go to your Start Menu in Windows and navigate to the "Visual Studio 6" menu and go into the "Microsoft Visual Studio 6.0 Tools" sub-menu. In here you will see an icon with the name "OLE View". Click on it.

    Starting OLE View

    The OLE View tool will open up. You will see a window similar to this one:

    Starting OLE View

    Collapse all the trees, if they are not already. This will make it easier to navigate to where we want to go. Highlight the "Type Libraries" (Step 1) and expand it. You should see a fairly massive listing. We need to locate our VB DLL. Now, remember what we named the project? Right, we need to look for vbTestCOM. Scroll down until you find this. Once you have found it, double click on it. A new window should appear - the "ITypeLib Viewer" window. We are only interested in the IDL (Interface Definition Language) code on the right side of the window. Select the entire IDL text and hit the "Ctrl" and "C" buttons to copy it to the clipboard.

    Copying the IDL file

    You can close this window and the OLE View window now as we are done with the tool. We need to add the contents of the IDL file into our VC++ project folder. Go to the folder you told VC++ to create your project in and create a new text file there (If you are in Windows Explorer, you can right click in the directory and select "New" then scroll over following the arrow and select "Text Document"). Rename the text document to "vbCOMTEST.idl". Then double click on the new IDL file (VC++ should open it if you named it correctly with an IDL extension). Now paste the code in the file by pressing the "Ctrl" and "V" keys. The IDL text should be pasted into the file. So far, so good. Now, this IDL file is not going to do us much good until we compile it. That way, VC++ can use the files it generates to talk to the VB DLL. Let's do that now. Open a DOS window and navigate to the directory you created your VC++ project in. Once in that directory, at the prompt you need to type the following to invoke the MIDL compiler:

    E:\VCSource\TestVBCOM\TestVBCOM\midl vbTestCOM.idl /h vbTestCOM.h

    Hit the "Enter" key and let MIDL do its magic. You should see results similar to the following:

    Compiling the IDL file with the MIDL compiler

    Close the DOS window and head back into VC++. We need to add the newly generated vbTestCOM.h and vbTestCOM_i.c files to the project. You can do this by going to the "Project" menu, then selecting the "Add to Project" item, and scrolling over to the "Files" menu item and clicking on it. A window titled, "Insert Files into Project" will open. Select the two files highlighted in the next picture, then select the "Ok" button.

    Adding the files generated by MIDL

    These two files were generated by MIDL for us, and VC++ needs them in order to talk to the VB DLL (actually VC++ does not need the "vbCOMTest_i.c" file in the project, but it is handy have in the project to review). We are going to add the following code to the "TestVBCOM.cpp" file now, so navigate to that file in VC++ using the "Workspace" window. Open the file by double clicking it and VC++ will display the empty file for editing.

    Updated Workspace Window

    Now add the following code to the "TestVBCOM.cpp" file:

    #include <iostream.h>
    #include <comdef.h>
    #include "vbTestCOM.h"
    void main()
       	// Declare an HRESULT and a pointer to the clsVBTestClass interface
       	HRESULT		hr;
    	_clsVBTestClass	*IVBTestClass = NULL;
    	// Now we will intilize COM
    	hr = CoInitialize(0);
    	// Use the SUCCEEDED macro and see if we can get a pointer 
    	// to the interface
    		hr = CoCreateInstance( CLSID_clsVBTestClass,
    					(void**) &IVBTestClass);
    		// If we succeeded then call the CountStringLength method, 
    		// if it failed then display an appropriate message to the user.
    			long		ReturnValue;
    			_bstr_t		bstrValue("Hello World");
    			// We can test this HR as well if we wanted to
    			hr = IVBTestClass->CountStringLength(bstrValue,
    			cout << "The string is: " << ReturnValue << " characters in
    							length." << endl;
    			// We can test this HR as well if we wanted to
    			hr = IVBTestClass->Release();
    			// Something went wrong
    			cout << "CoCreateInstance Failed." << endl;
    	// Uninitialize COM

    If all the code is entered in correctly, then press the "F7" key to compile this project. Once the project has compiled cleanly, then press the "Ctrl" and the "F5" keys to run it. In the C++ code, we include the MIDL created "vbTestCOM.h" file, the "Comdef.h" file for the _bstr_t class support and the "iostream.h" file for the "cout" support. The rest of the comments should speak for themselves as to what's occurring. This simple tutorial shows how well a person can integrate VB and VC++ apps together using COM. Not too tough actually.


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    C. Lung
    United States United States
    No Biography provided

    You may also be interested in...

    Comments and Discussions

    QuestionHow to call an ActiveX DLL that returns a string from a VC++ application Pin
    David Hann3-Jan-03 3:31
    memberDavid Hann3-Jan-03 3:31 
    AnswerRe: How to call an ActiveX DLL that returns a string from a VC++ application Pin
    Hansnet8322-Jun-06 10:50
    memberHansnet8322-Jun-06 10:50 
    GeneralPassing Picture from VB to VC and Back Pin
    Sreekant25-Nov-02 23:26
    sussSreekant25-Nov-02 23:26 
    GeneralRe: Passing Picture from VB to VC and Back Pin
    KarstenK18-Dec-06 21:25
    memberKarstenK18-Dec-06 21:25 
    GeneralNeed Help Pin
    Anonymous22-Oct-02 23:36
    sussAnonymous22-Oct-02 23:36 
    GeneralRegistering VB Dlls. Pin
    Anonymous8-Oct-02 2:03
    sussAnonymous8-Oct-02 2:03 
    GeneralRe: Registering VB Dlls. Pin
    cartacarta18-Nov-02 6:47
    susscartacarta18-Nov-02 6:47 
    Generalcall VC++ COM from VB Pin
    Zoltan2-Oct-02 6:23
    memberZoltan2-Oct-02 6:23 
    GeneralRe: call VC++ COM from VB Pin
    takrimcode26-Aug-03 1:04
    membertakrimcode26-Aug-03 1:04 
    Generalaccess and create dll in VC++ 6.0 Pin
    harish agarwal23-Aug-02 1:05
    sussharish agarwal23-Aug-02 1:05 
    Generalabout c++ dll Pin
    sliba200322-Aug-02 19:44
    susssliba200322-Aug-02 19:44 
    GeneralRecieving Structures in VB Pin
    Lauracln9-Jul-02 21:24
    memberLauracln9-Jul-02 21:24 
    GeneralQuestion... Pin
    Anonymous2-Jul-02 19:56
    memberAnonymous2-Jul-02 19:56 
    GeneralHelp!! Pin
    Anonymous5-Jun-02 7:03
    memberAnonymous5-Jun-02 7:03 
    The CoCreateInstance function keeps failing!!!

    I made Class_Initialize and Class_Terminate functions which pop up message boxes. When I run the VC program, both of these functions are called because the two message boxes appear, which indicates that the VB DLL is being accessed, but they immediatly follow eachother, indicating that when the class given an instance, it is immediatly released. When I download the code example, the CoCreateInstance function still fails. Am I missing something???
    GeneralShowing a Dialog from the Visual Basic Dll Pin
    Marco A.26-Apr-02 4:23
    memberMarco A.26-Apr-02 4:23 
    GeneralRe: Showing a Dialog from the Visual Basic Dll Pin
    Anonymous30-Apr-02 0:59
    memberAnonymous30-Apr-02 0:59 
    GeneralRe: Showing a Dialog from the Visual Basic Dll Pin
    Mr.Pe19-Nov-02 20:29
    memberMr.Pe19-Nov-02 20:29 
    GeneralRe: Showing a Dialog from the Visual Basic Dll Pin
    Sabarinathan Arthanari18-Sep-08 22:18
    memberSabarinathan Arthanari18-Sep-08 22:18 
    QuestionWhat if the DLL is on a different computer? Pin
    Julian Sanchez15-Mar-02 7:22
    memberJulian Sanchez15-Mar-02 7:22 
    Questionhow to return a string to the vb activex dll Pin
    SmiLeY1k2k27-Jan-02 9:04
    memberSmiLeY1k2k27-Jan-02 9:04 
    AnswerRe: how to return a string to the vb activex dll Pin
    Stan Lin7-Feb-02 1:51
    memberStan Lin7-Feb-02 1:51 
    GeneralHelp - Please Pin
    Anonymous622-Jan-02 10:29
    memberAnonymous622-Jan-02 10:29 
    GeneralExcellent Article Pin
    Steve Thresher22-Jan-02 6:22
    memberSteve Thresher22-Jan-02 6:22 
    GeneralRe: Excellent Article Pin
    Günter15-Feb-02 7:32
    memberGünter15-Feb-02 7:32 
    GeneralPlease help with this string problem. Pin
    me_again21-Jan-02 3:22
    memberme_again21-Jan-02 3:22 
    GeneralRe: Please help with this string problem. Pin
    Carlos Antollini21-Jan-02 3:39
    memberCarlos Antollini21-Jan-02 3:39 
    GeneralRe: Please help with this string problem. Pin
    me_again21-Jan-02 4:37
    memberme_again21-Jan-02 4:37 
    Generaldll Pin
    me_again20-Jan-02 15:59
    memberme_again20-Jan-02 15:59 
    Questionwhat about passing structures? Is this possible? Pin
    Anonymous10-Sep-01 12:02
    memberAnonymous10-Sep-01 12:02 
    AnswerRe: what about passing structures? Is this possible? Pin
    Anonymous20-Sep-01 19:37
    memberAnonymous20-Sep-01 19:37 
    GeneralRe: what about passing structures? Is this possible? Pin
    Anonymous27-Sep-01 12:35
    memberAnonymous27-Sep-01 12:35 
    GeneralRe: what about passing structures? Is this possible? Pin
    Ben Bradley18-Dec-01 0:04
    memberBen Bradley18-Dec-01 0:04 
    Generalvb is expecting an array of variant type Pin
    pats2-Aug-01 9:32
    memberpats2-Aug-01 9:32 
    GeneralRe: vb is expecting an array of variant type Pin
    Nemanja Trifunovic2-Aug-01 9:56
    memberNemanja Trifunovic2-Aug-01 9:56 
    QuestionHow to define smart pointers?where we can use? Pin
    ramki24-Jun-01 20:46
    memberramki24-Jun-01 20:46 
    Questionhow can a string in vb dll be accessed from the vc, gives a casting error Pin
    kgpsan30-Apr-01 23:03
    memberkgpsan30-Apr-01 23:03 
    AnswerRe: how can a string in vb dll be accessed from the vc, gives a casting error Pin
    Anonymous9-May-01 11:35
    memberAnonymous9-May-01 11:35 
    GeneralCalling VC++ ATL DLL methods (with arrays) from VB6 Pin
    Anonymous18-Apr-01 23:08
    memberAnonymous18-Apr-01 23:08 
    GeneralRe: Calling VC++ ATL DLL methods (with arrays) from VB6 Pin
    Jose M. Pardo21-Sep-01 2:07
    memberJose M. Pardo21-Sep-01 2:07 
    GeneralRe: Calling VC++ ATL DLL methods (with arrays) from VB6 Pin
    Anonymous18-Oct-01 9:31
    memberAnonymous18-Oct-01 9:31 
    GeneralRe: Calling VC++ ATL DLL methods (with arrays) from VB6 Pin
    Anonymous14-Feb-02 19:50
    memberAnonymous14-Feb-02 19:50 
    Generalmidl compile error Pin
    Anonymous13-Apr-01 19:49
    memberAnonymous13-Apr-01 19:49 
    GeneralCan't Get midl to work Pin
    Anonymous31-Mar-01 12:52
    memberAnonymous31-Mar-01 12:52 
    GeneralUsing ActiveX DLL from VB, VC and ASP Pin
    Roger Jarl28-Mar-01 21:47
    memberRoger Jarl28-Mar-01 21:47 
    Vlastimil Knotek21-Dec-00 1:06
    memberVlastimil Knotek21-Dec-00 1:06 
    Questionhow to retrun a string form vb Pin
    kevin13-Dec-00 0:29
    memberkevin13-Dec-00 0:29 
    AnswerRe: how to retrun a string form vb Pin
    David Rinaldi12-Feb-01 1:38
    memberDavid Rinaldi12-Feb-01 1:38 
    Questionwhere can I get mspdb60.dll?? Pin
    kevin12-Dec-00 20:03
    memberkevin12-Dec-00 20:03 
    AnswerRe: where can I get mspdb60.dll?? Pin
    Anonymous11-Jan-01 18:20
    memberAnonymous11-Jan-01 18:20 
    AnswerRe: where can I get mspdb60.dll?? Pin
    Chris B23-Feb-01 3:58
    memberChris B23-Feb-01 3:58 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.160525.2 | Last Updated 19 Nov 1999
    Article Copyright 1999 by C. Lung
    Everything else Copyright © CodeProject, 1999-2016
    Layout: fixed | fluid