I'm loading 3rd party DLLs and sometimes they open MessageBox windows that stops the flow of the application, I want to be able to detect when such thing happens.
I'm trying to use SetWindowsHookEx with WH_CBT but my callback does not get called.
I tried calling it this way:
SetWindowsHookEx (WH_CBT, myCallback, NULL, 0);
But it didn't worked.
What should be the right call for this function?
You may use the WM_ENTERIDLE message to detect if there's any MessageBox window, simply check with FindWindowEx().
This is C# but idea doesn't change:
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/d3f89686-e4d0-4bb1-9052-31abef2a9d2a
Related
I want to prevent users from closing a window by Alt + F4 or by clicking the close button.
How to achieve this?
I guess the windows API can do it, but I don't have any experience, and I can't find a specific solution.
Of course, it's good to be able to implement it,don't have to use a specific API.
Background: it is very difficult to find the last place in Word after closing it for a few days. After word2013, word2013 brought with it a way to return to the previous reading position, but that thing is very unstable and often can't be saved. When word is closed, I want to stop closing and pop up a notice to remind me to add a bookmark before exiting.
EDIT: This won't work, as it turned out. At least the message hook won't work because the message is posted and not sent, and about the CBT hook I'm not sure either, and I can't test it at the moment to give an evidence-based statement. The solution is probably to subclass the window but this is also non-trivial and I can't explain it properly and with working examples right now. I can't delete this answer though because it already has a comment. See here for more info. So take it with a grain of salt. I'm turning the answer to community wiki, feel free to edit it and fix/improve the solution!
EDIT2: Seems even subclassing won't be enough because Word is doing things its own way.
You need a windows hook. Either a CBT hook or a getmessage hook will do.
You have to create a DLL for this to work. The hook handler must be located in the DLL. It must have the same bitness as Word (probably 64 Bit). Then you call SetWindowsHookEx to install a global hook.
In the hook, you will have to check whether the current action is a window-closing attempt (in a CBT hook you would check for a HCBT_SYSCOMMAND of SC_CLOSE, in a getmessage hook you would check for a WM_CLOSE message), and whether it is about a Word window (for example using the window class - not sure if it has a recognizable class, you'd have to check - or the process' executable file name which you can get using GetModuleFileName since you will run inside Word's process) and prevent the action (by returning 1 from a CBT hook or returning 0 from a getmessage hook - to allow, call CallNextHookEx).
I'm creating an ActiveX using VS and MFC. Initialisation is done in constructor of my control. There the m_hWND window handle is not valid, it is still NULL.
So: is there some kind of "initialisation complete" function available in COleControl() that is called as soon as m_hWnd is valid and additional controls can be added to it?
Thanks!
It's been a while since I've written an ActiveX control, but, I seem to remember using OnResetState, or, one of the other events shown in the link to handle initialization.
Remember that there are windowless ActiveX controls. For such controls you never get a window handle!
So normally after the creation a ActiveX Control is "initialized", and you can use it.
ActiveX controls usually reside in the same Thread so there is nothing that can run beside your code and do further initialization.
If this doesn't help you Need to refine your question.
See discussion here too about ActiveX controls without a window handle.
I think OnActivate is what you are looking for.
Our application loads 3rd party DLLs that sometimes pops MessageBoxes.
We've notices that sometimes we can't just use exit(0) when there's an open MessageBox.
How can we still force an exist in such a case?
Thanks
Seems like your ugly DLL calls MessageBox (or whatever) from within DLL_THREAD_DETACH or DLL_PROCESS_DETACH.
If this happens in the same thread (i.e. the thread the calls exit) you may try to call PostQuitMessage right before the application exit. This should abort any message loop. If this happens in another thread - you may call PostThreadMessage.
There's also an option to intercept calls to Win32 API functions by hooking the appropriate module (Exe/Dll) import table. Invented by J.Richter (if I remember correctly). http://www.player.idv.tw/prog/index.php/APIHook
If you want to use brute force you may call the TerminateProcess() function. I'm not sure this is a good idea, you may want to detect if there's an open message box and send a close to it (for example using a FindWindow())
Take a look at MSDN.
My application is loading 3rd party DLLs, and some of this DLLs open MessageBox windows.
Is there a way for me to detect when such a window was being opened?
You'll need a CBT hook to receive a notification when a MessageBox window is displayed. You install this by calling the SetWindowsHookEx() function and specifying WH_CBT for the hook ID parameter. The hook callback function will provide you a handle to the MessageBox window, which you can then use to close it.
If you know exactly when to expect the MessageBox is being created and shown, then you can adopt a lighter and simpler approach than a global hook. This would involve calling the FindWindowEx function to get a handle to the MessageBox window, which you can likewise use to close it.
Of course, to close it properly, you will need to know which type of MessageBox it is (that is, which of the standard button choices that it displays) so that you can simulate a click of the desired button. Simply managing to get the MessageBox to disappear off of the screen without providing a valid answer is highly likely to have unexpected results in terms of the library code.
It bears explicit mention that this is really bad behavior on the part of a DLL. Unless absolutely necessary, ditch whatever 3rd party that is foisting such poorly written code upon you. If it is absolutely necessary, well then that's a red flag: work hard to eliminate it as a necessity. You shouldn't have to write and maintain gobs of code to work around their inability to get the big obvious things right.
I'm currently using SetWindowsHookEx to inject my DLL into another process, however it does not get loaded right away. I noticed that if I manually click the window, it will get loaded then, so I'm guessing it is waiting for some type of message to get the activation rolling? I'm currently getting it activated with a
SetForegroundWindow(otherAppHwnd);
SetForegroundWindow(myAppHwnd);
But this seems like a hack, and doesn't always work (i.e. the otherAppHwnd is minimized to the taskbar.
Any pointers would be great!
Edit: It is a CBT Hook
I've successfully used this:
SendMessage(otherAppHwnd, WM_NULL, 0, 0);
to achieve what you want, but only with lower-level hooks like WH_GETMESSAGE, never a CBT hook. It might work. 8-)
It seems that this is not possible.
So instead of forcing my way around this, I decided to just roll with it and make my design be able to handle this.