Free Educational Resource Center for teachers and students. Includes Interviews,
Sourcecode, Free Software, Research Papers, Articles, Tutorials and much more..
     R E S E A R C H A C T I V I T Y . C O M
Our Fellow Research Center for Ph.D Schollars
Home About Submit & Earn Archives: C And C + + Programming » Dev Packages » Interviews » Php Mysql Programming » Windows Programming
search
Windows Programming > create dll easily in c

Create DLL Easily In C DLL stands for Dynamic Link library you probably already know. You put few functions (and may be classes) in external file, compile it as DLL, then you call these functions at runtime through your application, that's all the story about DLL, which is not mystery. As per its name, best practice is to load DLL file in your application at runtime and use functions in it. The second way (I will not discuss is,) let your linker attach DLL to your application executable. I will discuss the first method which is called EXPLICIT LINKING. Part 1 : CREATING DLL
NOTE: the code provided in this document is tested with visual c++ and mingw compilers, it shall work with other C/C++ compilers too.
Following is the smallest possible example of creating a DLL. Our new DLL file will have only one function called 'function1' which will only display a message box. Look at following code now, which is clear enough to understand, except few things discussed below the code:
#include <windows.h>

extern "C" {
    void __declspec (dllexport) function1() {
        MessageBox (NULL, "test message","test title",0);
    }
}
BOOL WINAPI DllMain (HINSTANCE, DWORD, LPVOID) {
    return TRUE;
}
Details:
extern "C" { ..............    }
Force C Linkage to prevent name change, so that function names appear same in DLL. Within its braces, we put our functions that will be called by another application at runtime. Now every function must follow the syntax below
<return type> __declspec (dllexport) <function name> (<function arguments>)
For example a function sums up two integers and returns the sum [in general application]:
int sum(int var1,int var2);
modified form of same function to add it to DLL, and make it visible to calling applications [in DLL]:
int __declspec (dllexport) sum(int var1,int var2);
You have noticed that, right after the return type we add __declspec (dllexport). Where __declspec is a simple define for __attribute__, to which, we have passed dllexport which means this function is to be exposed to calling applications when DLL is loaded.
Part 2 : USING DLL As I already mentioned in first paragraph, that we will be using explicit linking method, here you go. First we need to make typedef of our function pointer that we need to call. In previous example we had a function called 'function1' which return type was 'void'. So creating new type out of it will be this way Then you need to load DLL in memory, get address of function function1 then run it, and then free the library.
#include <windows.h>

typedef void (*function1_ptr) ();

function1_ptr function1=NULL;

int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { 

    HMODULE myDll = LoadLibrary("myfile.dll"); 

    if(myDll) {  
		function1 = (function1_ptr) GetProcAddress(myDll,"function1");  

		if(function1)  
			function1();

		FreeLibrary(myDll);
    }

    return 0;
}
In some cases your function's return type is compound data type, such as BOOL LRESULT, or int LRESULT, or may be 'long int', in such case, Microsoft Visual C++ and some more compilers will complain and issue an error when typedef pointer of such function is created. Solution to this problem is you need two typedefs, first for the return type then for entire function, such as: Function in your DLL is
long int __declspec (dllexport) function1(char *abc) {

   //.....

}
So, it becomes
typedef long int MYTYPE;

typedef MYTYPE (*function1_ptr) (char *);
Then make pointer and get address from DLL:
function1 = (function1_ptr) GetProcAddress(myDll,"function1");  

function1('It is a test"); //use it

I am sure it will help you go forward. Stay well... create_dll.cpp:405:9,use_dll.cpp:727:10