Results 1 to 8 of 8

Thread: How To Close A Window (Kill A Process With Only Window Name)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    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...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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();
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    New Member
    Join Date
    Oct 2005
    Posts
    6

    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, [Outuint lpdwProcessId); 

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Talking Re: How To Close A Window (Kill A Process With Only Window Name)

    Perfect. Thanks guys

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How To Close A Window (Kill A Process With Only Window Name)

    We must have all watched Sesame Street: cooperation!
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    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
  •  



Click Here to Expand Forum to Full Width