"The appplication was unable to start correctly (0xC0000142). Click OK to close the application" shutdown.exe

Answered "The appplication was unable to start correctly (0xC0000142). Click OK to close the application" shutdown.exe

  • 24 februarie 2012 12:23
     
     

    Hey folks, 

    when I try to stop shutdown using c# i can't perform this because of the above error , even when i try to do that using cmd or run i get the same error, is there any shutdown process or service to stop or what to do ??

    thanks in advance

Toate mesajele

  • 24 februarie 2012 12:48
     
     
    Have you attempted to search on the error code? There seems to be lots of the same issue, and quite a few with solutions for the issue, even just in the forums.
  • 24 februarie 2012 13:08
     
     
         

      using System.Management;

        void Shutdown()
        {
            ManagementBaseObject mboShutdown = null;
            ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
            mcWin32.Get();

            // You can't shutdown without security privileges
            mcWin32.Scope.Options.EnablePrivileges = true;
            ManagementBaseObject mboShutdownParams =
                     mcWin32.GetMethodParameters("Win32Shutdown");

            // Flag 1 means we want to shut down the system. Use "2" to reboot.
            mboShutdownParams["Flags"] = "1";
            mboShutdownParams["Reserved"] = "0";
            foreach (ManagementObject manObj in mcWin32.GetInstances())
            {
                mboShutdown = manObj.InvokeMethod("Win32Shutdown", 
                                               mboShutdownParams, null);
            }

        } http://www.geekpedia.com/code36_Shut-down-system-using-Csharp.html


    Peter Koueik




  • 24 februarie 2012 13:29
     
     

    ThisBytes5 , of course I tried google first solutions i found there didn't solve my problem ,

    Peter koueik, i don't wanna shutdown, i wanna cancel shutdown 

    thanks anyways

      
  • 24 februarie 2012 13:37
     
     
    foreach ( Process p in System.Diagnostics.Process.GetProcessesByName("shutdown") )
    {
        try
        {
            p.Kill();
            p.WaitForExit(); // possibly with a timeout
        }
        catch ( Win32Exception winException )
        {
            // process was terminating or can't be terminated - deal with it
        }
        catch ( InvalidOperationException invalidException )
        {
            // process has already exited - might be able to let this one go
         }
    }

    Peter Koueik

  • 24 februarie 2012 13:52
     
     
    already tried this and didn't work , the problem is i can't start any process while shutting down tried to run this directly without c# couldn't run "shutdown -a"  because system cant start cmd or run to run the command :'(
  • 24 februarie 2012 13:55
     
     
  • 24 februarie 2012 15:19
     
     

    well allow me to explain... the thread you gave was not marked as answered by the person who asked the initial question.. so basically the person never got his solution... however the moderators followed the logic behind it and knew that if implemented it will solve the issue...
    the logic consists of creating a windows service in c#.... that stops the shutdown process because when you initiate the shutdown process all the processes are killed... so basically it is a race between the call to abort the shutdown in your solution and the process termination of the shutdown... and guess who will win ;)..
    anyway the service is the last to shut down ... thus you can use it to abort the shutting down...
    ill see if i can help you create a windows service that does that.
    please in the meanwhile if you get to any sort of a solution post it here so that others can learn thank you.


    Peter Koueik

  • 24 februarie 2012 15:27
     
     

    in the mean time i have found an interesting thread...
    check it out and study it as i will ...
    http://stackoverflow.com/questions/6799955/how-to-detect-windows-shutdown-or-logoff

    this too...

    http://msdn.microsoft.com/en-us/library/bb394721.aspx
    ps: important note within the previous link:
    This event is only raised if the message pump is running. In a Windows service, unless a hidden form is used or the message pump has been started manually, this event will not be raised.


    Peter Koueik



  • 24 februarie 2012 15:28
     
     Răspuns

    While the service solution is the best way to accomplish what you want, I think it's worth pointing out that this is typically not what you want to do. Your software should never wrest control of a computer from the user. The computer belongs to the user, and we have to work under the assumption that they know what they're doing better than your software does.

    If the user decides to shut down their machine, it's bad form for your application to prevent that. If you can tell us what base problem you're trying to solve or why you believe your application has to prevent shutdown, maybe we can help you come up with a better solution so you can avoid this situation altogether.


    Check out My Blog. Now updated to actually work!

  • 24 februarie 2012 15:36
     
     

    and even if it worked... the result varies amongst different versions of windows...
    check this!
    http://msdn.microsoft.com/en-us/library/bb394721.aspx

    and yettt in this link...
    there is this info :

    • Applications should not block shutdown. Respond to WM_QUERYENDSESSION as quickly as possible and postpone cleanup activities until processing the WM_ENDSESSION message.
    • Applications that must block shutdown should use the new ShutdownBlockReasonCreate function to register a string that explains the reason to the user. The user can decide whether to continue or cancel shutdown.
    • Applications cannot rely on being able to block shutdown.

    so basically what Tim Copenhaver is telling you is 100 % true...
    and thus the best solution is to fire an event right before shutting down as to make sure the user wants to shutdown...
    probably not the answer you are looking for ... good luck .

    Thank you Tim for the great logic behind your thread... nice to see such fine logic these days.


    Peter Koueik




  • 24 februarie 2012 17:57
     
     

    Peter koueik, i already tried those links u sent but none of them gave me what i want , yeah ShutdownBlockReasonCreate  blocks the shutdown but i want something to cancel the shutdown initiated by the shutdown button, 

    Tim Copenhaver,  i want this app to ask user before shutdown just like windows xp (shutdown 2 steps), so what i want is to abort shutdown that initiated by clicking shutdown button and let the application handle it 

    thanks guys...



  • 24 februarie 2012 18:22
     
     
  • 24 februarie 2012 18:41
     
     Răspuns
    if what you want is for the user when he/she clickes on shutdown button of windows your application detects the shutdown process and fires an alert asking if they are sure to shutdown the windows or cancel the shutdown process...
    i found this link
    http://msdn.microsoft.com/en-us/library/system.windows.application.sessionending.aspx

    By default, an application shuts down when the Windows session ends, which occurs when a user logs off or shuts down. When this happens, Windows asks each open application to shut down. However, it is possible that an application may not be ready to shut down when this occurs. For example, an application may have data that is in an inconsistent state, or in the middle of a long-running operation. In these situations, it may be desirable to prevent the session from ending, and may be more desirable to allow users the option to decide whether or not to let the session to end.

    You can detect when a session ends by handling the SessionEnding event. If an application needs to prevent the session from ending, the SessionEndingCancelEventArgs argument that is passed to the event handler exposes the Cancel that you set to true (the default value is false).
    If SessionEnding is unhandled, or is handled without being cancelled, Shutdown is called and the Exit event is raised.
    The following example demonstrates how to handle the SessionEnding event and allow the user to cancel it.

    using
    System.Windows; // Application, SessionEndingCancelEventArgs, MessageBox, MessageBoxResult, MessageBoxButton

    namespace SDKSample
    {
        public partial class App : Application
        {
            void App_SessionEnding(object sender, SessionEndingCancelEventArgs e)
            {
                // Ask the user if they want to allow the session to end
                string msg = string.Format("{0}. End session?", e.ReasonSessionEnding);
                MessageBoxResult result = MessageBox.Show(msg, "Session Ending", MessageBoxButton.YesNo);

                // End session, if specified
                if (result == MessageBoxResult.No)
                {
                    e.Cancel = true;
                }
            }
        }
    }

    dunno if this helps or if you already tried it...


    Peter Koueik

  • 24 februarie 2012 18:51
     
     

    yes but not the solution , here is my code which i think closest solution so far :

                               

     protected override void OnFormClosing(FormClosingEventArgs e)
            {
                if (e.CloseReason.Equals(CloseReason.WindowsShutDown))
                {
                    try
                    {
                        System.Diagnostics.Process.Start("cmd.exe", "/C " + "shutdown -a");
                        MessageBox.Show("shutdown was canceled :)");
                    }
                    catch
                    {
                        MessageBox.Show("couldn't cancel it :(");
                    }

                }
                base.OnFormClosing(e);
            }  but it can't cancel the shutdown ........

    thank u very much Peter koueik


    • Editat de lily86 24 februarie 2012 19:12
    •  
  • 24 februarie 2012 19:03
     
     

    from what i see is that you are trying to cancel the shutdown PROCESS which i think can't be done directly...
    because you are trying to start the cmd process after the shutdown has allready fired the termination of all processes... thus you wont be able to start the cmd process to be able to cancel the shutdown...

    however when the shutdown process starts a session ending event is fired as the previous thread...
    you can cancel the ending of the session thus cancel the whole shutdown process..
    in the previous thread

    void App_SessionEnding(object sender, SessionEndingCancelEventArgs e)
    {
       if dont shutdown
       {
          e.Cancel = true;
       }
    }


    Peter Koueik



  • 24 februarie 2012 19:36
     
     

     protected override void OnFormClosing(FormClosingEventArgs e)
            {
                if (e.CloseReason.Equals(CloseReason.WindowsShutDown))
                {
                    try
                    {
                     foreach(Process in System.Diagnostics.Process.GetProcessesByName("shutdown") )
                      { 
                            try 
                            { 
                               p.Kill(); 
                            }
                          catch ( Win32Exception winException )
                          {
                                 // process was terminating or can't be terminated - deal with it
                           } 
                          catch ( InvalidOperationException invalidException )
                          {
                             // process has already exited - might be able to let this one go
                           }

                   }                 

                  MessageBox.Show("shutdown was canceled :)");
               }
              catch
              {
                        MessageBox.Show("couldn't cancel it :(");
              }

         }
                base.OnFormClosing(e);
      }

                  what happens when you try this?         

    Peter Koueik

  • 24 februarie 2012 22:01
     
     
    already tried this and nothings happens also tried to stop service but seems there is no process nor service named "shutdown" starts when shutdown button clicked 
  • 25 februarie 2012 07:22
     
     

    solution that is working on win xp:

    using System;
    using System.Windows.Forms;
    using Microsoft.Win32;

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                SystemEvents.SessionEnding += SessionEndingEvtHandler;
            }
            private void SessionEndingEvtHandler(object sender, SessionEndingEventArgs e)
            {
                MessageBox.Show("Cancelling Windows shutdown");
                e.Cancel = true;
                
            }
          
        }
    }


    Peter Koueik

  • 25 februarie 2012 11:55
     
     

    but doesn't work on win 7 

    look how shutdown works on win7 : http://www.windowslifestyle.com/shutdown-boot-process-works-windows-7/  

  • 27 februarie 2012 05:25
     
     
    still no solution?

    Peter Koueik