Créer une DLL et l'exploiter
Bonjour,
ça fait bien une heure que je galère :?
J'ai créé une DLL, et je souhaite pour le moment faire un truc basique, à savoir afficher une messageBox sur appel du processus qui la rattache.
voici les sources:
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
| // dllmain.cpp : Définit le point d'entrée pour l'application DLL.
#include "stdafx.h"
extern "C" __declspec(dllexport) int WINAPI SayHello();
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
SayHello();
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
extern "C" {
__declspec(dllexport) int WINAPI SayHello()
{
MessageBox(NULL, L"Hello", L"DLL_Hello", MB_OK);
return 0;
}
} |
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
| // test.cpp*: définit le point d'entrée pour l'application.
//
#include "stdafx.h"
#include "test.h"
#define DLL_NAME TEXT("Hook.dll")
typedef int (WINAPI *PHookSayHello)(void);
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
HMODULE hDll = LoadLibrary(DLL_NAME);
PHookSayHello pHook;
FARPROC fp = GetProcAddress(hDll, "SayHello");
pHook = (PHookSayHello) fp;
if(NULL != pHook)
pHook();
else
printf ("Not found\n");
FreeLibrary(hDll);
return 0;
} |
Le problème est le suivant:
Au chargement de la DLL, j'ai bien ma MessageBox (la DLL est correctement chargée) , mais le GetProcAdress me retourne toujours 0, comme si la fonction n'existait pas...
Quelqu'un saurait pourquoi? :D