Friday, October 9, 2009

DLLs in Windows and Shared Objects in Linux

When first time i have worked on DLLs, creation and usage, i was felt like i have learnt a good thing of Windows. To know how to create DLLs itself i took 3 days. Usage of DLLs was necessary and also, i understood how DLLs are under use in Windows.

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");
}

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.


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

Thursday, October 1, 2009

Sending SIGNALS from Shell Script to our Application: Linux Platform

Signals:

I knew that signals are like software interrupts in Linux. Earlier, I never used signals. When, my first code of device detection was inconsistent, our lead has given shell script idea and one of my friends has given signals idea.

Within next 4 days, everything was ready...!


When can we signals in our application?

If we have to do something when something happen, then we can use signals instead of constant polling using threads or processes. In our case, we were polling for device insertion detection using pthreads in Linux. Hence we shifted to use shell script independent of our application and we made shell script to send a signal to our application when device insertion is detected.

Among number of signals, two user signals are there in Linux which can be used in our application. Those are signal number 10 and 12 (SIGUSR1 and SIGUSR2).

I have used SIGUSR1 in our application.


How can we use signals in our Application?

We use ‘kill’ utility of Linux/Unix to send signals to application.

Consider the following script for example:

while [ 1 ]
do
if [ -b /dev/sdb1 ]
then
kill -10 {PID of OurApplication}
fi
done

When a valid block device is found in /dev/ directory as sdb1, then this will send a signal to our application. We have to give PID of our application to kill command.


How to get the PID of our application at run time?

When shell script implementation was over, it was working fine and robustly as a stand alone application. But when we thought to send a signal when device insertion is detected, I came to know that KILL requires PID of our application. Our next question was how to get the PID of our application at run time. Idea was combination of PS and GREP command utility.

pid = (`ps | grep OurApplication`)
kill -10 $pid

Here, OurApplication is the name of our application (say myPlayer). Variable pid will get the ProcessID of our application and use the same for sending signal.


At the END:

while [ 1 ]
do
If [ -b /dev/sdb1 ]
then
pid = (`ps | grep myPlayer`)
kill -10 $pid
fi
done

This work made to me to know about Signals and its deployment in our applications. I was happy when it was completed.

Write to me at pramoda.ma@gmail.com for ur all concerns.