Problem with Subclassing - causes a crash - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Problem with Subclassing - causes a crash

Hi All,
I am writing a plug-in for Outlook Express using gloabal hooks. Everytime you open a new mail window, I add a toolbar to the window. I am using a cbt hook and whenever the new mail window is created, I subclass the winow and in the new window proc I have code to create the toolbar. My problem is that outlook express is constatnly crashing. Sometimes it will work okay, but occasionally OE will crash when I close one of the new mail windows, or it will crash when I try to close OE. Any suggestions as to why this is happening?

Thanks

Below is the code:
////////////////////////////////////////////
// GLOBAL VARIABLES
#pragma data_seg(".OE")
static int g_bHookInit = TRUE;
static int g_nNum = 0;
HINSTANCE g_hInst = NULL;
HHOOK g_hookCBT = NULL;
#pragma data_seg()
#pragma comment(linker, "/section:.OE,rws")

#pragma bss_seg("Shared1")
HWND arrHwnd[10]; // Array to store handles
long lOldHndl[10] ; // Array to store old window handles
BOOL bIsSubClassed[10];
#pragma bss_seg()

BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD Reason, LPVOID Reserved)
{
switch(Reason)
{
case DLL_PROCESS_ATTACH: {
g_hInst = hInstance;
return TRUE;
}
case DLL_PROCESS_DETACH:
return TRUE;
}
return TRUE;
}

BOOL SetHook()
{
g_hookCBT = SetWindowsHookEx(WH_CBT, (HOOKPROC)HookCBT, g_hInst, 0);
if(g_hookCBT != NULL)
return TRUE;

return FALSE;
}

BOOL ClearHook()
{
return UnhookWindowsHookEx(g_hookCBT);
}

wParam, LPARAM lParam)
LRESULT CALLBACK HookCBT(UINT nCode, WPARAM wParam, LPARAM lParam)
{
HWND hWnd;
TCHAR szClass[1024];
int i = 0;
if(nCode < 0)
return CallNextHookEx(g_hookCBT, nCode, wParam, lParam);

switch(nCode)
{
//case HCBT_DESTROYWND:
case HCBT_CREATEWND:
hWnd = (HWND)wParam;
GetClassName(hWnd, szClass, sizeof(szClass) - 1);

// ATH_Note is the compose message window.
if(!strcmp(szClass, "ATH_Note"))
{
g_bHookInit = FALSE;
i = g_nNum;
arrHwnd[i] = hWnd;
lOldHndl[i] = SetWindowLong(hWnd, GWL_WNDPROC,(LONG)OE_NewMail_WndProc);

g_nNum = g_nNum + 1;
}
break;
}

return CallNextHookEx(g_hookCBT, nCode, wParam, lParam);
}

LRESULT CALLBACK OE_NewMail_WndProc(HWND hwnd, UINT uiMsg,WPARAM wParam, LPARAM lParam)
{
static int iTimerID = 0;

int i, nNdx = -1;
for (i = 0; i < g_nNum; i++)
{
if (arrHwnd[i] == hwnd)
{
nNdx = i;
break;
}
}

// A timer is set here explicitly so tht we are sure to get a timer message after the specified time.
if (iTimerID == 0)
iTimerID = SetTimer(hwnd, 1, 100, NULL);

switch (uiMsg)
{
case WM_CREATE:
iTimerID = 0; //Just setting flags kinda stuff for SetTimer.
break;
case WM_ENABLE:
break;
case WM_NCACTIVATE:
break;
case WM_TIMER:
// Kill the timer so tht u dont get any more messages.
KillTimer(hwnd, iTimerID);
if(!g_bHookInit)
{
REBARBANDINFO rbBand;
HWND hwndSizableRebar = FindWindowEx(hwnd, NULL, "SizableRebar", NULL);
HWND hwndReBarWindow32 = FindWindowEx(hwndSizableRebar, NULL, "ReBarWindow32", NULL);

//All this windows are part of REBARBAND so get the info by sending it a message.
//Sending message means sending message to the winproc of the class. Here its of rebarband.
rbBand.cbSize = sizeof(REBARBANDINFO);
rbBand.fMask = RBBIM_STYLE | RBBIM_CHILD | RBBIM_CHILDSIZE | RBBIM_SIZE;

if(hwndReBarWindow32) {
g_bHookInit = TRUE;
CreateToolbar(hwndReBarWindow32);
}
}
break;


}

return CallWindowProc((WNDPROC)lOldHndl[nNdx], hwnd, uiMsg, wParam, lParam);
}
Sign In or Register to comment.