Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for example Process A(MFC Process),uses loadlibrary to load a dll and install a global hook(the hook procedure is in the dll).
I want this dll to pass any information to the Process A, So I used SendMessage in the hook procedure to send WM_COPYDATA to Process A,but it didnt work.The Process A(have setted onCopyData()) didnt receive the message , also the same as PostThreadMessage().

//Hook Procedure
C++
__declspec(dllexport)  LRESULT  CALLBACK  mousehook_proc(int nCode,WPARAM wParam,LPARAM lParam)
{	
                        COPYDATASTRUCT cds;
                        MOUSEHOOKSTRUCT *pmhs = (MOUSEHOOKSTRUCT *)lParam;
			int result = ::SendMessage((HWND)FindWindow(NULL,L"MySpyM"),WM_COPYDATA,(WPARAM)pmhs->hwnd,(LPARAM)&cds);//The MySpyM Process("MySpyM" also the Window name) did not receive the WM_COPYDATA Message and if i replace the Message with WM_CLOSE,it received.......
			return ::CallNextHookEx((HHOOK)hook_handle,nCode,wParam,lParam);
	}


//The spy program porcess the WM_COPYDATA Message
C++
BOOL CMySpyMDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{

	MessageBox(L"Get Message",L"Alert",NULL);
		return CDialog::OnCopyData(pWnd, pCopyDataStruct);
}
Posted
Updated 6-Feb-13 12:43pm
v6
Comments
Richard MacCutchan 6-Feb-13 9:46am    
You need to provide more detail of your code, we cannot guess what is happening in your program.
Keanu L 6-Feb-13 10:40am    
I want another process whose thread is installed hook to send data to the spy process using WM_COPYDATA, but the spy process coudnt receive the message....
Richard MacCutchan 6-Feb-13 10:49am    
Have you traced this code to check that FindWindow() gets the correct window handle. Also how does MySpyM check for the WM_COPYDATA message? You need to show us all the information connected with this issue.
Keanu L 6-Feb-13 18:37pm    
i have checked FindWindow() and it got the correct handle.i compared the handle value with the value got in Visual Studio tool Spy++.The key point is that if i replace the message with WM_CLOSE,the spy process got the message but the WM_COPYDATA.
Sergey Alexandrovich Kryukov 6-Feb-13 12:47pm    
To start with, if you load a DLL using LoadLibrary, it is loaded in the calling process, not the child one.
This way, you share your address space with this DLL; and there is no problem.
Second thing: if if was not the case, exchange between the modules of different processes would be very difficult, as processes are well isolated.
Finally: you need to install a window hook in a separate DLL only if you need a global hook. Is it the case.

Probably you are mixing up processes and executable modules...
—SA

1 solution

I have maked a mistake. i set the COPYDATASTRUCT wrong value....
the cbData in the COPYDATASTRUCT must be set the size of the data the lpData point to.
 
Share this answer
 
v3
Comments
Richard MacCutchan 7-Feb-13 4:27am    
So, in fact the receiver was getting the message, it was just not handling it well.

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