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 SubAuthor Information:
http://www.programmers-corner.com
