Creating DLL Files in C

The Microsoft Windows DLL examples in this chapter were prepared and tested with Microsoft Windows 95 and Windows NT Workstation 4.0 (SP2) using Microsoft Visual C++, version 4.2b. If you use another compiler, you may also need the Microsoft Win32 SDK.

Creating a Microsoft Windows DLL with Visual C++ is very simple. When you create a new project for your DLL, select "Dynamic-Link library" from the list of project types in the New Project Workspace dialog. For more information, select the Search command from the Help menu and search for the topic "DLL Creation."

When you create a simple DLL with Visual C++, you do not need to write a DllMain function. However, if your DLL must perform some action when it is initialized or unloaded, you will need to implement DllMain (the example DLL in Chapter 8 uses a DllMain function to initialize string byte counts when the DLL is first loaded).

A Simple Example

The CIRCUM.MAK project in the SAMPLE\CIRCUM directory builds a simple Microsoft Windows DLL. The DLL contains a single exported function, called CalcCircum, which takes the radius of a circle as its argument and returns the circle's circumference.

double * WINAPI CalcCircum (double *pdRadius)
{
    *pdRadius *= 6.283185308;
    
    return pdRadius;
}

Functions that will be called from Microsoft Excel in 32-bit Microsoft Windows should be declared as __stdcall. The example above uses the WINAPI typed for __stdcall, as well as the other Windows example code in this book.

For more information about using this DLL, see the next section, "Calling the Function from Within Microsoft Excel." For information about calling a DLL from Visual Basic, see Chapter 5, "Using DLLs from Visual Basic."