1 pièce(s) jointe(s)
[DLL MFC] Plantage et débugage d'une DLL
Salut,
Je développe sous VS2008 à la création d'une DLL.
J'ai fais une dll basique que je n'arrive malheureusement pas à faire marcher -> explosion dans la dll avec des pointeurs incorrects ... :aie:
J'ai mis en piece jointe la solution VC++ 2008 qui contient 2 projets : la dll et le programme d'exemple.
Le débugage de l'ensemble fait exploser l'application dès que l'on arrive dans la fonction exportée.
Voici le code de la dll mfc (dll partagée) :
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
| // Test DLL.h*: fichier d'en-tête principal pour la DLL Test DLL
//
#pragma once
#ifndef __AFXWIN_H__
#error "incluez 'stdafx.h' avant d'inclure ce fichier pour PCH"
#endif
#include "resource.h" // symboles principaux
// CTestDLLApp
// Consultez Test DLL.cpp pour l'implémentation de cette classe
//
class CTestDLLApp : public CWinApp
{
public:
CTestDLLApp();
int TestFunc();
private:
CString m_chaine1;
CString m_chaine2;
// Substitutions
public:
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
}; |
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
| // Test DLL.cpp*: définit les fonctions d'initialisation pour la DLL.
//
#include "stdafx.h"
#include "Test DLL.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CTestDLLApp
BEGIN_MESSAGE_MAP(CTestDLLApp, CWinApp)
END_MESSAGE_MAP()
// construction CTestDLLApp
CTestDLLApp::CTestDLLApp()
{
// TODO*: ajoutez ici du code de construction,
// Placez toutes les initialisations significatives dans InitInstance
}
// Seul et unique objet CTestDLLApp
CTestDLLApp theApp;
// initialisation de CTestDLLApp
BOOL CTestDLLApp::InitInstance()
{
CWinApp::InitInstance();
m_chaine1 = "Toto";
m_chaine2 = CString("Robert");
return TRUE;
}
int CTestDLLApp::TestFunc()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
m_chaine1 = "pouet";
m_chaine2 = "blabla";
return 0;
} |
Le fichier DEF :
Code:
1 2 3 4 5 6 7
| ; Test DLL.def : déclare les paramètres de module pour la DLL.
LIBRARY "Test DLL"
EXPORTS
; Les exportations explicites peuvent être placées ici
TestFunc @1 |
Y'a t'il quelque chose d'incorrect dans ma dll ?
Voici en gros le code de l'appli test (dialog based) :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| typedef int (* TESTFUNC)();
// boîte de dialogue CApplitestDlg
class CApplitestDlg : public CDialog
{
// Construction
public:
CApplitestDlg(CWnd* pParent = NULL); // constructeur standard
TESTFUNC TestFunc; //Entête de la fonction
int InitDll(); //Initialisation de La dll
FARPROC GetProc(char *Fonction); //Récupération et affectation des méthodes de la dll
private:
HMODULE ModuleLib; //Module pour le chargement de la dll BT
...
}; |
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
| BOOL CApplitestDlg::OnInitDialog()
{
...
// TODO : ajoutez ici une initialisation supplémentaire
InitDll();
return TRUE; // retourne TRUE, sauf si vous avez défini le focus sur un contrôle
}
void CApplitestDlg::OnDestroy()
{
CDialog::OnDestroy();
FreeLibrary(ModuleLib);
}
//Méthode de chargement de la dll BT
int CApplitestDlg::InitDll()
{
ModuleLib = LoadLibrary( CString("Test DLL.dll") );
if(ModuleLib == NULL)
{
TCHAR chaine[100];
int Erreur = GetLastError();
wsprintf(chaine, _T("Problème de chargement de la dll !"), Erreur);
AfxMessageBox(chaine);
return -1;
}
TestFunc = (TESTFUNC) GetProc("TestFunc");
return 0;
}
//Méthode d'affectation des fonctions de la dll BT
FARPROC CApplitestDlg::GetProc(char *Fonction)
{
FARPROC fonc = GetProcAddress(ModuleLib, Fonction);
if(fonc == NULL)
{
char ErreurMessage[200] = "Fonction non trouvée !";
strcat(ErreurMessage, Fonction) ;
AfxMessageBox(CString(ErreurMessage));
}
return fonc;
}
void CApplitestDlg::OnBnClickedButton1()
{
int err = 0;
err = TestFunc();
} |
Je ne comprends vraiment pas ou se trouve le problème ! C'est on ne peut plus basique .... :roll:
J'en arrive à me demander si le fait de débugger une dll et son programme d'exemple ne fait pas exploser l'ensemble !
Merci !