Programmer's Corner - Creating a C++ DLL for use in Visual Basic 6

Backup and Security Solutions 10% off all products with promo code: VISI-P1YR
Get the Programmer's Corner FireFox Search Plug-In

Creating a C++ DLL for use with VB6 – Step by Step

 

If you've ever had a piece of code which needs a small part in c++, here's a step-by-step guide to creating a simple c++ dll, and calling a function from VB.

Create the C++ Project:

1. Open Visual C++ 6.0

2. File > New > Win32 Dynamic-Link Library (on Projects tab).

3. Give it a name and hit ok. (my project will be named SampleProj)

4. On the next screen select “An empty DLL project” and click finish

5. Click ok on the project screen

 

Add files to your project:

1. In the project, go to File > New

2. On the Files tab select a C++ Source File and give it a name (mine will be SampleCode.cpp)

3. Repeat steps one and 2, but this time choose a C/C++ Header File. Name it the same as the source file with a .h extension (mine is SampleCode.h)

4. Repeat steps one and 2, but this time choose a text file and name it exports.def

5. In the Workspace bar on the left, select the FileView tab from the bottom, and confirm that all 3 files have been added to the project. The cpp file and exports.def should show up under Source Files, and the .h file should show up under Header Files.

 

Add to SampleCode.h

#include <windows.h>   
int nFirstNum;
int nSecondNum;   

Add to SampleCode.cpp

#include "SampleCode.h"   
   
//This Function will accept a String And two integers   
//It will display the String in a message box, And Return    
//the sum of the two integers   
//This Is intended As a sample of passing info between   
//c++ And visual basic   
   
// Function definition   
int __stdcall MyFunction(char *szString, int nFirstNum, int nSecondNum)   
{   
    //Display a messagebox With the String (just wanted To show you   
    //how To pass a String)   
    MessageBox(NULL,szString,"Sample Code", NULL);   
   
    //Return the sum of the two integers. Notice a VB Long Is an Integer   
    //in c++.    
    return nFirstNum + nSecondNum;   
}   

Add to exports.def

EXPORTS   
    MyFunction    @1   

Additional export functions would be @2, @3, ...

Build the dll

1. Press F7 to build the project (or Build > Build SampleProj.dll). In the build window at the bottom of the screen you will see the results. It should end up with 0 errors and 0 warnings.

2. Browse to the folder with the source files. A new subfolder will be there called 'debug'. In this folder you will find the new dll (SampleProj.dll)

3. When making your final version you should build a 'release' file. To do this, under Build > Set Active Configurations choose SampleProj - Win32 Release, and then build your project again. A folder named 'release' will be added to your source code folder with the release version of the dll. (this document will not cover the differences)

4. Note the path to the dll, you will need it in the VB code

We're ready for the VB code. For the sample, we will keep it simple.

Create the sample project

1. Open VB6 and create a standard exe project

2. Paste the following code into the Form1 code section

Option Explicit   
   
'Declare the c++ dll. In the real world you will probably do this   
'in a module. Note the path to your dll might be different than mine.   
'Also note that the values are being passed ByVal 
Private Declare Function MyFunction Lib _   
    "c:\pub\sampleproj\debug\SampleProj.dll" (ByVal szString As String, _   
    ByVal nFirstNum As Long, ByVal nSecondNum As Long) As Long   
       
Private Sub Form_Load()   
    Dim lngResults As Long   
    'call the function. It should display a messagebox with the string   
    'which will originate from the dll, and then another messagebox   
    'from vb with the total of our two longs 
    lngResults = MyFunction("This is a test.", 5, 4)   
    MsgBox "The total is " & CStr(lngResults)   
    Unload Me   
End Sub
Author Information:

Josh Fruits

http://www.programmers-corner.com

josh@programmerscorner.com

Comments:

Add your comments here.

Name

Comment

You can also send feedback to feedback@programmers-corner.com


P - January 12, 2005 8:23 AM

The tutorial was very helpful. Particularly the Release version thing. I was scratching my head for 2 days why I was unable to create an exe. The dll I took was from a Debug one but with release version it worked like a charm.


Jay - September 28, 2005 10:49 PM

This is a big help. Thanks!


Toni - October 5, 2005 11:20 PM

Dear Josh

Thanks for your description. I tried to create
a dll with the defines "..(dllexport) / (dllimport)". Everything was compiled without an
error but in excel I got a run-time error '49'
(wrong DLL parameter calling) ??

Thank you very much
Toni
(P.S.: Your source code was implemented in an MFC-DLL Project with a MS Visual C++ compiler 2003 and Excel 2003)


Mon - November 1, 2005 11:58 PM

just a question, can an axported function call non-exported ones? for example, if my VB app calls myfunc from a dll, can myfunc be written in such a way that it calls other functions within the dll to accomplish its task?


SangeetaSK - November 3, 2005 5:59 AM

Thnx alot for making us to know abt building .dll


LIN - December 6, 2005 5:40 AM

how to call this function in c++?


Josh - December 19, 2005 9:10 AM

Toni - Don't know anything about an MFC DLL. I'm more of a VB programmer. The best I can offer is stick with the Win32 Dynamic Link Library project, and use the exports.def file rather than dllexport (which was the originally way I went and then had to change).

Mon - Yes. That is how I have implemented it. I needed to automate some things that had a C++ API, but no resources for VB. My result was to create functions such as this as a wrapper to the ones unavailable to VB.

LIN - No clue. I created this with the purpose of calling it from VB. My guess would be there are better ways to create DLL functions that are called from within C++.


Manoj - January 6, 2006 3:24 AM

This one is Very helpfull for me. But one more thing we are going to is, how we will register this dll in system registry then how will we use this dll in VB.


Bearnerd Bondly - January 13, 2006 1:28 AM

Thanks for this very easy step by step guide to create and use c++ dll! I've been searching this for so long through internet and i think this is the best sample. Thank you so much!


John Smith - January 21, 2006 7:39 AM

Thanks. Very helpful.

I wonder how I pass Excel Range to a C++ array. Thanks again.


Lizy - February 16, 2006 1:47 AM

Really very very... helpful.Thanx a lot


Arun - February 28, 2006 9:25 AM

This is a very good tutorial for begineers who try to create a dll in C++ and call it from VB. Very clear and Precise.


Kishore babu - March 2, 2006 3:24 AM

Sir,
I need to create win32 Dynamic Link-Libray in VC++ 6.0.I follwed these steps File>New>Win32-Dynamci-Link Library>Empty Project>

Then i created a Hello world program in one .c file and i executed the program.It is working fine and giving a .dll output in the Debug folder.
I am not able to see the .lib file in the Debug folder.I think i should get the .lib file in that folder. what is the reason for this.