Overblog Tous les blogs Top blogs Technologie & Science Tous les blogs Technologie & Science
Editer l'article Suivre ce blog Administration + Créer mon blog
MENU

Creation and use the DLL in Visual Studio

17 Janvier 2013 Publié dans #c-c++, #library

We will, in the article, see some examples for creating and using the DLL in VS.

What's DLL?

Dynamic-link library , or DLL, is Microsoft's implementation of the shared library concept in the Microsoft Windows and OS/2 operating systems. These libraries usually have the file extension DLL, OCX (for libraries containing ActiveX controls), or DRV (for legacy system drivers).

But Where is the advantage to use DLL?

1. It locates in a small project, when we need we can call dll, so the project might not be too big.

2.The fact that diviser the project make it easy for group work, every one can work on a seperate part.

3.If there are some modification in one programme, only one dll should be changed, this avoids redistribute all the ptoject.

4.A lot of projects can share one common DLL, we need not to re-write the fonctions in the dll.

Example :

We will creat a very simple DLL, it contain one fonction which and calcule the factorial of one number.

In Visual C++, choose New Project and Win32 Project.

Then choose Dll and leave Empty Project and Export symbols empty.

Here is the fonction :

unsigned long int factorielle(int n)

{

unsigned long int resultat = 1;

if(n < 0)

return -1;

if(n == 0)

return 1;

for(; n > 0; n--)

resultat *= n;

return resultat;

}

We will add our fonction just after :

#include "stdafx.h"

BOOL APIENTRY DllMain( HANDLE hModule,

DWORD ul_reason_for_call,

LPVOID lpReserved

)

{

return TRUE;

}

Like this :

extern "C" __declspec(dllexport) unsigned long int factorielle(int n)

{

unsigned long int resultat = 1;

if(n < 0)

return -1;

if(n == 0)

return 1;

for(; n > 0; n--)

resultat *= n;

return resultat;

}

extern "C" __declspec(dllexport) indicate the compiler that the fonction can be called from a programme outside.

Try to compiler, you will get a file .lib and .dll. You will use them later.

Creat one client programme, it can be a simple project Console. Add this line to make sure that the DLL can be included in the project. (the files .lib and .dll should locate in the folder of the project)

#pragma comment (lib, "dll_factorielle.lib")

Then, importe the fonctions like this :

extern "C" __declspec(dllimport) unsigned long int factorielle(int n);

The fonction can be used like a normal fonction. Then you can distribute the file .dll with the .exe

ATTENTION :

After you created your project dll, you can copy the .h .cpp files in the folder of the project,

for the .h you write like this :

#ifndef __RECHERCHE_INCLUDED__

#define __RECHERCHE_INCLUDED__

#define _EXPORTS_API __declspec(dllexport)

#ifdef __cplusplus

extern "C" {

#endif

_EXPORTS_API struct result{

char * field;

char * keyword;

char * contente;

char * path;

float score;

int doc_times;

int term_times;

};

_EXPORTS_API struct result * recherche (char * champs, char * mots);

#ifdef __cplusplus

}

#endif

#endif

for the .cpp you add this :

#include "stdafx.h"

for the project that you want use the .lib and .dll, you do this :

add the link of include and lib in the parameter, and copy the .dll in the folder as .exe.

That's all!

FIN!

Publicité
Partager cet article
Repost0
Pour être informé des derniers articles, inscrivez vous :
Commenter cet article