473,245 Members | 1,406 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,245 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 3014
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

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

Similar topics

8
by: Jonathan Heath | last post by:
Hi all, I have created an ASP script that enters data into an Access Database. My problem is that I'd like this script to run when the computer is shutdown or the user logs off. I think...
3
by: Tim Werth | last post by:
Is there an attribute or property that I can set in my AssemblyInfo.cs that is used to setup my COM+ application so that I can set the "Minutes until idle shutdown" of the app to something other...
3
by: Senthil | last post by:
Hi, I would like to know the code for reboot and shutdown windows xp professional using VB.Net. thanx Senthil
8
by: Bill Sonia | last post by:
I've written a Windows Service to send e-mails on events like OnStart, OnStop, OnShutDown using System.Web.Mail. It works for everything but OnShutdown. My guess is that for OnShutDown, once my...
6
by: carbon_dragon | last post by:
Ok, so here is the problem. I'm working on a headless server program implemented as a .NET C# Console project. There is a UPS mounted to this server (though not a windows compliant UPS). I can only...
2
by: Brian Worth | last post by:
I have just upgraded from VB 4.0 to VB .NET 2002. One program under VB 4.0 was able to shut down or restart the (windows XP) machine using a series of API calls. (Getlasterror, GetCurrentProcess,...
4
by: Andrew | last post by:
I have ASP.NET 2.0 application. I'm running some initialization code in Application_Start() in Global.asax. If this code fails I would like to shutdown the application completely. Meaning no...
2
by: Sin Jeong-hun | last post by:
I created a windows form application. It has a Threading.Timer and when the timer ticks it does some work and show a popup window. The problem is that while this program is running if the user...
3
by: IdleBrain | last post by:
Gurus, I am trying to delay Windows Shutdown/Restart to perfrom cleanup and I am using the following code: protected override void WndProc(ref Message ex)
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.