shutdown windows programmatically in .NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chandru
    New Member
    • Aug 2006
    • 5

    shutdown windows programmatically in .NET

    hai friends,

    Iam developing a application using .NET remoting ,in which it shuts down,logs off , restart , locks remote PC.i tried to use windows ExwindowsEXAPI for shutdown,logoff , restart and SetSuspendState for lock PC and the later worked perfectly but the problem is ExwindowsEx only Logsoff the PC and shutdown and restart is not working .Although i achieved the result using shutdown.exe ,but iam not convinced,so could anyone say what may be the problem.


    server code
    Code:
     
    
    
             [DllImport("user32.dll")]
            public static extern int ExitWindowsEx(uint uflag,uint reson);
    
                 public void shutdown_server(uint f)
            {
                ExitWindowsEx(f,0);
                      }
            
            public void shut_server_exe(string arg)
            {
            
              System.Diagnostics.Process.Start("C:/shutdown.exe", arg);
                    }
    
            [DllImport("Powrprof.dll")]
            public static extern bool SetSuspendState(bool h, bool g, bool i);
                   public void standby()
            {
                SetSuspendState(false, false, false);
            }

    client code



    Code:
    private void logOffToolStripMenuItem_Click(object sender, EventArgs e)
            {
                try
                {
                    serverclass obj = (serverclass)Activator.GetObject(
                                  typeof(serverclass),
                                  "http://" + IP + ":1234/serverclass.soap");
                    obj.shutdown_server(0);
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                    disconnect();
                }
            }
    
            private void turnOffToolStripMenuItem_Click(object sender, EventArgs e)
            {
                try
                {
                    serverclass obj = (serverclass)Activator.GetObject(
                                  typeof(serverclass),
                                  "http://" + IP + ":1234/serverclass.soap");
                    //obj.DoExitWin(2);
                   // obj.shutdown_server(8);
                    obj.shut_server_exe("/L /C /Y");
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                    disconnect();
                }
            }
    
            private void restartToolStripMenuItem_Click(object sender, EventArgs e)
            {
                try
                {
                    serverclass obj = (serverclass)Activator.GetObject(
                                  typeof(serverclass),
                                  "http://" + IP + ":1234/serverclass.soap");
                   // obj.shutdown_server(2);
                    obj.shut_server_exe("/L /R /C /Y");
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                    disconnect();
                }
            }
    
            private void lockPCToolStripMenuItem_Click(object sender, EventArgs e)
            {
                try
                {
    
                    serverclass obj = (serverclass)Activator.GetObject(
                                             typeof(serverclass),
                                             "http://" + IP + ":1234/serverclass.soap");
                    obj.standby();
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                    disconnect();
                }
            }

    thanks friends
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Pretty sure using the shutdown.exe is a good way to do it, but for remote PCs the shutdown.exe must be using some kind of commands to do it, and those should exist in the win32 APIs somewhere (RPC call maybe?)

    Comment

    Working...