|
-
July 6th, 2015, 04:22 AM
#1
Sending Window message to a window created in the dll.
Hi guys,
I have created a windows inside a thread in dll. Inside this dll i have set up a hook to a window in another application. I want to copy the messages on that other window(in another process) to send the same messages to my window inside the dll. I am able to catch the message from window in another process. But i am not able to send messages to a window inside the injected dll. All help will be very much appreciated.
#include "stdafx.h"
#include <windows.h>
#define DLLAPI extern "C" __declspec(dllexport)
// shared variables
#pragma data_seg("Shared")
HHOOK hook = NULL;
HINSTANCE g_hInstDll = NULL;
HWND hWndServer = NULL;
HINSTANCE hinst;
HINSTANCE inj_hModule; //Injected Modules Handle
HWND prnt_hWnd; //Parent Window Handle
HWND hMyWnd;
HANDLE hThread1 = NULL;
#pragma data_seg()
#pragma comment(linker, "/section:Shared,rws")
#define MYMENU_EXIT (WM_APP + 101)
#define MYMENU_MESSAGEBOX (WM_APP + 102)
void KillThread(HANDLE thread);
HWND anotherWnd;
LRESULT CALLBACK msghook(UINT nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0)
{ /* pass it on */
CallNextHookEx(hook, nCode, wParam, lParam);
return 0;
} /* pass it on */
static DWORD dwTickKeep = 0;
if ((GetTickCount() - dwTickKeep)>200)
{
dwTickKeep = GetTickCount();
Beep(1500, 80);
}
CWPSTRUCT* data = (CWPSTRUCT*)lParam;
switch (data->message) {
case WM_CLOSE:
MessageBoxA(NULL, "WANNA CLOSE HA", "NOTE", MB_OK);
if (hThread1) {
SendMessage(hMyWnd, WM_CLOSE, NULL, NULL); //<-----NOT BEING SENT
}
break;
case WM_SIZE:
switch (data->wParam) {
case SIZE_MINIMIZED:
ShowWindow(hMyWnd, SW_HIDE); //<-----NOT BEING ABLE TO HIDE
//SendMessage(hMyWnd, WM_SYSCOMMAND, SC_MINIMIZE, NULL);
//MessageBoxA(NULL, "MINIMIZED", "NOTE", MB_OK); //<----Minimized window
break;
case SIZE_MAXIMIZED:
ShowWindow(hMyWnd, SW_HIDE);//<-----NOT BEING ABLE TO HIDE
break;
case SIZE_RESTORED:
ShowWindow(hMyWnd, SW_SHOWNORMAL);
// Do whatever
break;
}
default:
break;
}
return CallNextHookEx(hook, nCode, wParam, lParam);
} // msghook
extern "C" __declspec(dllexport) BOOL setMyHook(HWND hWnd, unsigned long threadId)
{
if (hWndServer != NULL)
return FALSE; // already hooked!
hook = SetWindowsHookEx(WH_CALLWNDPROC,
(HOOKPROC)msghook,
g_hInstDll,
threadId);
if (hook != NULL)
{ /* success */
hWndServer = hWnd;
return TRUE;
} /* success */
MessageBox(NULL, L"Could not set a Hook", L"Note", MB_OK);
return FALSE; // failed to set hook
} //
extern "C" __declspec(dllexport) BOOL clearMyHook(HWND hWnd)
{
if (hWnd != hWndServer || hWnd == NULL)
return FALSE;
BOOL unhooked = UnhookWindowsHookEx(hook);
if (unhooked)
hWndServer = NULL;
return unhooked;
} // clearMyHook
LRESULT CALLBACK DLLWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
break;
case WM_CLOSE:
MessageBox(NULL, L"Received close message", L"Note", MB_OK);
PostQuitMessage(0);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
BOOL RegisterDLLWindowClass(wchar_t szClassName[])
{
WNDCLASSEX wc;
wc.hInstance = hinst;
wc.lpszClassName = (LPCWSTR)szClassName;
wc.lpfnWndProc = DLLWindowProc;
wc.style = CS_DBLCLKS;
wc.cbSize = sizeof(WNDCLASSEX);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszMenuName = NULL;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
if (!RegisterClassEx(&wc))
return 0;
return TRUE;
}
DWORD WINAPI StartWindow(LPVOID lpParam)
{
MSG messages;
wchar_t *pString = reinterpret_cast<wchar_t * > (lpParam);
RegisterDLLWindowClass(L"InjectedDLLWindowClass");
prnt_hWnd = FindWindowA("SIMPLEAPP", "SimpleApp");
hMyWnd = CreateWindow(L"InjectedDLLWindowClass", pString, WS_EX_PALETTEWINDOW, 730, 50, 350, 350, NULL, NULL, hinst, NULL);
if (prnt_hWnd) {
ShowWindow(hMyWnd, SW_SHOWNORMAL);
}
while (GetMessage(&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return 1;
}
extern "C" __declspec(dllexport) HANDLE StartInfoThread(void)
{
hThread1 = CreateThread(0, NULL, (LPTHREAD_START_ROUTINE)&StartWindow, (LPVOID)L"Window Title", NULL, NULL);
return hThread1;
}
void KillThread(HANDLE thread)
{
if (thread) {
SendMessage(hMyWnd, WM_CLOSE, NULL, NULL);
WaitForSingleObject(thread, INFINITE);
CloseHandle(thread);
thread = NULL;
}
}
extern "C" __declspec(dllexport) void KillInfoThread(HANDLE thread)
{
KillThread(thread);
}
BOOL APIENTRY DllMain(HINSTANCE hInstance,
DWORD Reason,
LPVOID Reserved
)
{
switch (Reason)
{ /* reason */
case DLL_PROCESS_ATTACH:
g_hInstDll = hInstance;
hinst = g_hInstDll;
return TRUE;
break;
case DLL_PROCESS_DETACH:
if (g_hInstDll != NULL)
clearMyHook(hWndServer);
return TRUE;
break;
} /* reason */
return TRUE;
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|