Messages from another window in another thread

This is a discussion on Messages from another window in another thread within the Windows Programming forums, part of the Platform Specific Boards category; Howdy, Just a simple question, is there any way to get messages for a window created in a different thread ...

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Messages from another window in another thread

    Howdy,

    Just a simple question, is there any way to get messages for a window created in a different thread than my own?

    I want to use PeekMessage on Notepad. I tried using FindWindow(NULL, "untitled - Notepad"), which found the window perfectly, but was unable to use GetMessage or PeekMessage(). I think thats because in the MSDN help under GetMessage, it has this:
    hWnd

    [in] Handle to the window whose messages are to be retrieved. The window must belong to the calling thread. The NULL value has a special meaning[...]
    I expect the same is also true for PeekMessage, because I had the same problem there.

    Is it possible to check for messages at all from another thread?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,674
    Read up on the Win32 Hook functions.

    gg

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    #include <windows.h>
    #include <iostream>
    #include <string>
    #include <conio.h>
    
    const std::string strMod = "easy.dll";
    
    int main()
    {
    	HWND hWnd = FindWindow(NULL,"Calculator);
    	if(hWnd == 0)
    	{
    		std::cout << "Unable to find Calculator";
    		return 0;
    	}
    
    	DWORD dwProc;
    	
    	GetWindowThreadProcessId(hWnd,&dwProc);
    
    	HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwProc);
    	if(hProc == 0)
    	{
    		std::cout << "Unable to open process";
    		return 0;
    	}
    
    	HMODULE hKern = GetModuleHandle("kernel32.dll");
    	if(hKern == 0)
    	{
    		std::cout << "Unable to get Kernel32 Handle";
    		return 0;
    	}
    
    	WNDPROC lpfLoadLibraryA = reinterpret_cast<WNDPROC>
    		(GetProcAddress(hKern,"LoadLibraryA"));
    	if(lpfLoadLibraryA == 0)
    	{
    		std::cout << "Unable to get LoadLibrary Func";
    		return 0;
    	}
    
    	LPVOID lpMemAddress = VirtualAllocEx(hProc,0,strMod.length() + 1,
    		MEM_COMMIT,PAGE_READWRITE);
    	if(lpMemAddress == 0)
    	{
    		std::cout << "Unable to alloc memory";
    		return 0;
    	}	
    	
    	SIZE_T stBytesWritten;
    	if(!WriteProcessMemory(hProc,lpMemAddress, (LPVOID) strMod.c_str(),
    		strMod.length() + 1,&stBytesWritten))
    	{
    		std::cout << "Unable to write memory";
    		return 0;
    	}	 
    
    	DWORD dwThreadID;
    	HANDLE hRemThread = CreateRemoteThread(hProc,0,0,
    		reinterpret_cast<LPTHREAD_START_ROUTINE>(lpfLoadLibraryA),
    		lpMemAddress,0,&dwThreadID);
    	if(hRemThread == 0)
    	{
    		std::cout << "Unable to create thread";
    		return 0;
    	}
    
    	//This returns once the thread has a signaled state, ie, when it terminates
    	//The remote thread, LoadLibraryA, terminates when DLLEntryPoint returns
    	std::cout << "Waiting for single object" << std::endl;
    	WaitForSingleObject(hRemThread,INFINITE);
    	std::cout << "Thread terminated" << std::endl;
    	
    	getch();
    	return 0;
    }
    Code:
    #include <windows.h>
    
    BOOL WINAPI DllMain(HANDLE,DWORD,LPVOID);
    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
    
    char *chHookWindow="Calculator"; //window name of target window
    char *chHookClass=NULL; //class name of target window
    WNDPROC wpOriginal;
    UINT iMsg;
    
    //Any code executed here is in the context of the remotely created thread, which means any windows
    //will be destroyed when DllMain returns (because that is when LoadLibraryA terminates)
    BOOL WINAPI DllMain(HANDLE hinstDLL,DWORD dwReason,LPVOID lpReserved)
    {
    	switch (dwReason)
    	{
    	case DLL_PROCESS_ATTACH: //Dll is attaching to process
    		{
    			HWND hFind=NULL;
    			DWORD dwThisProcessId=GetCurrentProcessId();
    			DWORD dwFindWindowProcessId;
    
    			//Loop through every valid window until we get one in the same process
    			do
    			{
    				hFind=FindWindowEx(NULL, hFind, chHookClass, chHookWindow);
    				GetWindowThreadProcessId(hFind,&dwFindWindowProcessId);
    			} while ( (dwFindWindowProcessId!=dwThisProcessId) && (hFind) );
    
    			//Subclass
    			wpOriginal=(WNDPROC)SetWindowLong(hFind, GWL_WNDPROC, (LONG) WndProc);
    			
    			//Grab us a unique window message, so we can start doing things in the right context
    			iMsg=RegisterWindowMessage("HookProjectUniqueInitializationWindowMessage");
    			//Run the intialization code
    			PostMessage(hFind,iMsg,NULL,NULL);
    			break;
    		}
    	default:
    		break;
    	}
    	return TRUE;
    }
    
    //Any code executed here is in the context of the original thread
    LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
    	if (msg==iMsg)
    	{
    		//Initialization code
    		SetWindowText(hwnd,"Ben's Calculator");
    		return 0;
    	}
    
    	switch (msg)
    	{
    	case WM_CREATE:
    		break;
    	
    	case WM_COMMAND:
    		break;
    			
    	default:
    		break;
    	}
    	return CallWindowProc(wpOriginal,hwnd,msg,wParam,lParam);
    }
    Capeesh?
    Last edited by bennyandthejets; 10-22-2003 at 03:30 AM.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 10:06 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM
  5. Receiving messages from child of a child.
    By Sindol in forum Windows Programming
    Replies: 3
    Last Post: 01-26-2002, 06:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21