How to prevent a window from opening from another application?
Question
I will prevent a window from iTunes to be opened. I googled a lot but can't find an iTunes library that allows me to control such things, so I think I must get back to basics and close it after it opens, but how?
I think:
- Tick a timer every 500 ms
- Check if the window handle is opened
- Close it
Is that possible? How can I recognize a window from this application on other computers (I will give away my application)?
Language is C#.Net 2.0.
Solution
Yes it's an option to find the window and close it. However the user will still see it.
You can do the PInvoke method of FindWindow or use the C# ones (prefer those)
using System.Diagnostics;
Process[] processes = Process.GetProcessesByName("notepad");
foreach (Process p in processes)
{
p.CloseMainWindow();
}
This is only for closing the top application, I dont know if you can find the subwindow with Process. I know you can with PInvoke, see example here
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
or
[DllImport("user32.dll", EntryPoint="FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
OTHER TIPS
Sure there are ways to do this, an approach I used before was based on the win32APIs, you'll want to look at the following:
In short you can use the timer, and when it fires use FindWindow (either using the window title bar, or the application "class") to get the handle, once you have that you SendMessage to the window of at least a WM_CLOSE or WM_DESTROY
Perhaps I'm wrong here, as I am pretty much an amatuer coder but let me take a shot at this:
Is this even possible? The mechanisms that control a window closing are within the application (In your example: iTunes) itself and the operating system. It seems like a huge security breach if one application can override another application's code and close a window. Doesn't seem plausible
Looks like I'm wrong here. Ignore!