views:

101

answers:

3

Hi,

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.

+1  A: 

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();
}

From here

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);
PoweRoy
A: 

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!

joslinm
it's do-able, it might raise some eyebrows, but it certainly is something that you can do. If you want to get even more squirrelly, look into application hooks....again it may raise some eyebrows, but there are legitimate uses
curtisk
Ahh ok, thanks for the info!
joslinm
for future reference, so you can avoid some future grumbles from the user base: if you aren't actually answering the main question, you should add a comment to the main question, that's the area for discussion(comments), answers should only be answers(though they can have comments below them, like here..)
curtisk
thanks curtisk, I'll remember that.
joslinm
A: 

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:

  1. FindWindow
  2. SendMessage

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

curtisk