Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ DLL hooking
i have written application for WH_SHELL and WH_CBT hooking,the problem with this application is that i'm able to hook window created messages but for window destroy messages my application just crashes,if i comment the window destroy code the application works fine,i have google it and found no solution.My ultimate concern is how can i handle window destroy message in this application.Posting the necessary code snippet below
this is my hooking dll
LRESULT CALLBACK HookProc (int nCode, WPARAM wParam, LPARAM lParam )
{		        
	if (nCode == HCBT_KEYSKIPPED && (lParam & 0x40000000))
	{        
		
	}
	else if (nCode == HCBT_SETFOCUS)
	{
		::PostMessage(g_hSpyWin, MSG_MY_WM_SETFOCUS, wParam, lParam);
	
	}
	else if(nCode==HSHELL_WINDOWCREATED)
	{
		::PostMessage(g_hSpyWin,MSG_MY_WM_ACTIVATE,wParam,lParam);
	}
	else if(nCode==HSHELL_WINDOWDESTROYED)
	{
		::PostMessage(g_hSpyWin,MSG_MY_WM_DESTROY,wParam,lParam);
	}
	return CallNextHookEx( 0, nCode, wParam, lParam);
}
this is my main function,here i call another function OnNewCreateWindow() which does nothing but writes information about created window into file.
LRESULT OnNewWindowCreate(WPARAM wParam,LPARAM lParam)
{
	
	thwnd=(HWND)wParam;
	OnNewCreateWindow1(thwnd);
	return S_OK;
}
 
LRESULT On_WindowDestroy(WPARAM wParam,LPARAM lParam)
{
	dHwnd=(HWND)wParam;
	MessageBox(NULL,"Into destroy window function","Destroy",MB_OK);
	//OnWindowDestroy1(dHwnd);
	return S_OK;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (message == MSG_MY_WM_KEYDOWN)
		return OnInterceptKeyStroke(wParam, lParam);
 
	if (message == MSG_MY_WM_SETFOCUS)
		return OnSetKeyboardFocus(wParam, lParam);
 
	if(message==MSG_MY_WM_ACTIVATE)
		return OnNewWindowCreate(wParam,lParam);
 
	if(message=MSG_MY_WM_DESTROY)
		return On_WindowDestroy(wParam,lParam);
		
 
	switch (message)
	{	
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
		
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
 
Here in the destroywindow function i have entered a messagebox() when i execute the application it just shows the messagebox twice and then exists the application.i dont know where i'm going wrong or what should i do more to handle the destroy window msg,same happens if i use
Posted 7-Jan-13 12:35pm
analogx653
Edited 7-Jan-13 12:40pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

HEY finally found a work around,instead of hooking HSHELL_WINDOWDESTROY message i just use the handle returned from HSHELL_WINDOWCREATED to get the process id using which i could get the handle to process,i stored the process id,handle to process in a singly link list and then wrote a checking function in thread which would check every 1000 ms that a process is terminated or not using the values process id and handle to process from link list,if its terminated i simply retrieve its End time and store it for the same entry in the link list.Hence i could get both the start and end time of the process...
I thought instead of pondering on solution why not get a work around...
Thank you guys...
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 395
1 Sergey Alexandrovich Kryukov 333
2 BillWoodruff 309
3 OriginalGriff 307
4 George Jonsson 275
0 OriginalGriff 5,095
1 CPallini 4,285
2 Sergey Alexandrovich Kryukov 3,767
3 George Jonsson 2,941
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100