Bonjour à tous,
Cela fait maintenant deux jours que je galère pour pouvoir acceder à une fonction mise dans une dll.
J'ai dabors créé ma dll. Dans mon projet j'ai:
Le fichier CoursDll.cpp:
#include <windows.h>
#include "CoursDll.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;
}
extern "C" void WINAPI Initialize()
{
// Write here all the code you need to initialize the DLL
}
extern "C" void WINAPI Release()
{
// Write here all the code you need to free everything ...
}
extern "C" void WINAPI TestFunction()
{
//MessageBox(0,"DLL","",0);
}
Un fichier CoursDll.h:
#ifndef __API_TESTDLL_H
#define __API_TESTDLL_H
#if defined (__cplusplus)
extern "C" {
#endif
// Main API functions declaration
typedef void (WINAPI *DLL_Function_Initialize) ();
typedef void (WINAPI *DLL_Function_Release) ();
typedef void (WINAPI *DLL_Function_TestFunction) ();
#if defined (__cplusplus)
}
#endif
#endif __API_TESTDLL_H
et un fichier CoursDll.Def:
; TestDLL.def : Declares the module parameters for the application
LIBRARY CoursDLL
DESCRIPTION 'Example of DLL'
EXPORTS
; Explicitly exported initialization routine
Initialize @1
Release @2
TestFunction @3
La compilation se passe sans problème.
Esnuite j'ai créér un second projet pour l'executable contenant:
Le fichier main.cpp:
#include <windows.h>
typedef void (WINAPI *DLL_Function_TestFunction)();
DLL_Function_TestFunction pfn_TestFunction;
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HMODULE hDLL;
hDLL = LoadLibrary("CoursDll.dll");
if (hDLL == NULL){
MessageBox(0,"Dll Introuvable","Erreur",0);
}else{
pfn_TestFunction = (DLL_Function_TestFunction)GetProcAddress(hDLL,"TestFunction");
pfn_TestFunction();
FreeLibrary(hDLL);
}
return 0;
}
La compilation ce deroule aussi sans problème.
Ensuite je copie la dll dans le dossier Debug du projet de l'executable.
Lorsque je démarre l'application ca me fait a chaque fois "Exception non gérée à 0x00000000 dans FenetreBase.exe:0xC0000005: Violation d'accès lors de la lecture de l'emplacement 0x00000000."
J'ai beau chercher je ne trouve pas d'où cela provient...
En ésperant que quelqu'un voit de quoi il sagit, bonne journée/soirée/nuit à tous!
++
ChrOnOs