Dynamic Link Library ( DLL ) creation - Quick Reference

Introduction

Dynamic Link Library Quick Reference

Implementation

Step 1

	Create a Win32 Dynamic Link Library project and add a .cpp & a .h file.

Step 2

	In  the .cpp file, create a class instantiated from the CWinApp file.

		   
	# include 
	# 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;

Step 3

	In the .h file (Here it is SourceFile.h) define the functions to be used.
	Also specify the "dllexport" value for the _declspec function.

	extern "C" _declspec(dllexport) int Calc(char no1,char no2)
	{
  	  char result;
	  result = no1 + no2;
	  return result;
	}

Step 4

 	Then Compile the Dll.

Step 5

	Then Create a normal Win32 Application with a .cpp file & a .h file.

Step 6

	In the .h file, ( Here it is AppHeader.h )declare the function with the 
	dllimport value of _declspec

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

Step 7

	In the .cpp file, use the function.

	# include 
	# 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()
	};

Step 8

	In the Link tab of the "Project->Settings" Dialog, Go to the Text Box labelled
	"Object / Library Modules" and specify the path of the Dll File.

	Then copy the compiled dll file to your current app path directory and run the program.

	Note :- The DLL file may not be visible due to the File View Options in the
		Windows folder. So, U 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.

Note :

	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.	

	   _declspec(dllexport)CString Display(CString a,CString b)
	   {
		AFX_MANAGE_STATE(AfxGetStaticModuleState());
		CString Str;
		Str = a + b;
		return Str;
	   }

Conclusion

THATS ALL FOLKS. A Simple tutorial for a dynamic link library. All Luck.
Hosted by www.Geocities.ws

tutorials /
dll
home /
Hosted by www.Geocities.ws

1