Utiliser fonction (avec des paramètres) définie ds une DLL
Bonjour
je souhaite utiliser une fonction définie dans une DLL.
Cela marche très bien lorsque ma fonction n'a pas de paramètre mais moins bien lorsqu'il y en a.
Voci le code qui construit la DLL:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include "stdafx.h"
#include "creationDLL2.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// This is an example of an exported function.
CREATIONDLL2_API int affiche()
{
return 12;
}
// This is an example of an exported function.
CREATIONDLL2_API int affiche2(int param)
{
return param;
} |
Le .h correspondant:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the CREATIONDLL2_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// CREATIONDLL2_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
#ifdef CREATIONDLL2_EXPORTS
#define CREATIONDLL2_API __declspec(dllexport)
#else
#define CREATIONDLL2_API __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C" { // only need to export C interface if
// used by C++ source code toto
#endif
// This class is exported from the creationDLL2.dll
class CREATIONDLL2_API CCreationDLL2 {
public:
CCreationDLL2(void);
// TODO: add your methods here.
};
extern CREATIONDLL2_API int nCreationDLL2;
CREATIONDLL2_API int fnCreationDLL2(void);
CREATIONDLL2_API int affiche(void);
CREATIONDLL2_API int affiche2(int);
#ifdef __cplusplus
}
#endif |
Et voici le programme de test:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include "../src/dataprocessing.h"
#include <stdio.h>
#include "interface.h"
#include <windows.h>
typedef int* DLL_nDataProcessing;
typedef int (WINAPI *type1) ();
typedef int (WINAPI *type3) (CInterface*);
typedef int (WINAPI *type4) (void);
typedef int (WINAPI *type5) (int);
int main(int argc, char* argv[])
{
HINSTANCE hLibrary,hLibraryInterface,hLibraryTest;
// chargement de la la DLL DataProcessing.dll
hLibrary = LoadLibrary("DataProcessing.dll");
hLibraryInterface = LoadLibrary("Interface.dll");
hLibraryTest = LoadLibrary("creationDLL2.dll");
if ((hLibrary == NULL) || (hLibraryInterface == NULL) || (hLibraryTest == NULL) )
{
printf("Erreur chargement librairie\n");
return -1;
}
printf("Chargement fonction fnDataProcessing\n");
type1 fct1 = GetProcAddress(hLibrary,"fnDataProcessing") ;
type1 affiche = GetProcAddress(hLibrary,"affiche") ;
type4 constructeur = GetProcAddress(hLibrary,"constructeur") ;
type3 afficheInterface = (type3) GetProcAddress(hLibrary,"affiche2") ;
type5 affiche3 = (type5) GetProcAddress(hLibrary,"affiche3") ;
type1 affiche4 = (type1) GetProcAddress(hLibrary,"affiche4") ;
type1 affiche5 = (type1) GetProcAddress(hLibraryTest,"affiche") ;
type5 affiche6 = (type5) GetProcAddress(hLibraryTest,"affiche2") ;
type1 CInterface_constructeur = GetProcAddress(hLibraryInterface,"constructeur") ;
/*
int* var = (int*) GetProcAddress(hLibrary,"nDataProcessing") ;
printf("Appel constructeur DATA PROCESSING\n");
constructeur();
printf("Appel a la fonction affiche: %d\n", affiche());
printf("Appel au constructeur CInterface\n");
CInterface* interface1 = (CInterface*) CInterface_constructeur();
printf("Valeur du membre de la classe CInterface: %d\n",interface1->mMembre1);
//fn_affichageStructureInterface = GetProcAddress(hLibrary,"affiche2") ;
printf("Appel affiche4: %d\n",affiche4());
printf("Appel affiche3: %d\n",affiche3(12));
//int val = afficheInterface(interface1);
//printf("Valeur du membre de l'objet Interface: %d\n",val);
*/
printf("Appel affiche5: %d\n",affiche5());
printf("Appel affiche6: %d\n",affiche6(12));
return 0;
} |
Je charge la DLL creationDLL2
puis j'appelle deux fonctions affiche5 et affcihe6
lorsque j'appelle la fonction affiche5 qui ne prend pas de paramètre : OK
qd j'appelle la fonction affiche6 qui prend un paramètre : KO
Message:
The value of ESP was not properly saved accross a function call.
This is usally a result of calling function declared with one calling convention with a function pointer declared with a different calling convention.
Merci pour vos réponses.
Frédéric.