Hello to all,
I have the following code in an DLL that I tried to inject it:
#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{
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();
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;
return(FALSE);
}
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:
#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?