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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include "ChargerMYDLL.h"
IMPLEMENT_APP( myApp )
//------------------------------------------------------------------------------
// Implémentation de la méthode OnExit.
int myApp :: OnExit( )
{
return 0 ;
}
//------------------------------------------------------------------------------
// Implémentation de la méthode OnInit..
bool myApp :: OnInit( )
{
//Création de la fenêtre principale.
cl_Frame *pFrame = new cl_Frame( ) ;
pFrame -> Centre( wxBOTH ) ; //Affichage de la fenêtre au centre de l'écran
pFrame -> Show( true ) ; //On fait en sorte que la fenêtre soit visible.
return true ; //true pour que le programme ne se termine pas immédiatement.
}
//------------------------------------------------------------------------------
// Constructeur.
cl_Frame :: cl_Frame( )
: wxFrame( NULL, wxID_ANY, ( "ChargementDLL" ), wxDefaultPosition, wxSize( 600, 300 ),
wxDEFAULT_FRAME_STYLE | wxCLIP_CHILDREN | wxFULL_REPAINT_ON_RESIZE )
{
//Création de l'objet pPanel global.
pPanel = new wxPanel( this, -1 ) ;
pPanel -> SetBackgroundColour( *wxGREEN ) ;
//Affichage de pBtChargerDLL sur le panel.
pBtChargerDLL = new wxButton( pPanel, -1, "Charger la DLL.", wxPoint( 20, 60 ), wxDefaultSize ) ;
wxString nomDLL = "./MyDll/bin/Release/MyDll.dll" ;
pBtChargerDLL -> Bind( wxEVT_COMMAND_BUTTON_CLICKED, [ nomDLL, this ] ( wxCommandEvent &event ) { cl_Frame :: ChargerDLL( nomDLL ) ; } ) ;
// Interception de la fermeture de la fenêtre par la crois ( X )
this -> Bind( wxEVT_CLOSE_WINDOW, & cl_Frame :: FermerParX, this ) ;
}
//------------------------------------------------------------------------------
// Destructeur.
cl_Frame :: ~cl_Frame( )
{
//Destruction de...
}
//------------------------------------------------------------------------------
// Fermeture de la DLL si celle-ci existe avant la fermeture de la fenêtre.
void cl_Frame :: FermerParX( wxCloseEvent &event )
{
if ( pFonctionDLL != NULL ) // C'est à dire que la DLL est chargée.
{
cl_Frame :: FermerDLL( ) ;
}
Destroy( ) ;
}
// Fermer la DLL.
void cl_Frame :: FermerDLL( )
{
if ( pDynLib -> IsLoaded( ) )
{
pFonctionDLL = ( pNomDLL(*)( ) ) pDynLib -> GetSymbol( "pDestroyDLL" ) ;
if( pFonctionDLL == NULL )
{
wxMessageBox( "Fonction non chargée" ) ;
exit( 0 ) ;
}
pFonctionDLL( ) ; // Affiche la fonction pDestroyDLL.
pBtFermerDLL -> Destroy( ) ; // Détruit le bouton, on en a plus besoin.
pFonctionDLL = NULL ; // FonctionDLL non chargée.
}
}
//-------------------------------------------------------------------------
// Chargement de la DLL.
void cl_Frame::ChargerDLL( wxString nomDLL )
{
pDynLib = new wxDynamicLibrary ( ) ;
//Déclaration d'un pointeur pDynLib sur la classe wx.
pDynLib -> Load( nomDLL ) ;
if ( ! pDynLib -> IsLoaded( ) )
{
wxMessageBox( "Erreur de chargement de la DLL" ) ;
exit( 0 ) ;
}
else
{
if ( pFonctionDLL == NULL )
{
pFonctionDLL = ( pNomDLL(*)( ) ) pDynLib -> GetSymbol( "pCreateDLL" ) ;
if ( pFonctionDLL == NULL )
{
wxMessageBox( "Fonction non chargée" ) ;
exit( 0 ) ;
}
pFonctionDLL( ) ; // Affiche la fonction.
// Création d'un bouton pour fermer correctement la DLL.
pBtFermerDLL = new wxButton( pPanel, -1, " Fermer la DLL .", wxPoint( 20, 100 ), wxDefaultSize ) ;
pBtFermerDLL -> Bind( wxEVT_COMMAND_BUTTON_CLICKED, [ this ] ( wxCommandEvent &event ) { cl_Frame :: FermerDLL( ) ; } ) ;
}
}
} |
Partager