|
-
Feb 8th, 2006, 04:09 AM
#1
Thread Starter
Lively Member
How To Close A Window (Kill A Process With Only Window Name)
Does anyone have example code of how to close a window/process with only the name of the window?
Not sure if this should be in API or C# section...
-
Feb 8th, 2006, 04:18 AM
#2
Re: How To Close A Window (Kill A Process With Only Window Name)
What exactly do you mean by "the name of the window"? Do you mean the text in the title bar? I'm sure that you'd have to use APIs to get the ID of the process to which the window belonged. I can give you some code to use once you have the process ID.
Code:
System.Diagnostics.Process myProcess = System.Diagnostics.Process.GetProcessById(procID);
// Ask the process to exit.
myProcess.CloseMainWindow();
// Wait up to 10 seconds for the process to exit gracefully.
myProcess.WaitForExit(10000);
if (!myProcess.HasExited)
{
// Force the process to exit.
myProcess.Kill();
}
-
Feb 8th, 2006, 04:30 AM
#3
Thread Starter
Lively Member
Re: How To Close A Window (Kill A Process With Only Window Name)
Cool. I do have the window caption/text. The Findwindow function might be what I need for this to get the handle
Code:
[DllImport("user32", EntryPoint = "FindWindowA")] private static extern IntPtr FindWindow( [MarshalAs(UnmanagedType.LPStr)] string lpClassName, [MarshalAs(UnmanagedType.LPStr)] string lpWindowName ); IntPtr hWndCMD = FindWindow("NAMEOFWINDOWClass", sTitleOfWindow);
but how do i convert IntPtr to int?
Code:
System.Diagnostics.Process myProcess = System.Diagnostics.Process.GetProcessById(hWndCMD);
Last edited by 2MuchRiceMakesMeSick; Feb 8th, 2006 at 04:38 AM.
-
Feb 8th, 2006, 04:37 AM
#4
Re: How To Close A Window (Kill A Process With Only Window Name)
IntPtr.ToInt32 will do that, but keep in mind that a window handle is not a process handle. I've got a feeling there's an API function that will get the process ID from a window handle but my memory is a bit foggy.
-
Feb 8th, 2006, 11:19 AM
#5
New Member
Re: How To Close A Window (Kill A Process With Only Window Name)
Use the window handle you get from FindWindow with this one to get the process ID
PHP Code:
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, [Out] uint lpdwProcessId);
-
Feb 8th, 2006, 04:09 PM
#6
Thread Starter
Lively Member
Re: How To Close A Window (Kill A Process With Only Window Name)
Perfect. Thanks guys
-
Feb 8th, 2006, 05:47 PM
#7
Re: How To Close A Window (Kill A Process With Only Window Name)
We must have all watched Sesame Street: cooperation!
-
Feb 8th, 2006, 09:48 PM
#8
Thread Starter
Lively Member
Re: How To Close A Window (Kill A Process With Only Window Name)
hahah
Co-operation ... makes it happen
Co-operation ... working together
Dig it!
Co-operation ... makes it happen
Co-operation ... working together
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|