Creating the DLL (VC++.NET)

Fire up VC++.NET and select “File->New” to create a new project. You’ll be presented with a large dialogue asking you which language you want to write in and what kind of project you want to create in that language, so select "Visual C++ Projects" from the list on the left and "Win32 Project" from the list on the right (Yep, I do mean that and not MFC DLL - I’ve not just lost the plot! All the base Win32 projects are located in this tab under VC++.NET)
Before you hit Ok, type in a name for the DLL in the name field, this name is important as it will define a lot of things later on so make sure you remember it and also the path that the files will be created in. For this demo I’m calling the DLL "VCNETDLL" and it’s in the path "C:\DLLdev\VCNETDLL" however you can call it whatever you want and put it wherever you want, just make sure you know where they are!

The next screen allows you to select what project type you want to create so click on the "Application settings" tab and select "DLL" as the "Application type".

After some grinding of the hard disk the dialogue will disappear and drop you back into the IDE where you can arrange the panels to taste.
If the Solution explorer isn’t already visible, you can display it with “Ctrl+R” and double click on the “[DLLName].cpp” document to open it.
This is the main code file for your project and is where you’ll add your custom code for the time being, for larger projects it’s better to create new .cpp and .h documents and use those rather than cluttering up the DLL’s main file however in this case it’s ok.
You’ll see that there’s only one function defined so far “DllMain” which is effectively the “Form_Load” of a DLL however for the time being we’re not interested in what it does and how it does it, just know that it has to be there for the DLL to function correctly.
At the end of that file we can our new method, which will look like this:

int _stdcall ReportVersion() {
return 1;
}

Make sure you remember the semi-colon at the end of the middle line, this is a constant stumbling block for beginner C++ programmers and I can guarantee you’ll get stung with it more than a few times!
If you have done some basic C++ before then you may be wondering about the “_stdcall” keyword, this is just to make sure that the function uses to the same way as VB for passing variables about and working with the stack. Since we’re not passing anything to this method, you could leave it out but for completeness we’ll leave it in.
If you’ve never written any C++ before then this is basically the same as the VB function:

Function ReportVersion() As Long
ReportVersion = 1
End Function

It will simply return 1 when you call it, nothing complicated but it will allow up to make sure that we can call it properly.
Before the DLL is ready for use in VB, we must make sure that the function(s) we create are viewable by outside applications and as such we must create a special kind of file called an "Export definition list" which is nothing more complicated than a list of function names to export so applications know what they’re looking for when querying the DLL for methods. To add a new file to the project, select "Project -> Add new item -> Visual C++ -> C++ -> DEF file (.def)" Then in the name field type "[DLLName].def".

Once you hit ok, the file will be created, added to the project and opened for you in the IDE.
You’ll see that there’s already a line in the file:

LIBRARY[DLLName]

This just reports the library name to other applications, we need to add the export list below that. This starts with the keyword “EXPORTS” then a list of the function names so ours will look a little something like this:

LIBRARY[DLLName]
EXPORTS
ReportVersion

You can add comments to this file by prefixing the comment text with a semi colon character so you could also write:

LIBRARY[DLLName];Test VC++.NET DLL project
EXPORTS
ReportVersion;Reports our version number for the DLL file

Hit F5 to run the project as you would in VB and you’ll be presented with the dialogue:

"These project configuration(s) are out of date
[DLLName] – Debug Win32
Would you like to build them?"

Select yes and you’ll see the build log being generated as the files are compiled and linked, as long as everything went ok you should see something like this:

------ Build started: Project: VCNETDLL, Configuration: Debug Win32 ------

Compiling...
stdafx.cpp
Compiling...
VCNETDLL.cpp
Linking...
Creating library Debug/VCNETDLL.lib and object Debug/VCNETDLL.exp

Build log was saved at "file://c:\DLLdev\VCNETDLL\Debug\BuildLog.htm"
VCNETDLL - 0 error(s), 0 warning(s)


---------------------- Done ----------------------

Build: 1 succeeded, 0 failed, 0 skipped

If you get any error’s or warnings then have a look back through your code and make sure you’ve not made any mistakes although there’s very little to get wrong at this point since we’ve only added a few lines!

Finished code for chapter 1b:
Form

Move on to chapter 2
Back to the index