473,396 Members | 1,875 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,396 software developers and data experts.

shutdown my computer using C#

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
4 42159
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
"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
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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Won | last post by:
I want to know how to shut down computer using VB .NET. Thank you
10
by: Progman | last post by:
How do you get the IP address of a computer using ASP.net? I tried Request.UserHostAddress and it does not work. I get Request for the permission of type...
1
by: habeeb | last post by:
hello i need help, i want to know how we can access a table in Msaccess database of one computer using another computer. I tried by giving the path of one computer in the data source path of...
0
by: koh soo min | last post by:
Hi, Can anyone give me hints to write VB code for send and receive message between two computer using ComPort. Thanks.
2
by: yellowblueyellow | last post by:
Hi, I need a function to List all users in a group on a local computer using C#. any assistance would be much appreciated
0
xxoulmate
by: xxoulmate | last post by:
how to extract username of computer using nbtstat., in dos mode i use nbtstat -a computername then it list down all the workgroup,the computername,and the username was there but the problem., is...
1
by: pnalla | last post by:
Hi, I am Connecting ti remote computer using C# WMI through the following code.But i t gives Some error "INVALID PARAMETERS" plse suggest me any modification in the code. try { ...
0
by: phpuser123 | last post by:
I want to connect to a computer using its IP address.. I want to use J2ME to achieve that.. I investigated a bit and came up with Connector.open(url), however, I also found out that mobile phones...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.