CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2015
    Posts
    11

    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;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Sending Window message to a window created in the dll.

    Please, use CODE tags around code snippets. Otherwise you code is absolutely unreadable! See Announcement: Before you post....
    How and where from do you call the StartInfoThread function?
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2015
    Posts
    11

    Re: Sending Window message to a window created in the dll.

    Quote Originally Posted by VictorN View Post
    Please, use CODE tags around code snippets. Otherwise you code is absolutely unreadable! See Announcement: Before you post....
    How and where from do you call the StartInfoThread function?
    Hi Victor,
    Thanks for answering.
    I am calling StartInfoThread(exported function) from another application that does the dll injection to that window application.
    I also noticed that inside the hook proc function "msghook" the handle of the window inside the dll is NULL.

    I am just thinking maybe the handle "hMyWnd" is not declared as it should have been.

    Thanks.

  4. #4
    Join Date
    Jul 2015
    Posts
    11

    Re: Sending Window message to a window created in the dll.

    Code:

    pStartInfoThread StartInfoThread = (pStartInfoThread)GetProcAddress(hinst, "StartInfoThread");
    if (hinst) {
    if (setMyHook)
    {
    setMyHook(targetWnd, threadID);
    }
    }
    Sleep(1500);

    HANDLE thread = StartInfoThread();

    This is how i call it. How do i put code tags?

  5. #5
    Join Date
    Jul 2015
    Posts
    11

    Re: Sending Window message to a window created in the dll.

    Code:
    pStartInfoThread StartInfoThread = (pStartInfoThread)GetProcAddress(hinst, "StartInfoThread");
    if (hinst) {
    if (setMyHook)
    {
    setMyHook(targetWnd, threadID);
    }
    }
    Sleep(1500);
    
    HANDLE thread = StartInfoThread();

  6. #6
    Join Date
    Jul 2015
    Posts
    11

    Re: Sending Window message to a window created in the dll.

    I just put my code in tags. So it can be more readable. I see that inside msghook function the handle of the window that i created in the dll is not known...I believe they are on different threads but why it is NULL.The handle name is hm
    Code:
     #pragma data_seg("Shared")
    HHOOK hook = NULL; 
    HINSTANCE g_hInstDll = NULL;
    HWND hWndServer = NULL;
    HINSTANCE hinst;
    HWND       prnt_hWnd;            //Parent Window Handle
    HWND hMyWnd;
    HANDLE	hThread1 = NULL;
    #pragma data_seg()
    #pragma comment(linker, "/section:Shared,rws")
    
    void KillThread(HANDLE thread);
    HWND anotherWnd;
    
    
    extern "C" __declspec(dllexport) void minimizeWindow()
    {
    	if (hMyWnd) {
    		SendMessage(hMyWnd, WM_SYSCOMMAND, SC_MINIMIZE, NULL);
    	}
    } 
    
    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;
    	std::stringstream ss;
    	switch (data->message) {
    	
    	case WM_CLOSE:
    			if (!hMyWnd) {     //<-----THIS IS NULL 
    			//SendMessage(hMyWnd, WM_CLOSE, NULL, NULL);   //<-----NOT BEING SENT
    			MessageBoxA(NULL, "Handle is NUll", "NOTE", MB_OK);
    		}
    		else {
    			MessageBoxA(NULL, "Handle is NOT NUll", "NOTE", MB_OK);
    		}
    		break;
    	case WM_SIZE:
    		switch (data->wParam) {
    		case SIZE_MINIMIZED:
    			ss << hMyWnd;
    			MessageBoxA(NULL, ss.str().c_str(), "MyWindowHandle", MB_OK);
    			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) {
    		std::stringstream ss;
    		ss << hMyWnd;
    		MessageBoxA(NULL, ss.str().c_str(), "MyWindowHandle Entrance", MB_OK);
    		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;
    }

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Sending Window message to a window created in the dll.

    Quote Originally Posted by NirDemchog View Post
    Hi Victor,
    Thanks for answering.
    I am calling StartInfoThread(exported function) from another application that does the dll injection to that window application.
    I also noticed that inside the hook proc function "msghook" the handle of the window inside the dll is NULL.

    I am just thinking maybe the handle "hMyWnd" is not declared as it should have been.
    This handle was declared OK.
    But declaration is just a declaration.
    To use this handle you must first properly initialize it so it will correspond to some really existing window.
    If you create a window in another application but want to use it in this one then you will have to implement some IPC for these two applications.
    Victor Nijegorodov

  8. #8
    Join Date
    Jul 2015
    Posts
    11

    Re: Sending Window message to a window created in the dll.

    Thanks again for the answer.
    I wanted to be sure. When i have a dll that i will inject via the method of SetWindowsHookEx..Like this
    Code:
    hook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)msghook,  g_hInstDll,threadId);
    Does it mean that this code becomes part of the process where this threadId is part of? If i create another thread in the dll , will it be part of that process that i am hooked to ?
    Thanks in advance for the answer.

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Sending Window message to a window created in the dll.

    Quote Originally Posted by NirDemchog View Post
    Does it mean that this code becomes part of the process where this threadId is part of? If i create another thread in the dll , will it be part of that process that i am hooked to ?
    1) Yes, assuming that thread has a messagequeue associated with it.
    2) the thread you create "becomes part" of whatever process you created it in. thread creation is Always associated with the process that creates the thread. so 'it depends' where you call CreateThread. if you call it from the messagehook then yes, it's in the other process, if you call it from the process that does the SetWindowsHookEx, then it'll be in your 'hook installer' process.

    If you want to explicitely create a thread in another process, use CreateRemoteThread().

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Sending Window message to a window created in the dll.

    Quote Originally Posted by NirDemchog View Post
    I am just thinking maybe the handle "hMyWnd" is not declared as it should have been.
    Your thinking goes in right direction. There's one funny thing about uninitialized global variable: no matter what section it was put in with pragma, once it found uninitialized it goes into .bss section.
    Best regards,
    Igor

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
  •  





Click Here to Expand Forum to Full Width

Featured