Sunday, April 4, 2010

Creating Simple Dll

This post is a tutorial for creating a simple dll in C++.
So lets get started, 
Create new project "SimpleDll" in Visual C++. If you are using Visual C++ 6.0 then select Dynamic Link Library Project and if you are using 2005 then select Win32 Project and select Dll and Empty Project in Additional options.
After this add new header file "simpledll.h" to our project and add following code in header file.

// SimpleDll.h
#ifndef _SIMPLEDLL_H_
#define _SIMPLEDLL_H_

#include
// this is the function which we will export
VOID ShowMessage(WCHAR msg);

#endif
Next step is to add Cpp file to our project, add new "simpledll.cpp" and add following code to this file.
#include"simpledll.h"

BOOL WINAPI DllMain(HINSTANCE hInstance,DWORD dwReason,LPVOID lpReserved)
{
 /** 
 * as we are creating a simple dll
 * we are returning TRUE for every reason
 **/
 switch(dwReason)
 {
 case DLL_PROCESS_ATTACH:
 case DLL_THREAD_ATTACH:
 case DLL_PROCESS_DETACH:
 case DLL_THREAD_DETACH:
  return TRUE;
 }
 return FALSE;
}

VOID ShowMessage(LPCWSTR msg)
{
MessageBox(NULL,msg,L"Message from SimpleDll",MB_ICONINFORMATION);
}

And then final step, add new "simpledll.def" (in VS 2005 select Module-Definition File) file to our project  and add following code to this file


LIBRARY SIMPLEDLL

EXPORTS
 ShowMessage @1
 
Thats it, now build the project and your DLL file is ready!!!
Simple isn't it?

No comments:

Post a Comment

Ads Inside Post