indiocolifa
January 6th, 2011, 07:00 PM
I'm doing IPC between an EXE program and a DLL which is injected into another process via SetWindowsHookEx.
The problem is that I want to the DLL to unload when the main EXE quits, I'm doing that in the WM_CLOSE message:
LRESULT MsgClose(HWND hwnd)
{
dprintf(L"Closing.");
SetEvent(g_hQuitEvent);
WaitForSingleObject(g_thRegNotify, INFINITE);
CloseHandle(g_thRegNotify);
CloseHandle(g_hQuitEvent);
UnhookWinEvent(g_hEvtHook);
dprintf(L"Removing mailslot mappings\n");
for (std::map<DWORD,HANDLE>::iterator it = g_mailslotMap.begin();
it != g_mailslotMap.end(); ++it)
{
CloseHandle(it->second);
}
dprintf(L"Unhooking processes...\n");
for (std::set<HHOOK>::iterator it = g_hHookSet.begin();
it != g_hHookSet.end(); ++it)
{
UnhookWindowsHookEx(*it);
}
if (g_hLib32)
FreeLibrary(g_hLib32);
The DLL hook contains a thread that listens for two evetns: mailslot messages and the quit event with WaitForMultipleObjects:
do
{
OVERLAPPED ov;
ZeroMemory(&ov,sizeof(ov));
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
ov.hEvent = hEvent;
if (!ReadFile(g_hMailSlot, &ipcMsg, sizeof(IPCMESSAGE), 0, &ov))
{
if (GetLastError() != ERROR_IO_PENDING)
{
dprintf(L" ReadFile FAILED with error %d\n",GetLastError());
bContinue = FALSE;
rv = GetLastError();
}
}
HANDLE waitEvents[2] = { hEvent, g_hQuitEvent };
DWORD wo = WaitForMultipleObjects(2, waitEvents, FALSE, INFINITE);
if (wo - WAIT_OBJECT_0 == 1)
{
dprintf(L" (mailslot thread) Quit event signaled.\n");
bContinue = FALSE;
}
else
{
// Process mailslot message
...
...
...
At DLL exit I perform:
if (g_hMailSlot && g_hMailSlotThread)
{
SetEvent(&g_hQuitEvent);
WaitForSingleObject(g_hMailSlotThread, INFINITE);
CloseHandle(g_hMailSlotThread);
}
dprintf(L" Mailslot thread terminated.\n");
If I close the process that the DLL resides in, the g_hQuitEvent is signalled properly. But not when hooking is removed via closing the main EXE.
Any idea as where I can look? The thread awaits at the WaitForSingleObject line marked above, halting the hosting EXE.
Thanks in advance.
The problem is that I want to the DLL to unload when the main EXE quits, I'm doing that in the WM_CLOSE message:
LRESULT MsgClose(HWND hwnd)
{
dprintf(L"Closing.");
SetEvent(g_hQuitEvent);
WaitForSingleObject(g_thRegNotify, INFINITE);
CloseHandle(g_thRegNotify);
CloseHandle(g_hQuitEvent);
UnhookWinEvent(g_hEvtHook);
dprintf(L"Removing mailslot mappings\n");
for (std::map<DWORD,HANDLE>::iterator it = g_mailslotMap.begin();
it != g_mailslotMap.end(); ++it)
{
CloseHandle(it->second);
}
dprintf(L"Unhooking processes...\n");
for (std::set<HHOOK>::iterator it = g_hHookSet.begin();
it != g_hHookSet.end(); ++it)
{
UnhookWindowsHookEx(*it);
}
if (g_hLib32)
FreeLibrary(g_hLib32);
The DLL hook contains a thread that listens for two evetns: mailslot messages and the quit event with WaitForMultipleObjects:
do
{
OVERLAPPED ov;
ZeroMemory(&ov,sizeof(ov));
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
ov.hEvent = hEvent;
if (!ReadFile(g_hMailSlot, &ipcMsg, sizeof(IPCMESSAGE), 0, &ov))
{
if (GetLastError() != ERROR_IO_PENDING)
{
dprintf(L" ReadFile FAILED with error %d\n",GetLastError());
bContinue = FALSE;
rv = GetLastError();
}
}
HANDLE waitEvents[2] = { hEvent, g_hQuitEvent };
DWORD wo = WaitForMultipleObjects(2, waitEvents, FALSE, INFINITE);
if (wo - WAIT_OBJECT_0 == 1)
{
dprintf(L" (mailslot thread) Quit event signaled.\n");
bContinue = FALSE;
}
else
{
// Process mailslot message
...
...
...
At DLL exit I perform:
if (g_hMailSlot && g_hMailSlotThread)
{
SetEvent(&g_hQuitEvent);
WaitForSingleObject(g_hMailSlotThread, INFINITE);
CloseHandle(g_hMailSlotThread);
}
dprintf(L" Mailslot thread terminated.\n");
If I close the process that the DLL resides in, the g_hQuitEvent is signalled properly. But not when hooking is removed via closing the main EXE.
Any idea as where I can look? The thread awaits at the WaitForSingleObject line marked above, halting the hosting EXE.
Thanks in advance.