In my previous posts, i have already told about how to create DLLs using Visual Studio.
Now i will tell about how we use the same.
How can we use DLLs?
As for i know, DLL is nothing but executable with some memory area. Whatever functions are defined in DLLs, those will be executed in DLL's memory area. If u call a function defined in DLL, then the execution of that function will happen in DLLs memory area itself. Only address of DLL or address of function define in DLL will be appended in the executable.
When we have to call the function defined in the DLL, then we have to use the functions(APIs) of windows, LoadLibrary() and GetProcAddress().
LoadLibrary is to load the library into memory manually and GetProcAddress() is to get the address of the function defined in DLL. We have to use the function pointer to assign the address of DLL function.
Ex:
void (*FunctionPtr)(int,int, int);
hDll = LoadLibrary("OurDllFile.dll");
if(hDll)
{
FunctionPtr = GetProcAddress(hDll, "MyFunctionName");
}
void (*FunctionPtr)(int,int, int);
hDll = LoadLibrary("OurDllFile.dll");
if(hDll)
{
FunctionPtr = GetProcAddress(hDll, "MyFunctionName");
}
Then u can call the function MyFunctionName() which is defined in OurdllFile.dll, using FunctionPtr.
As i have told in my first post about SingleByte(ASCII) or MultByte (Widecharacter), both types o f GetProcAddress() and LoadLibrary() is available.
Note: But, for both types, second parameter to GetProcAddress should be in SingleByte type.
Another thing is, if u include MyDllFile.lib file in the project work space, then functions of DLL will be automatically called and its addresses will be checked while compiling itself. No need to use LoadLibrary() and GetProcAddress(). Directly, we can call the functions.
As i have told in my first post about SingleByte(ASCII) or MultByte (Widecharacter), both types o f GetProcAddress() and LoadLibrary() is available.
Note: But, for both types, second parameter to GetProcAddress should be in SingleByte type.
Another thing is, if u include MyDllFile.lib file in the project work space, then functions of DLL will be automatically called and its addresses will be checked while compiling itself. No need to use LoadLibrary() and GetProcAddress(). Directly, we can call the functions.
Shared Objects in Linux Environement:
In the same way, we can use the Shared Objects in Linux with little change. First i will tell u how to create Shared objects and then i will share about its usage.
Creation
gcc -fPIC -c *.c
gcc -shared -o libMyShareObject.so *.o
Here, first line is to create object files of source (.c) files and second line is to create shared object of the name MySharedObject. I am mentioning here all source files and objects files present in the current directory using wild characters. But u can specifically mention the name of source files and object files separated by a space character.
Here, option 'fPIC' is mandatory which tells compiler to create position independent object file which is required for shared objects and 'shared' option is required for shared objects.
Note: Every shared object must start with 'lib' and followed name will be user defined.
Accessing the sared objects:
To use the shared ibjects, like DLLs in Windows, we use dlopen() and dlsym() APIs of Linux operating system. dlopen() is like Loadlibrary() and dlsym() is like GetProcAddress(). But, to use dlopen() and dlsym() APIs in our application, we have to link 'dl' library as follows.
gcc -ldl OurSource.c
See here, '-l' is replaced in the place of 'lib'(meaning is 'Link'). Actually its name will be libdl.so. In the same way we can use any shared objects and staic libraries in Linux.
Example usage:
hDll = dlopen("libMySharedObject.so")
if(hDll)
{
FunctionPtr = dlsym("MyFunctionName", RTLD_LAZY);
if(FunctionPtr)
{
FunctionPtr(); //calling the function
}
}
}
Like, how we include .lib file in project work space in windows, here if we compile the code by linking the shared object, then we need not to call the dlopen() and dlsym() APIs.
I will tell u in my next post about the creation of static libraries.
Please send a mail about ur doubts, sugeestions and my mistakes in my articles to pramoda.ma@gmail.com
Wish u gud luck...