non-persistent DllMain

Sam Liddicott
Fri Oct 3 11:02:00 GMT 2008

I'm trying to use the Win32::GuiTest perl module on WinXP SP3.
I installed it under cygwin with something like:

perl -MCPAN -e 'install Win32::GuiTest'

However certain use is failing on the SetWindowHookEx because it is
passing a NULL hModule, which is surprising because dllmain shows that
hModule is saved in g_hDLL - except it's not working like that.

BOOL APIENTRY DllMain(HANDLE hModule, DWORD  ul_reason_for_call,
                      LPVOID lpReserved)
        switch (ul_reason_for_call)
        case DLL_PROCESS_ATTACH:
                // Value used by SetWindowsHookEx, etc.
                g_hDLL = (HINSTANCE)hModule;
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:

        return TRUE;

g_hDLL is always 0.

If I put in DllMain
if (! hModule) return FALSE;
then the load does NOT abort like a plain return FALSE, so I know
hModule is passed in as non-zero

Further debugging shows the following:

return FALSE; aborts the program - the only debugging tool that works,

setting global variables in DllMain has no effect, when examined later
in the DLL, they have their default initialized values. Even variables
that don't have a specific initialized don't retain any value set during

calling functions like fprintf, fopen have no effect in DllMain, not
even strace shows up their attempted use. Not even setenv does anything
in DllMain.

So, in short, DllMain doesn't save the hModule because it can't save
global variables, in fact it seems so ephemeral that the only thing it
CAN do is: return FALSE or return TRUE.

Anyone recognize this problem or have suggestions on what to do?


Unsubscribe info:
Problem reports:

More information about the Cygwin mailing list