You are on page 1of 5

Creation of a Simple DLL - CodeProject http://www.codeproject.com/KB/DLL/dll.

aspx

7,682,156 members and growing! (35,500 online) Adhom 301 Sign out

Home Articles Questions & Answers Learning Zones


Search
Features Help! The Lounge

» General Programming » DLLs & Assemblies » Beginners

Licence

Creation of a First Posted


Views
29 Jun 2002
223,831
See Also
More like this
Simple DLL Downloads
Bookmarked
0
66 times
More by this author

By VGirish | 29 Jun 2002


VC6 VC7 Win2K WinXP Visual-Studio MFC Dev Beginner

Steps to create your first DLL file

16

Article Browse Code Stats Revisions

3.08 (49 votes) Sponsored Links

Introduction
See Also...
This article shows a step-by-step technique to create your
first DLL with VC++.

Steps to Create Your First DLL


Create a Win32 Dynamic Link Library project, adding a
*.cpp and a *.h file.
In the *.cpp file, create a class instantiated from the
CWinApp file.
Collapse

The Daily Insider

1 of 5 04/05/2011 03:08 PM
Creation of a Simple DLL - CodeProject http://www.codeproject.com/KB/DLL/dll.aspx

# include <stdafx.h>
# include "SourceFile.h"

class CDllApp : public CWinApp


{
public:

CDllApp::CDllApp()
{
Calc(0,0);
}

DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(CDllApp,CWinApp)

END_MESSAGE_MAP()

CDllApp DllObject;

In the *.h file (here it is SourceFile.h), define the


functions to be used. Also specify the dllexport
value for the _declspec function.
Collapse

extern "C" _declspec(dllexport) int Calc(char


no1,char no2)
{
char result;
result = no1 + no2;
return result;
}

Then compile the DLL.


Create a normal Win32 Application with a *.cpp file and
a *.h file.
In the *.h file, (here it is AppHeader.h ), declare the
function with the dllimport value of _declspec .
Collapse

extern "C" _declspec(dllimport) Calc(int FirstValue,


int SecondValue);

In the *.cpp file, use the function.


Collapse

2 of 5 04/05/2011 03:08 PM
Creation of a Simple DLL - CodeProject http://www.codeproject.com/KB/DLL/dll.aspx

# include "AFXWIN.H"
# include "AppHeader.h"

class MainFrame : public CFrameWnd


{
public:

MainFrame()
{
Create(0,"Trial");
}

void OnLButtonDown(UINT nFlags,CPoint point)


{
int res;
char str[5];
res = Calc(998,226);
sprintf(str,"%d",res);
MessageBox(str);
}

DECLARE_MESSAGE_MAP()
};

In the Link tab of the "Project->Settings" dialog, go to


the text box labeled "Object / Library Modules" and
specify the path of the DLL file. Then copy the
compiled DLL file to your current appliation path
directory and run the program.

Some Things to Note


The DLL file may not be visible due to the File View options
in the Windows folder. So, you can either go to the DOS
prompt and copy the file or enable the setting "Show all
files" in Windows Explorer to copy the file. To create a DLL
that uses MFC, see the following example. Note that extern
"C" has not been used and the macro
AFX_MANAGE_STATE(AfxGetStaticModuleState());
has been used to implement MFC.

Collapse

_declspec(dllexport)CString Display(CString a,CString b)


{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString Str;
Str = a + b;
return Str;
}

That's all, folks. All luck and have a great time.

License
This article has no explicit license attached to it but may
contain usage terms in the article text or the download files

3 of 5 04/05/2011 03:08 PM
Creation of a Simple DLL - CodeProject http://www.codeproject.com/KB/DLL/dll.aspx

themselves. If in doubt please contact the author via the


discussion board below.

A list of licenses authors might use can be found here

About the Author

VGirish www.geocities.com/contactgirish
/AboutMe.html

Web Developer

India

Member

Article Rate this article for us! Poor Excellent Vote


Top

Comments and Discussions

FAQ Search

Noise Tolerance Medium Layout Normal Per page


25 Update

New Msgs 1 to 16 of 16 (Total in Forum: 16)


First Prev Next
Message (Refresh)

How can i create 0:19 14 Nov '07


a same type of JothiMurugeswaran
dll in vb6?
building dll 19:33 20 Apr '07
(C++) to be
P.Luke
called by C
applications
Dll-ASP.NET 6:10 23 Feb '07

Messages Gipico 9:32 27 Oct '06

C DLL without 16:47 8 Mar '05


changing the saflitzk
source code
problem with dll 7:24 10 Jun '04
dnqhung

Re: problem 17:31 14 Jun '04


Christian Kluin
with dll

4 of 5 04/05/2011 03:08 PM
Creation of a Simple DLL - CodeProject http://www.codeproject.com/KB/DLL/dll.aspx

Error C1083 in 13:07 8 Mar '04


VC++ .NET 2002 Scott H. Chang

Re: Error 8:06 11 Jan '07


C1083 in VC++ Sachin R Sangoi
.NET 2002
how to export 2:45 23 Aug '02
vb function
sailu
through c++ dll

dll compilation 2:30 23 Jul '02


MINAKANNAN
error
Re: dll 16:05 11 Mar '03
compilation Anonymous
error
Re: dll 6:43 25 Aug '04
compilation Anonymous
error
Re: dll 2:09 7 Apr '03
compilation sivo
error
Re: dll 8:08 30 Jan '04
compilation DaFrawg
error
Re: dll 2:09 7 Apr '03
compilation sivo
error
Last Visit: 2:30 5 Apr '11 Last Update: 4:00 5 Apr '11 1

General News Question Answer Joke Rant


Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads,


Ctrl+PgUp/PgDown to switch pages.

link | Privacy | Terms of Use | Mobile Copyright 2002 by VGirish


Last Updated: 29 Jun 2002 Everything else Copyright © CodeProject, 1999-2011
Web24 | Advertise on the Code Project

5 of 5 04/05/2011 03:08 PM

You might also like