Creating a DLL for Excel or Visual Basic with lcc-win32

Create a directory of your choice (I had troubles on my old Windows if I do it through wedit).

Open wedit and select to create a new project (may be you are asked to give a login name):

Name your project and select the path through the "browse" button (it will insert your selection).

Choose the option "Multi user" and "Dynamic Link Library" as project type.

Hit the create button.

You will be asked whether you want to use a wizard to help you. Say "yes". After that confirm again:

After confirming you want to agree with the compiler settings:

while in the Linker settings activate the check box "Do not include underscores in dll exports".

If you would have "underscores" then the function(s) to be called from the DLL will have different names
and even if that is systematic it is tedious to find what to state in the declarations for Excel.







The entry point for Excel is named LibMain, no need to change it now (or to care what it means).

The Debugger settings can be accepted as well - then wedit opens with the file vb_dll.c
 
 

After that use a text editor to create a file vb_dll.txt and insert the following:

LIBRARY vb_dll
EXPORTS
  add
Save the file in your project directory (at the same level where you have the C source vb_dll.c).

Then rename the text file and call it vb_dll.def - even if Windows complains: take that extension.
 

Through the menu open the project configuration and change the settings for the Linker by adding
the export file as "..\vb_dll.def" - observe the two dots and the back slash.

You will be asked whether the makefile should be rebuilt, agree that.

Likewise you can add the def-file through the menu: Project / Add/Delete files and then use the button
"Add new file " to select the def-file (showing all files).

Do not use both ways to add a def-file to the project!
 
 

Then at the end of the C source insert your function (which is exported through the def file):

extern __declspec(dllexport) double __stdcall
add (double a, double b)
{
 return a + b;
}
Choose Compiler / Rebuild all and after saving you will find your DLL in the local subdirectory "lcc".

If you have MSVC you can see the functions in the DLL

But this is not need it is just to see, that there are no "underscores" (there wont be any).
 
 

All you need now is to create an Excel file and use the following code in its project:

Option Explicit

Declare Function add_DLL _
  Lib "C:\_Work\other\LCC_Work\VB_DLL\lcc\vb_dll.dll" _
  Alias "add" ( _
  ByVal a As Double, ByVal b As Double) As Double

Sub tst()
Dim a As Double, b As Double

' take some randomized test values
a = Rnd()
b = Rnd()

Debug.Print add_DLL(a, b); a + b
End Sub


Done.
 

AVt, Nov 2005