Tuesday, 16 July 2013

Creating DLL File

In Visual Studio 2010 we can easily create .dll and .lib files by the following steps

Open a New Project in Visual Studio.


In installed templates see for the Visual C++ and then for the Win32 Console Application as shown in the figure below and give it a name.


Click on Next and then click on DLL and Export Symbol as shown below.



Now you have created a dll file now you have to input the program but before that there must be some settings in your dll file. Lets have looks on those settings.

Go to the properties of the file


Then to the Configuration Properties  and in that Configuration manager.



Select the Configuration to Active Release mode in order to make your dll file safer from the outer world interference and set platform to the Win32 if your OS is of 32 bits if not edit it to "x64" for 64 bit OS.


After configuration of the  properties goto C/C++ option there in change the Calling Convention to _stdcall(/Gz) (standard calling convention)







Write the following code to the dll file:
extern "C" _declspec(dllexport) int
      _stdcall funcName(parameters)
{
       return func;
}


Now close your visual studio and again open it or simply open a new visual studio with a new project but this time project should be empty project.


Add a new Item in the format of .cpp or create a .cpp file in which we would be calling our dll and lib files.



Now we have created our .cpp file but before going towards the coding you have to set some changes in your file properties as we have done earlier for dll file. Similarly go to the properties of the file.



Here in configuration manager change the solution to Release if your dll file is in release mode and also change your active solution platform to Win32 if your OS is of 32 bit. 


Now go to general option, in linker, there in edit and browse the additional library direction and add the release folder in it.


Here you create a new line for giving the path of the release folder consisting of your library file as well as dll file.


Now go to Input option, in same linker option, and there you type your library file name with .lib extension along with other library files.


Now you can write your program and can call your function using the dll and lib files. Here is a sample program for you. Remember here that you should mention the yellow part in your program before calling your function stored in the library.

#include<stdio.h>
#include<conio.h>
extern "C" int
_stdcall sub(int, int);
void main()
{
int c= sub(13,4);
printf("%d",c);
_getch();
}

Now if you run your program, you may get an error. This is because the .dll and .lib extention files which you have called up in your settings and in your program too, doesn't exists in the path directory of the program, so just copy the files (only .dll and .lib) from the release (or debug) folder of the dll folder and paste it to the place where your .exe file of the program had been generated.


Below are my .dll and .lib files.


And this the path where my .exe file has been generated and as you can see I have pasted my .dll and .lib files here only.



Now you may Run your program easily and without any bugs in it.


OpenCV Installation

Open-CV is a Open source Computer Vision library of a programming function for real time computer vision.
Here I'll be telling you about how to install a OpenCV in your Desktop.

Download OpenCV setup from http://opencv.org/downloads.html
Now install both software in any drive of your computer
Run cMake. This will create a library solution for OpenCV.


  • Locate the source code using the “Browse Source” Button. This is where you installed OpenCV.
  • Locate where you want to install the library using the “Browse Build” button.







  • Click Configure to create the folders and files needed.
  •  It will ask for you to specify the generator for the project. Choose the version of Visual Studio that you are using.




  • Click Finish.
  • Now click to configure, do not mind the red area as in our case we need not to worry about it and press configure again and then press generate option.






  • Open your visual studio and open your build folder in the opencv. In that you'll see the solution of open-cv. Click on open option and then finish.
  • After finish it would few time to install all files.



  • After the loading of all files you are ready to build the OpenCV, so click on build option and it would also take few time to build and then you'll get a 31 succeeded and 3 skipped.





  • Now open new visual studio and create a new project in Win32 console
  • Click finish and then load a sample program to it.






  • Since you may see so many errors in your program so to rectify them you need to do some configuration.
  • Go to properties ---> VC++ directories ---> Include directories.





  • In include directories choose the edit option.
  • New line (New folder icon in the box) ---> Browse for OpenCV2.2 - include - opencv folder
  • Again select New line ---> Browse for OpenCV2.2 - select include folder
  • Click Apply option.





  • Click on library directories, and click on edit
  • Select New line ---> browse for lib folder in OpenCV2.2
  • Click Apply.






  • Now click on Linker, then to the Input option as shown in the figure.
  • Click to Additional dependencies and copy the following libraries from the library folder and paste their path here.


Copy the library path (where you have installed your opencv):
C:\OpenCV2.2\lib\opencv_core220d.lib
C:\OpenCV2.2\lib\opencv_highgui220d.lib
C:\OpenCV2.2\lib\opencv_video220d.lib
C:\OpenCV2.2\lib\opencv_ml220d.lib
C:\OpenCV2.2\lib\opencv_legacy220d.lib
C:\OpenCV2.2\lib\opencv_imgproc220d.lib
  • Now you can run your program but before that copy the image file to the path where your this project has been created.
Here is a sample program for opencv:

#include "cv.h"      // include it to used Main OpenCV functions.
#include "highgui.h" //include it to use GUI functions.

int main(int argc, char** argv)
{
    IplImage* img = cvLoadImage( "image.jpg" ); //change the name (image.jpg) according to your Image filename.
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    cvShowImage("Earth", img);
    cvWaitKey(0);
    cvReleaseImage( &img );
    cvDestroyWindow( "Earth" );
    return 0;
}