Archive: plugin hooking... one step to void the /NOUNLOAD


plugin hooking... one step to void the /NOUNLOAD


HINSTANCE hInst;
HMODULE hMod;
int i;
HHOOK hHook;
HOOKPROC hHookProc;

#define Function(name) extern "C" void __declspec(dllexport) name(HWND hwndParent, int string_size, char *variables, stack_t **stacktop)

Function(suma)
{
EXDLL_INIT();
setuservariable(0, int2str(++i));
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
hInst = hinstDLL;
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
{
i = 0;
hMod = LoadLibrary("nsDLL");
hHookProc = (HOOKPROC)GetProcAddress(hMod, "suma");
hHook = SetWindowsHookEx(WH_CBT, hHookProc, hMod, 0);
return TRUE;
}
case DLL_PROCESS_DETACH:
{
UnhookWindowsHookEx(hHook);
FreeLibrary(hMod);
return TRUE;
}
}
return TRUE;
}

Example:

Section
nsDLL::suma ; $0 1
nsDLL::suma; $0 2
nsDLL::suma ; $0 1
nsDLL::suma ; $0 2
DetailPrint $0 ; $0 2
SectionEnd

at the end, $0 returns 2, when it suppose to be 4.... what I'm missing? :igor:

-LoadLibrary in DllMain, safe?
-SetWindowsHookEx in DllMain, safe? (if I remember correctly only some of the functions in kernel32 may be used in DllMain)
-wrong function type for SetWindowsHookEx (suma returns void, hookproc returns LRESULT)
-does not call CallNextHookEx
-global hook needed?
-why is hHookProc a global variable?


so, any ideas how? :)


I have not tested this code and im not even sure that the dll is not unloaded right away

As I have told you before, the way I have done it is to call LoadLibrary on yourself each time one of the exported functions are called by nsis, the only problem with this is unloading, so you could subclass the nsis window and unload in WM_DESTROY or use a hook. Dro has also talked about api hooking FreeLibrary or something like that