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...
Printable View
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...
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();
}
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);
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.
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);
Perfect. Thanks guys :thumb:
We must have all watched Sesame Street: cooperation! :D
hahah
Co-operation ... makes it happen
Co-operation ... working together
Dig it!
Co-operation ... makes it happen
Co-operation ... working together