
Visual C++ DLL in Visual Basic
How To Write a C++ DLL for Visual Basic
by Mike McKee
You can pull this off easily in 13 steps:
Assume you have VB 4/32 and VC++ 4.0.
Assume you can write an ANSI C function, bare minimum.
Launch VC++ Developer Studio.
Choose File, New... and choose Project Workspace.
Choose DLL option and type in the name of your DLL under 8 chars, as in
"netfs." Assume here for the rest of these instructions that x is your
project name.
Choose Insert Files into Project, Source Files type. Type a name like
x.cpp
Save the file before you start typing text into it.
Type into x.cpp your C functions with no regard to whether this is a DLL or
not.
Do not type in a Main, DllMain, or any other "main" type function--a DLL
should just contain functions.
Now for the fun stuff. Preface your C code at the top with:
#define CCONV __stdcall /* NOTE: __stdcall is two underscores, not one
underscore!!! */
#define NOMANGLE
And do the following conversion on each function:
Before
LONG GrantDir (LPSTR lpszDomain, LPSTR lpszAccount, LPSTR lpszDir) {
After
NOMANGLE LONG CCONV GrantDir (LPSTR lpszDomain, LPSTR lpszAccount, LPSTR
lpszDir) {
Choose Insert Files into Project, Definition Files type. Type a name like
x.def.
Save the file before you start typing text into it.
In x.def file, you must assign a definition for your project. You might
have to change some things, but generally it should look like:
LIBRARY x
DESCRIPTION 'x can be called from Visual Basic'
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE
EXPORTS
Note that the library name should be the same name as we've been using for
x all along here. Also note that the EXPORTS command requires a listing of
functions you had in your x.cpp file. Each function should be suffixed
From the Build menu, choose Build menuitem to create x.dll. It's that
easy.
Now an explanation of the trick that made this all easy. Normally, making
DLLs is a very hard process, involving a DllMain, and some other odd calls,
but when you chose New, Project Workspace, DLL, a header file is attached
to your project that does all the magic to make this a DLL.