Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Closing Window

Closing Window

Scheduled Pinned Locked Moved C#
question
14 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R rakesh_nits

    Hi Joe, Thanks for your reply.I have opened the directory using Process Class of .Net framework.And it is being opened in explorer.Any suggestions how i can achieve this.Thanks in Advance. Rakesh

    J Offline
    J Offline
    joe carbone
    wrote on last edited by
    #4

    Hi Rakesh the process class has a property named Handle. with this property you can use the interop function SendMessage with the parameter WM_CLOSE. example: SendMessage(Handle, WM_CLOSE, 0, 0); or use the Kill Methode of your process class.

    R 1 Reply Last reply
    0
    • J joe carbone

      Hi Rakesh the process class has a property named Handle. with this property you can use the interop function SendMessage with the parameter WM_CLOSE. example: SendMessage(Handle, WM_CLOSE, 0, 0); or use the Kill Methode of your process class.

      R Offline
      R Offline
      rakesh_nits
      wrote on last edited by
      #5

      Hi Joe, Thanks for your reply.both Process.kill and SendMessage are throwing the same error. System.InvalidOperationException: No process is associated with this object. I tried with both UseShellExecute equal to true and false.In sendMessage case the error is coming while I am trying to use the Handle property.So the error is not coming in calling the sendMessage method but in getting the handle.Any Suggestions.I also tries using the startverb both as "Open" and "kill" but the result was same.Thanks in Advance. Rakesh

      R 1 Reply Last reply
      0
      • R rakesh_nits

        Hi Joe, Thanks for your reply.both Process.kill and SendMessage are throwing the same error. System.InvalidOperationException: No process is associated with this object. I tried with both UseShellExecute equal to true and false.In sendMessage case the error is coming while I am trying to use the Handle property.So the error is not coming in calling the sendMessage method but in getting the handle.Any Suggestions.I also tries using the startverb both as "Open" and "kill" but the result was same.Thanks in Advance. Rakesh

        R Offline
        R Offline
        rakesh_nits
        wrote on last edited by
        #6

        Hi Joe, My mistake that I hadn't specified the process name "explorer.exe" in the process.now I am able to get the handle but process.kill gives the error that the process is already exited.and closeHandle does not do anything.using getlasterror i am getting the following error. ERROR_MOD_NOT_FOUND 126 The specified module could not be found. here is the code static int WM_CLOSE = 0x0010; string path = @"C:\Documents and Settings\rakesh\Desktop\A" pr.StartInfo.CreateNoWindow = true; pr.StartInfo.Verb = "Open"; pr.StartInfo.FileName = "explorer.exe"; pr.StartInfo.Arguments = path; pr.Start(); //pr.Kill();//gives the error the process has already exited iptr = pr.Handle; SendMessage(iptr,(uint)WM_CLOSE,0,0); int errorCode = Marshal.GetLastWin32Error(); Console.WriteLine("errorCode::"+errorCode.ToString());//gives errorcode 126 ************************************** Any help will be greatly appreciated.Thanks in Advance. Rakesh

        J 1 Reply Last reply
        0
        • R rakesh_nits

          Hi Joe, My mistake that I hadn't specified the process name "explorer.exe" in the process.now I am able to get the handle but process.kill gives the error that the process is already exited.and closeHandle does not do anything.using getlasterror i am getting the following error. ERROR_MOD_NOT_FOUND 126 The specified module could not be found. here is the code static int WM_CLOSE = 0x0010; string path = @"C:\Documents and Settings\rakesh\Desktop\A" pr.StartInfo.CreateNoWindow = true; pr.StartInfo.Verb = "Open"; pr.StartInfo.FileName = "explorer.exe"; pr.StartInfo.Arguments = path; pr.Start(); //pr.Kill();//gives the error the process has already exited iptr = pr.Handle; SendMessage(iptr,(uint)WM_CLOSE,0,0); int errorCode = Marshal.GetLastWin32Error(); Console.WriteLine("errorCode::"+errorCode.ToString());//gives errorcode 126 ************************************** Any help will be greatly appreciated.Thanks in Advance. Rakesh

          J Offline
          J Offline
          joe carbone
          wrote on last edited by
          #7

          Hi Rakesh if you set CreateNoWindow = true; you can see the explorer window? if not, is difficult to close this process from code because the process don't have a window handle. the only way is to find out the window handle from the processID and then call the sendmessage function.

          R 1 Reply Last reply
          0
          • J joe carbone

            Hi Rakesh if you set CreateNoWindow = true; you can see the explorer window? if not, is difficult to close this process from code because the process don't have a window handle. the only way is to find out the window handle from the processID and then call the sendmessage function.

            R Offline
            R Offline
            rakesh_nits
            wrote on last edited by
            #8

            Hi Joe, Yes I am able to see the window.and also i am able to get the process handle.but not able to close the window either using process.kill or sendmessage.when i set createnowindow to false ,I am able to use pocess.kill successfully.now it's working.Thanks a lot. Rakesh

            R 1 Reply Last reply
            0
            • R rakesh_nits

              Hi Joe, Yes I am able to see the window.and also i am able to get the process handle.but not able to close the window either using process.kill or sendmessage.when i set createnowindow to false ,I am able to use pocess.kill successfully.now it's working.Thanks a lot. Rakesh

              R Offline
              R Offline
              rakesh_nits
              wrote on last edited by
              #9

              Hi Joe, May be i replied too early.even in this case if i make the current thread sleep for some time again i get the same error that the process has been exited.actually what i want basically to open a window and close it after some time but the process.kill works only if i give it just after process.start or when the sleep time is so that the window does not show at all.Thanks in Advance. Rakesh

              J 1 Reply Last reply
              0
              • R rakesh_nits

                Hi Joe, May be i replied too early.even in this case if i make the current thread sleep for some time again i get the same error that the process has been exited.actually what i want basically to open a window and close it after some time but the process.kill works only if i give it just after process.start or when the sleep time is so that the window does not show at all.Thanks in Advance. Rakesh

                J Offline
                J Offline
                joe carbone
                wrote on last edited by
                #10

                hi rakesh smile, the only way is to find out the window handle. tray this page http://msdn.microsoft.com/vstudio/downloads/101samples/default.aspx you find 101 c# samples in a msi file. this msi file contains a sample to find out all running processes in your system. download the right file, the page contains samples for both .net 1.1 and 2.0

                R 1 Reply Last reply
                0
                • J joe carbone

                  hi rakesh smile, the only way is to find out the window handle. tray this page http://msdn.microsoft.com/vstudio/downloads/101samples/default.aspx you find 101 c# samples in a msi file. this msi file contains a sample to find out all running processes in your system. download the right file, the page contains samples for both .net 1.1 and 2.0

                  R Offline
                  R Offline
                  rakesh_nits
                  wrote on last edited by
                  #11

                  Hi Joe, thanks for the link.I have got the handle using enumWindows.but even after that i am not able to close it. i tried SendMessage , CloseWindow(according to documentation it only minimizes the window),DestroyWindow,EndTask and some more but even after all this i am not able to close the window.although i can kill the process (windows explorer in this case) by using TerminateProcess, but I just want to close the window.the windowhandle i valid as i am able to get the text of all windows using GetWindowText but while trying to close the window I am getting different errors.Again thanks for the marvellous link. Rakesh

                  R 1 Reply Last reply
                  0
                  • R rakesh_nits

                    Hi Joe, thanks for the link.I have got the handle using enumWindows.but even after that i am not able to close it. i tried SendMessage , CloseWindow(according to documentation it only minimizes the window),DestroyWindow,EndTask and some more but even after all this i am not able to close the window.although i can kill the process (windows explorer in this case) by using TerminateProcess, but I just want to close the window.the windowhandle i valid as i am able to get the text of all windows using GetWindowText but while trying to close the window I am getting different errors.Again thanks for the marvellous link. Rakesh

                    R Offline
                    R Offline
                    rakesh_nits
                    wrote on last edited by
                    #12

                    Hi Joe, Thanks a lot.Finally i am able to do it using EndTask.Actually previously i was sending the third parameter fForce to true which kills the explorer instead of the task.But sending it as false closes the window.Thanks for all your help. Rakesh

                    J 1 Reply Last reply
                    0
                    • R rakesh_nits

                      Hi Joe, Thanks a lot.Finally i am able to do it using EndTask.Actually previously i was sending the third parameter fForce to true which kills the explorer instead of the task.But sending it as false closes the window.Thanks for all your help. Rakesh

                      J Offline
                      J Offline
                      joe carbone
                      wrote on last edited by
                      #13

                      Hi Rakesh this is a good message :-). if i can help you in other programming problems send me an email at: joe.carbone@mymail.ch Joe

                      1 Reply Last reply
                      0
                      • R rakesh_nits

                        Hi All, I want to close a directory which is being opened.what i have is only the path of the directory.How can i emulate clicking the close button on this window programmatically.Thanks in Advance. Rakesh

                        D Offline
                        D Offline
                        dbrenth
                        wrote on last edited by
                        #14

                        I know this is along time ago. But I was wondering if you would post the code that worked for you. The links pointed to in this thread don't work any more. Thanks

                        Brent

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups