Click here to Skip to main content
11,718,016 members (81,532 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Win32 C++/MFC
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
__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
BOOL CMySpyMDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
 
	MessageBox(L"Get Message",L"Alert",NULL);
		return CDialog::OnCopyData(pWnd, pCopyDataStruct);
}
Posted 6-Feb-13 1:37am
Edited 6-Feb-13 12:43pm
v6
Comments
Richard MacCutchan at 6-Feb-13 9:46am
   
You need to provide more detail of your code, we cannot guess what is happening in your program.
Lygatto Zenki at 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 at 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.
Lygatto Zenki at 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 at 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
Lygatto Zenki at 6-Feb-13 18:41pm
   
..indeed. my spy process has installed a global hook,so the another process sending message will load the dll.
Sergey Alexandrovich Kryukov at 6-Feb-13 18:45pm
   
I don't get it. OK, if you need a global hook, install it; and you really will need a separate DLL. But it's loaded in one process, where is the other? Do you mean the processes which invoke the hooks? Then, this is the whole idea: your process has nothing to do with them, it only gets hook events...
So, what's the problem? It all depends on what do you want to do with the hook event data.
—SA
Lygatto Zenki at 6-Feb-13 19:08pm
   
I just want the hook dll passed the hook event data to the spy process(not the sending message process) using WM_COPYDATA.
Now i have solved it. the cbData in the COPYDATASTRUCT must be set the size of the data the lpData point to.

Thank you very much~:-)
Sergey Alexandrovich Kryukov at 6-Feb-13 19:11pm
   
Congratulations! You are welcome.
—SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v3
Comments
Richard MacCutchan at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 874
1 OriginalGriff 215
2 F-ES Sitecore 210
3 Maciej Los 140
4 Jochen Arndt 120
0 Sergey Alexandrovich Kryukov 1,649
1 OriginalGriff 535
2 Maciej Los 529
3 Richard MacCutchan 440
4 CHill60 415


Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100