IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

MFC Discussion :

Utiliser fonction (avec des paramètres) définie ds une DLL


Sujet :

MFC

  1. #1
    Candidat au Club
    Inscrit en
    Décembre 2004
    Messages
    5
    Détails du profil
    Informations forums :
    Inscription : Décembre 2004
    Messages : 5
    Points : 4
    Points
    4
    Par défaut 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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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.

  2. #2
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Points : 16 075
    Points
    16 075
    Par défaut
    t'as compilé avec l'option _declspec ?

  3. #3
    Membre éclairé
    Avatar de matazz
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    471
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 471
    Points : 668
    Points
    668
    Par défaut
    je pense (je me trompe peut-être) que ton problème vient du fait que tu fait un loadlibrairy.

    Tu peut pas inclure le .lib et le .h de ta DLL dans ton workspace ???
    Qui va piano va sano...

  4. #4
    Candidat au Club
    Inscrit en
    Décembre 2004
    Messages
    5
    Détails du profil
    Informations forums :
    Inscription : Décembre 2004
    Messages : 5
    Points : 4
    Points
    4
    Par défaut
    Oui mais pourquoi lorsque je n'ai pas de paramètre ça marche ?

  5. #5
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Points : 16 075
    Points
    16 075
    Par défaut
    loadlibrary permet d'utiliser des paramètres, à mon avis ca ne vient pas de ca.
    As-tu renseigné correctement les conventions d'appel ? dans le compilo et sur les fonctions ? c'est une dll qui va etre appellé par quel autre type de programme ?

  6. #6
    Candidat au Club
    Inscrit en
    Décembre 2004
    Messages
    5
    Détails du profil
    Informations forums :
    Inscription : Décembre 2004
    Messages : 5
    Points : 4
    Points
    4
    Par défaut
    La DLL est appele par un programme developpé sous Visual C++.
    Effectivement je pense que le pb vient de la configuration des options de compilations.

  7. #7
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Points : 16 075
    Points
    16 075
    Par défaut
    et bien, n'hésite pas à les changer

    project-->settings-->c/c++-->code generation-->calling convention

    Reste en congruence avec tes prototypes de fonction

Discussions similaires

  1. Appel fonction avec des paramètres se trouvant dans des cellules
    Par SI-SHARE dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 24/04/2013, 10h02
  2. Réponses: 2
    Dernier message: 25/11/2012, 18h14
  3. Vue sur une fonction avec des paramètres
    Par The F0x dans le forum PL/SQL
    Réponses: 1
    Dernier message: 04/04/2012, 17h00
  4. Réponses: 5
    Dernier message: 28/04/2006, 14h40
  5. Erreur sur une fonction avec des paramètres
    Par Elois dans le forum PostgreSQL
    Réponses: 2
    Dernier message: 05/05/2004, 21h00

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo