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 4 42134
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
"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");
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");
.
<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 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");
. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Won |
last post by:
I want to know how to shut down computer using VB .NET. Thank you
|
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...
|
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...
|
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.
|
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
|
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...
|
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
{
...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |