Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello to all,
I have the following code in an DLL that I tried to inject it:
C++
#include <windows.h>
#include <InjectCode.h>

static struct SubClassInfo subClassInfo;

BOOL WINAPI	DllMain(HINSTANCE hInst, DWORD reason, LPVOID reserved){
	BOOL returnVal=TRUE;
	
	switch(reason){
		case DLL_PROCESS_ATTACH:
			subClassInfo.windowOfInterest=findApplicationWindow(&subClassInfo);
			if(subClassInfo.windowOfInterest){
				if(!(SubclassAppWindow(&subClassInfo, SubclassProc))){
					returnVal=FALSE;
				}
			}
			else{	/* subClassInfo.windowOfInterest */
				returnVal=FALSE;
			}

			break;
		case DLL_PROCESS_DETACH:
			if(subClassInfo.windowIsSubclassed){
				SubclassAppWindow(&subClassInfo, subClassInfo.originalWndProc);
			}
			
			break;
		default:

			break;
	}

	return returnVal;
}

HWND findApplicationWindow(struct SubClassInfo *scInfo)
{
	BOOL result;
	
	scInfo->windowOfInterest=(HWND)0;
	scInfo->windowThread=GetCurrentThreadId();
	//HWND hwnd = GetHwndFromPID(GetCurrentProcessId());

	result=EnumThreadWindows(scInfo->windowThread,
							 (WNDENUMPROC)enumWindowsCallback,
							 (LPARAM)scInfo);
	
	return scInfo->windowOfInterest;
}

BOOL CALLBACK enumWindowsCallback(HWND aWnd, LPARAM windowThreadInfo){
	DWORD windowThreadId;
	struct SubClassInfo *sci;
	
	sci=(struct SubClassInfo *)windowThreadInfo;
	
	windowThreadId=GetWindowThreadProcessId(aWnd, (LPDWORD)0);
	
	if (windowThreadId == (DWORD)sci->windowThread) {
		sci->windowOfInterest=aWnd;

		/* we found our window.  stop looking */
		return(FALSE);
	}

	/* this isn't our window.  continue looking */
	return(TRUE);
}

LRESULT CALLBACK SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	TCHAR charCode;
	
	switch(uMsg)
	{
		case WM_KEYDOWN:
			charCode=(TCHAR)wParam;
			break;

		default:
			break;

	}

	return CallWindowProc(subClassInfo.originalWndProc,
						  subClassInfo.windowOfInterest,
						  uMsg,
						  wParam,
						  lParam);
}

BOOL SubclassAppWindow(struct SubClassInfo *sci, WNDPROC newProc)
{
    BOOL returnVal= 1;
	long result;

	result=SetWindowLong(sci->windowOfInterest, GWL_WNDPROC, (LONG) newProc);
	if(result){
		sci->originalWndProc=(WNDPROC)result;
		sci->windowIsSubclassed=TRUE;
	}
	else{
		returnVal= 0;
	}
	
	return returnVal;
}

And here is InjectCode.h:
C++
#ifdef __cplusplus
#define EXPORT extern "C" __declspec (dllexport)
#else
#define EXPORT __declspec (dllexport)
#endif


struct SubClassInfo
{
	HWND windowOfInterest;
	bool windowIsSubclassed;
	WNDPROC originalWndProc;
	DWORD windowThread;

};


LRESULT CALLBACK SubclassProc (HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK enumWindowsCallback(HWND, LPARAM);
HWND findApplicationWindow(SubClassInfo*);
BOOL SubclassAppWindow(SubClassInfo*, WNDPROC);

When I toggle a breakpoint in the enumWindowsCallback function, control doesn't pass to it. What's the problem? I use Windows 7 and VS2008.
Could you advise me please?
Posted
Comments
Richard MacCutchan 17-Dec-14 6:45am    
What result do you get from the call to EnumThreadWindows?
Raheleh14 20-Dec-14 5:33am    
It always returns true and GetLastError() return zero.
Richard MacCutchan 20-Dec-14 7:44am    
I can only assume that there are no windows to enumerate. But your code is a bit difficult to understand, and I am not even sure that you should be making those calls in DllMain.
[no name] 17-Dec-14 14:41pm    
Are there any windows for the calling thread at the time the DLL is loaded?
Raheleh14 20-Dec-14 5:36am    
Yes. I create a thread for an existing window that it loads this DLL.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900