MFC DLL Instructions

To create an MFC DLL to be called from HTBasic using Microsoft DevStudio 6.0:

  1. First, create a new project
  2. From the file menu select New
  3. Select the Projects tab
  4. Highlight MFC AppWizard (dll) and type in the name of the new DLL in the Project Name field

  1. Choose OK
  2. Leave the default options and choose Finish on the next dialog screen
  3. Choose OK on the New Project Information Dialog

At this point a new project has been created. The MFCAppWizard has created a few files for the project to start off with.

(In Microsoft’s vocabulary, a “Wizard” is a program that will help generate code. It is very convenient for writing standard pieces of code that would require a lot of system setup. Using the wizard can eliminate the need to type in large tedious sections of system code. Of course the programmer still has to type in the source code for the parts that are unique to his program. The wizard frees him up to concentrate on that, rather than the system support code. This sample uses the wizard fairly extensively.)

Choose the FileView tab in the workspace window of DevStudio. By default this window is located on the left edge of the DevStudio program. Open the root directory and the Source Files sub directory in the window.

The workspace tree shows that the AppWizard has created 4 files: a .cpp file, a .def file, a .rc file, and StdAfx.cpp.

The .cpp file contains source code that the wizard generated.

The .def file contains some information about this DLL. This is where the labels to be exported by the DLL will be listed.

The .rc file contains resource information about this DLL. (A resource is something that the program will use that is not source code. Samples of resources are: icons, bitmaps, menus, dialog boxes, etc.)

StdAfx.cpp is a system source file. It can be ignored.

To add a dialog box into the project, click on the Insert menu and choose Resource….

On the Insert Resource dialog box, highlight the Dialog option and choose New.

This will create a dialog box that can be edited graphically. It will also show the “controls” window. It has a set of some of the objects that can be put onto the dialog.

Click on the Static Text control (the one with the Aa). Move the mouse inside the blue rectangle area in the dialog and click to place some text.

Change the size of the static text box by clicking on one of the blue squares in the border with the left mouse button. While holding the left mouse button down, move the mouse to adjust the size of the box.

To change the text in the static text box, click with the right mouse button on the box, and choose Properties on the popup menu. This will bring a dialog box to set the properties (or attributes) of the static text box. Click in the Caption: text edit box and change the word “static” to any character string.

After closing the Text Properties dialog box, the caption change will be reflected in the static text box on the main dialog.

Before using this dialog, some associated source code will have to be generated. Using the wizard is the easiest way to do this.

From the View menu choose ClassWizard. This will detect that a dialog has been created that has no associated source code. It will bring up a dialog box asking to create a new class.

Choose Create a new class and click OK.

The wizard will then display a dialog box to set some information about the new class that is being created. Type the name of the new class in the Name: edit box. Notice that the wizard will automatically create a new source file for this code.

Click OK

The wizard will now display a dialog box to set more information about the new class that has just been created.

Typically the defaults are acceptable, so choose OK.

The wizard has now created one more source file. It may be seen in the source file list by clicking on the FileView tab of the workspace window.

Next, a subprogram that can be called from HTBasic needs to be created. Place it in the main .cpp file.

Edit the main .cpp file by double clicking on its name in the workspace window.

This is the file the code wizard generated.

In order to use the new dialog class that was just created, its information must be included in the source file.

Each .cpp file has an associated .h file. Information about the source code is contained in the .h file. If a file needs information about code in another file, that other file’s .h file must be included.

Type in the “#include” command for the .h file of the new dialog that was created.

Move to the end of the file and type in the subprogram.

In this sample “ShowHTBDialog” is the name of the subprogram that HTBasic will eventually call.

“AFX_MANAGE_STATE(AfxGetStaticModuleState())” is a code macro that Microsoft requires to be at the beginning of any DLL functions or subprograms that will use MFC. This must be the first line.

The line that says “MyCoolDialog SeeDialog” is a variable declaration. A new type of variable has been defined in the file “MyCoolDialog.cpp”. SeeDialog is declared as an instance of that variable type.

SeeDialog.DoModal()" is the call that actually shows the dialog box on the screen. “DoModal” is an MFC built-in function to show dialog boxes.

In order for the routine “ShowHTBDialog” to be seen and accessed by other programs, it must be exported by listing it in the .def file.

Edit the .def file.

Enter the name of the function or subprogram to be exported. Usually it is easiest to cut and paste the name from the source file.

Save all the files and build the project by opening the Build menu and selecting the Build menu item, by pushing the “F7” key, or by choosing the build icon from the tool bar.

“0 error(s), 0 warning(s)” means that the DLL has been created and is ready for use.


Copyright 2007 © TransEra Corp.