How can you create a dll file?

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 | Copy Code

    # 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 | Copy Code

    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 | Copy Code

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

  • In the *.cpp file, use the function. Collapse | Copy Code

    # 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.