471,066 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4.          [DllImport("user32.dll")]
  5.         public static extern int ExitWindowsEx(uint uflag,uint reson);
  6.  
  7.              public void shutdown_server(uint f)
  8.         {
  9.             ExitWindowsEx(f,0);
  10.                   }
  11.  
  12.         public void shut_server_exe(string arg)
  13.         {
  14.  
  15.           System.Diagnostics.Process.Start("C:/shutdown.exe", arg);
  16.                 }
  17.  
  18.         [DllImport("Powrprof.dll")]
  19.         public static extern bool SetSuspendState(bool h, bool g, bool i);
  20.                public void standby()
  21.         {
  22.             SetSuspendState(false, false, false);
  23.         }
  24.  
  25.  
  26.  

client code



Expand|Select|Wrap|Line Numbers
  1.  
  2. private void logOffToolStripMenuItem_Click(object sender, EventArgs e)
  3.         {
  4.             try
  5.             {
  6.                 serverclass obj = (serverclass)Activator.GetObject(
  7.                               typeof(serverclass),
  8.                               "http://" + IP + ":1234/serverclass.soap");
  9.                 obj.shutdown_server(0);
  10.             }
  11.             catch (Exception err)
  12.             {
  13.                 MessageBox.Show(err.Message);
  14.                 disconnect();
  15.             }
  16.         }
  17.  
  18.         private void turnOffToolStripMenuItem_Click(object sender, EventArgs e)
  19.         {
  20.             try
  21.             {
  22.                 serverclass obj = (serverclass)Activator.GetObject(
  23.                               typeof(serverclass),
  24.                               "http://" + IP + ":1234/serverclass.soap");
  25.                 //obj.DoExitWin(2);
  26.                // obj.shutdown_server(8);
  27.                 obj.shut_server_exe("/L /C /Y");
  28.             }
  29.             catch (Exception err)
  30.             {
  31.                 MessageBox.Show(err.Message);
  32.                 disconnect();
  33.             }
  34.         }
  35.  
  36.         private void restartToolStripMenuItem_Click(object sender, EventArgs e)
  37.         {
  38.             try
  39.             {
  40.                 serverclass obj = (serverclass)Activator.GetObject(
  41.                               typeof(serverclass),
  42.                               "http://" + IP + ":1234/serverclass.soap");
  43.                // obj.shutdown_server(2);
  44.                 obj.shut_server_exe("/L /R /C /Y");
  45.             }
  46.             catch (Exception err)
  47.             {
  48.                 MessageBox.Show(err.Message);
  49.                 disconnect();
  50.             }
  51.         }
  52.  
  53.         private void lockPCToolStripMenuItem_Click(object sender, EventArgs e)
  54.         {
  55.             try
  56.             {
  57.  
  58.                 serverclass obj = (serverclass)Activator.GetObject(
  59.                                          typeof(serverclass),
  60.                                          "http://" + IP + ":1234/serverclass.soap");
  61.                 obj.standby();
  62.             }
  63.             catch (Exception err)
  64.             {
  65.                 MessageBox.Show(err.Message);
  66.                 disconnect();
  67.             }
  68.         }
  69.  

thanks friends
Mar 12 '08 #1
1 2855
Plater
7,872 Expert 4TB
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?)
Mar 12 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Bill Sonia | last post: by
2 posts views Thread by Sin Jeong-hun | last post: by
3 posts views Thread by IdleBrain | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.