Wednesday 30 January 2013

Shift Operator (signed/unsigned)

Shift operator should be used very carefully .Because a little change in shift operator makes a major change in output.So it is very necessary to understand how it is internally work.Today I run a program which explains about shift operator .We need to take many signed & unsigned int & char variables.Then assign them some value.After performing right shift operation we conclude that the value of an integer become half of the previous value if we right shift the value by 1.Thus as we right shift by different values our previous value become half of its previous value.If we right shift a char say B by 1 which has a ASCII value 66 then it converts   char which has a ASCII value 33.The Same theory is applied to conversely applied to the right shift which double the previous value if we right shift by 1.If we right shift by 4 then it doubles the value 4 times .So It very useful in various opearation.    

Tuesday 22 January 2013

Creating and using a DLL in C

Today I create a dll in C on microsoft visual studio 2010.Following are the snapshots of steps which we take during creating a dll in  microsoft visual studio 2010.








   After clicking finish we see several type of files in the project which are shown in below snapshot.


where temp is the name of project & cpp file.
In the temp.cpp file we need to write the definition of function & header files included in function writen in either temp.cpp or temp.h.Then we need to run which create temp.lib file in directory.By adding this temp.lib file into any project we easily use the dll only by adding used header in function into .h or .cpp file & calling the function.
we need to write __declspec (dllexport) for Cpp file & extern "C" __declspec (dllexport) for C file while creating library.



Here are some important keyword used in creating and using DLL:-

__declspec:-It is a storage class specifier .It uses various storage class attribute to enable various usage of it.

dllexport:-dllexport storage class attribute enables us to export function ,data & objects from the dll.Declaring function as dllexport eliminates the need for a module definition file ,at least with respect to the
specification of exported functions .Note that dllexport replaces the _export keyword .If a class is marked declspec (dllexport ),any specialization of class templates in the class hierarchy are implicitly marked as declspec(dllexport ).This means templates are explicitly instantiated and its member must be defined .The declaration of dllexport must use extended attribute syntax and the _declspec keyword.
Ex:-


__declspec( dllexport ) void func();


dllimport:-dllimport storage class attribute enables us to import function ,data & objects from the dll.The declaration of dllimport must use extended attribute syntax and the _declspec keyword.
Ex:-


__declspec( dllimport ) int i;

naked:-For the function declared with the naked attribute ,the compiler generates code without prolog and apilog code.Naked functions are particularly useful in writing virtual device driver.The naked code is only valid on x86 and is not available on x64 or itanium.
Ex:-


__declspec( naked ) int func( formal_parameters ) {}