Making DLL's in Microsoft Visual C++ 6.0

Example 1 : Working from the Command Line

Now we make a one-line DLL. Here's the source:

extern "C" __declspec(dllexport) void myfun(int * a){*a = - *a; }

Save this to file myfun.cpp and compile it from the DOS prompt with:

cl -LD myfun.cpp

The -LD switch says to generate a DLL. Next we make an executable, which calls the DLL. Here's the source:

#include iostream.h

extern C __declspec(dllimport) void myfun ( int * a);

void main(void)
   int a = 6;
   int b = a;

   cout << '-' << a << " is " << b << "! \n";

Save this to the file main.cpp. Then compile and link from the command prompt with:

cl main.cpp /link myfun.lib

Execute it from the command line (just type 'main').

Example 2 : Using VC++ IDE to Create DLL

In Microsoft Visual C++ 6.0, you can create a DLL by selecting either the Win32 Dynamic-Link Library project type or the MFC AppWizard (dll) project type.

The following code is an example of a DLL that was created in Visual C++ by using the Win32 Dynamic-Link Library project type.

// SampleDLL.cpp

#include "stdafx.h"
#include "sampleDLL.h"

BOOL APIENTRY DllMain(HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
   return TRUE;

void HelloWorld()
   MessageBox( NULL, TEXT("Hello World"), TEXT("In a DLL"), MB_OK);
// File: SampleDLL.h

#ifndef INDLL_H

   #define INDLL_H

      extern __declspec(dllexport) void HelloWorld();
      extern __declspec(dllimport) void HelloWorld();

Kickstart Your Career

Get certified by completing the course

Get Started