Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse VC++ to create a DLL file and use its functions in Visual Basic 6
DescriptionThis example shows how to use VC++ to create a DLL file and use its functions in Visual Basic 6.
KeywordsVC++, VCC, DLL, C++
CategoriesSoftware Engineering
 
I have found 2 methods to do this. The first makes the VC++ easier and the Visual Basic messier. The second makes the VC++ harder and the Visual Basic easier.

Method 1: Easy VC++, Hard VB

  1. VC++: Invoke New\Projects\Win32 Dynamic-Link Library. Enter the directory where you want the VC++ project and the project name (MyFuncsProject in the example).

  2. VC++: Invoke New\Files\C++ Source File. Check the Add To Project box. Enter the file name (MyFuncs.cpp in the example).

  3. VC++: Enter the function's source code. Use __declspec (note the two underscores) to export the function's symbol. Use 'extern "C"' to minimize name mangling by VC++.
 
// Define DllExport to declare exported symbols.
#define DllExport __declspec( dllexport )

// Prototype the function.
// Use 'extern "C"' to minimize name mangling.
extern "C" DllExport long MyCFunc(long x);

// Define the function.
extern "C" DllExport long MyCFunc(long x)
{
    return x * x;
}
 
  1. VC++: Set project options using Project\Settings. On the C/C++ tab, select the Code Generation category. Then change Calling Convention to __stdcall.

  2. VC++: Select Build\Set Active Configuration. Select the Release configuration. Repeat step 4 to make the options apply to the release configuration in addition to the debug configuration. Use Build\Set Active Configuration to reselect the debug configuration if desired.

  3. VC++: Build the project (press F7 or use the Build menu). This creates the DLL file.

  4. VB: In your Visual Basic program, declare the DLL function using the DLL file's full path name. The function's name in the DLL file has been slightly mangled by VC++. The name is an underscore, followed by the name you gave it, followed by "@", followed by the number of bytes in the function's argument list. In this example the name is _MyCFunc@4 because the function takes one 4 byte argument (a long integer).
 
Private Declare Function MyCFunc Lib _
    "C:\VBHelper\VcDll\Method1\Release\MyFuncsProject.dll" _
    Alias "_MyCFunc@4" _
    (ByVal x As Long) As Long

Private Sub Command1_Click()
Dim x As Long
Dim y As Long

    x = CInt(Text1.Text)
    y = MyCFunc(x)
    Label1.Caption = Str$(y)
End Sub
 

*** HINT: To quickly determine the mangled name of the function, find the DLL file in Windows Explorer. Right click on the file and select the "Quick View" command. This presents an editor showing information about the DLL. Page down 2 or 3 pages and you will find a list of exported symbols available in the DLL. One of these will be the mangled function name.

  1. VB: Run the program.


Method 2, Hard VC++, Easy VB

Steps 1 through 5 are the same as in Method 1.

  1. VC++: New\Text File. Check the Add To Project box. Enter the file name. Give it a .DEF extension (MyFuncs.def in the example).

  2. VC++: Enter definition file information that tells VC++ to export the function with the mangled name using the name you want it to have. The following code makes the exported name MyCFunc equivalent to _MyCFunc@4.
 
EXPORTS
	MyCFunc=_MyCFunc@4
 
  1. VC++: Build the project (press F7 or use the Build menu). This creates the DLL file.

    *** HINT: Use Quick View to verify the exported names. Find the DLL file in Windows Explorer. Right click on the file and select the "Quick View" command. Page down 2 or 3 pages and you will find a list of exported symbols available in the DLL. This includes both the mangled name and the name you specified in the .DEF file.

  2. VB: In your Visual Basic program, declare the DLL function using the DLL file's full path name. Use the name you placed in the .DEF file not the mangled name.
 
Private Declare Function MyCFunc Lib _
    "C:\VBHelper\VcDll\Method2\Release\MyFuncsProject.dll" _
    (ByVal x As Long) As Long

Private Sub Command1_Click()
Dim x As Long
Dim y As Long

    x = CInt(Text1.Text)
    y = MyCFunc(x)
    Label1.Caption = Str$(y)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated