By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,759 Members | 1,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,759 IT Pros & Developers. It's quick & easy.

shutdown my computer using C#

P: n/a
Dear sirs
I want to know how can I shutdown or restart my computer
using C#
Also I want to know how can I open any windows program
using C# (EX: opening the windows calculator from my
application)

thanks
Mohammed
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
You can use ExitWindowsEx() Win32 API (thru PInvoke) to shutdown/restart the
computer.

For opening other apps, you can use Process.Start() method.

"Mohammed Abdel-Razzak" <an*******@discussions.microsoft.com> wrote in
message news:1c****************************@phx.gbl...
Dear sirs
I want to know how can I shutdown or restart my computer
using C#
Also I want to know how can I open any windows program
using C# (EX: opening the windows calculator from my
application)

thanks
Mohammed
Nov 16 '05 #2

P: n/a
"Mohammed Abdel-Razzak" <an*******@discussions.microsoft.com> wrote in
message news:1c****************************@phx.gbl...
I want to know how can I shutdown or restart my computer using C#
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}

[DllImport("kernel32.dll", ExactSpelling=true) ]
internal static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr
phtok );

[DllImport("advapi32.dll", SetLastError=true) ]
internal static extern bool LookupPrivilegeValue( string host, string name,
ref long pluid );

[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );

[DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool ExitWindowsEx( int flg, int rea );

internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;

private void DoExitWin( int flg )
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref
htok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero,
IntPtr.Zero );
ok = ExitWindowsEx( flg, 0 );
}

/* usage:
DoExitWin( EWX_SHUTDOWN );
or
DoExitWin( EWX_REBOOT );
*/

Also I want to know how can I open any windows program using C# (EX:

opening the windows calculator from my application)

Process.Start("calc.exe");
Nov 16 '05 #3

P: n/a
thanks so much for your help, but I don`t know where I
place that code
-----Original Message-----
"Mohammed Abdel-Razzak" <an*******@discussions.microsoft.com> wrote inmessage news:1c****************************@phx.gbl...
I want to know how can I shutdown or restart my
computer using C#
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}

[DllImport("kernel32.dll", ExactSpelling=true) ]
internal static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtrphtok );

[DllImport("advapi32.dll", SetLastError=true) ]
internal static extern bool LookupPrivilegeValue( string host, string name,ref long pluid );

[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );
[DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]internal static extern bool ExitWindowsEx( int flg, int rea );
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;

private void DoExitWin( int flg )
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, refhtok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid ); ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero,IntPtr.Zero );
ok = ExitWindowsEx( flg, 0 );
}

/* usage:
DoExitWin( EWX_SHUTDOWN );
or
DoExitWin( EWX_REBOOT );
*/

Also I want to know how can I open any windows program
using C# (EX:opening the windows calculator from my application)

Process.Start("calc.exe");
.

Nov 16 '05 #4

P: n/a
<an*******@discussions.microsoft.com> wrote in message
news:20****************************@phx.gbl...
thanks so much for your help, but I don`t know where I
place that code
Place it wherever you want! If you want the user to shut down the computer
(or launch Calculator) by clicking a button, place the code in the button's
_Click() event, etc...
-----Original Message-----
"Mohammed Abdel-Razzak"

<an*******@discussions.microsoft.com> wrote in
message news:1c****************************@phx.gbl...
I want to know how can I shutdown or restart my

computer using C#

using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}

[DllImport("kernel32.dll", ExactSpelling=true) ]
internal static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll", ExactSpelling=true,

SetLastError=true) ]
internal static extern bool OpenProcessToken( IntPtr h,

int acc, ref IntPtr
phtok );

[DllImport("advapi32.dll", SetLastError=true) ]
internal static extern bool LookupPrivilegeValue( string

host, string name,
ref long pluid );

[DllImport("advapi32.dll", ExactSpelling=true,

SetLastError=true) ]
internal static extern bool AdjustTokenPrivileges(

IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr

relen );

[DllImport("user32.dll", ExactSpelling=true,

SetLastError=true) ]
internal static extern bool ExitWindowsEx( int flg, int

rea );

internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME

= "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;

private void DoExitWin( int flg )
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken( hproc,

TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref
htok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME,

ref tp.Luid );
ok = AdjustTokenPrivileges( htok, false, ref tp, 0,

IntPtr.Zero,
IntPtr.Zero );
ok = ExitWindowsEx( flg, 0 );
}

/* usage:
DoExitWin( EWX_SHUTDOWN );
or
DoExitWin( EWX_REBOOT );
*/

Also I want to know how can I open any windows program

using C# (EX:
opening the windows calculator from my application)

Process.Start("calc.exe");
.

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.