Click to See Complete Forum and Search --> : SendMessage freezes other apps


graphix
October 18th, 2008, 06:37 PM
Hello,

I'm trying to control another software from my software. Both of them are written in managed code but I've posted here since is a win32 problem.
The scenario at the time X (when the problem is spotted) is the following:
- the other software has two windows
- the main window is the parent and the second window is a modal dialog with OK and Cancel button
- my software is trying to click OK button from that dialog in order for the main window to become active


SendMessage((int)buttonOkHandle, BM_CLICK, 0, IntPtr.Zero);


The dialog window receives the message and closes but the parent window freezes after this. Do I have to send activation messages to the main window? I've tried wm_activate with no success.

Thank you

S_M_A
October 19th, 2008, 04:55 AM
Try PostMessage instead of SendMessage

graphix
October 19th, 2008, 11:58 AM
Thanks for your reply.
Unfortunately I'm getting the same behavior using PostMessage

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int PostMessage(int hWnd, int msg, IntPtr wParam, IntPtr lParam);

S_M_A
October 19th, 2008, 12:53 PM
Hm, well at least it was a quick test. I presume that it all works if you click the OK button manually? Can you reduce your code enough to post it here?

graphix
October 19th, 2008, 04:23 PM
Yes it works when you press ok.
Well I'm using the ManagedSpy library to get the managed windows:
http://msdn.microsoft.com/en-us/magazine/cc163617.aspx
Then I'm using the sendMessage method that is attached to ControlProxy class from the library. It takes the same parameters and behaves the same as the user32's SendMessage.


ControlProxy[] topWindows = ControlProxy.TopLevelWindows;
foreach (ControlProxy cproxy in topWindows)
{
//select the window that I'm interested on
ControlProxy button = cp.Children[1]; // get the OK button
button.SendMessage(BM_CLICK, IntPtr.Zero, IntPtr.Zero);

}


I suppose the ControlProxy.TopLevelWindows is using FindWindow and GetWindow or similar winAPI methods.
I'm repeating this procedure once a minute and I see that the other software unfreezes when I'm running again ControlProxy.TopLevelWindows property to get the managed windows.
Also ControlProxy.TopLevelWindows thorows some nullPointerExceptions.

LE this would be the GetTopLevelWindows method:

array<ControlProxy^>^ Desktop::GetTopLevelWindows() {
_ASSERTE(topLevelWindows->Count==0);

EnumWindows((WNDENUMPROC)EnumCallback, (LPARAM)0);

array<ControlProxy^>^ winarr = gcnew array<ControlProxy^>(topLevelWindows->Count);
topLevelWindows->CopyTo(winarr);
topLevelWindows->Clear();
return winarr;
}