Using Visual C++ from Visual Basic (Tutorial With Example)


Author: Abdulaziz Alfoudari

Version Compatibility: Visual Basic 6

More information:
This tutorial contains a dll file created with Microsoft Visual C++. and a VB project that uses a function written in the dll file. This tutorial explains how can you use the power of Visual C++ in Visual Basic.

Instructions: Copy the declarations and code below and paste directly into your VB project.


Visual Basic is a powerful language that allows you to do many great things, also you can use the API functions for things that Visual Basic can't do, but it is not as powerful as Visual C++. Visual C++ is a very big language that allows you to create any type of programs. You can create MS-DOS applications, Windows application, DLLs, etc. So, why don't you use the power of Visual C++ in Visual Basic? Can you do that? Of course you can do that.


Creating a Message function in a DLL created in Visual C++

You can use the power of Visual C++ in Visual Basic by creating a DLL file that includes the functions written in Visual C++.

In this tutorial, we are going to write a simple DLL that contains a function called "Message", this function allows you to show a message box. You can show a message box in Visual Basic, but Visual C++ can do more with the Message Boxes, however, the function "Message" is simple as I said. You will only show a message box, with OK button, that's all.

Please note that you still can test the DLL function if you don't have Visual C++ installed on your computer.



Now, let's go to work. Please follow the following steps: (Please skip the steps 1 to 8 if you don't have Visual C++ installed on your computer)

  1. Click Start, if you have Windows 9x, click Programs, and if you have Windows XP, click All Programs, and then Microsoft Visual Studio 6.0 -- Or Microsoft Visual C++ 6.0 --, and then Microsoft Visual C++ 6.0.

  2. In Microsoft Visual C++ 6.0, click File, and then New.... (Or press Ctrl+N)

  3. In New dialog, click Projects tab, select MFC AppWizad (dll) in the Projects list. Set the Project name to MessageDLL, make sure Create New Workspace is selected, and then click OK button.

  4. In the MFC AppWizard - Step 1 of 1 dialog, select Regular DLL using shared MFC DLL, select Yes, please in the bottom, and then click Finish button. Click OK button in the next dialog box.

  5. In the Workspace window, expand the MessageDLL classes item, expand the Globals item, and then double click theApp item.

  6. Please add the highlighted code by gray to theApp window:

// MessageDLL.cpp : Defines the initialization routines for the DLL.

#include "stdafx.h"
#include "MessageDLL.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

// Note!
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
// For example:
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
// Please see MFC Technical Notes 33 and 58 for additional
// details.

// CMessageDLLApp

// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!


// CMessageDLLApp construction

// TODO: add construction code here,
// Place all significant initialization in InitInstance


// Message function

int _stdcall Message(HWND hHWND)
   MessageBox(hHWND, "Hello World", "Message Box Title -- Hello World", MB_OK);
   return 0;

// The one and only CMessageDLLApp object

CMessageDLLApp theApp;


This is an explanation of Message function. First of all, we write an int word, this word mean that the function is an integer (like VB Function Func1() As Integer). The _stdcall is used to allow the users to use the Message function. The Message is the function name. The Message(HWND hHWND)  is the function parameters, the HWND hHWND parameter is used to determine the form handler.

The MessageBox is the name of a function in Visual C++, this function is used to show a message box. The MessageBox syntax is int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType). As I explained, the hHwnd is used to determine the form handler. the lpText is used for the Message Box prompt/message. lpCaption is used for the Message Box title. And finally, you can use the uType to add pictures and to show/hide buttons from the Message Box.

The last thing is the return 0, the function will return what is typed after return statement.

  1. Now, to let the developers use the functions in your DLL. You must add the function name to a file called MessageDLL.def. In the bottom of the Workspace window, click FileView tab, in the Workspace list, expand MessageDLL files item, expand Source Files item, and then double click on MessageDLL.def. Modify the file by replacing all the text with the following text:

; MessageDLL.def : Declares the module parameters for the DLL.

DESCRIPTION 'MessageDLL Windows Dynamic Link Library'

; Explicit exports can go here



Under the Exports section, you must add all the functions you want to allow the developers to use.


  1. Now we're done with the DLL. Go to Build menu, and click Compile MessageDLL menu item. If an error occur, please check the code, or download the tutorial. Now, go to File, Open.... In the Open dialog box, open your MessageDLL Project folder, go to Debug, make sure that you're showing all the file types, right-click on MessageDLL.dll file and click Copy. Now open your system folder, located at /windows/system/ in Windows 9x, and located in /windows/system32/ in Windows XP. Paste the DLL file to the system folder.

  2. Now, let's go to Visual Basic. Start Visual Basic, create a New Project. In Form1, right-click on the form and click View Code/or double-click the form.

  3. Replace all the code with the following code:

Option Explicit

Private Declare Function Message Lib "MessageDLL.dll" (ByVal hWnd As Long) As Long

Private Sub Form_Load()
Dim i As Integer

    i = Message(Me.hWnd)
End Sub


  1. Now, we are done. Visual Basic will locate the MessageDLL.dll without specifying the DLL file path, because Visual Basic always looks for the DLLs in the system folder, application folder, and many other folders.

  2. Now, run the application by clicking the Start button, or by clicking F5 on your keyboard. When the program starts, a message box will appear with a "Hello World" message.

We did all this hard work just to show a "Hello World" message. Well, it's just the beginning, know you know how to use Visual C++ functions in Visual Basic. You properly will use the functions in your Visual Basic applications.

-- -- Abdulaziz


Download The Example (Please note that the DLL file and the Visual Basic project are already compiled.)

(You'll receive an error if you started the application from Visual Basic, PLEASE COMPILE the project and then run the EXE file, so the application can locate the DLL file)