This message was discovered on microsoft.public.dotnet.languages.csharp.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
| Paco Tudel |
| GOOD ANSWER |
English -------------------------------------------------------------------
Hi Everybody!
I'm trying to shutdown my Win XP with this code... but... don't work... :-(
Can someone tell me how to shutdown my XP whith C# or tell me why this code is not running?
Thanks in advance
EspaƱol--------------------------------------------------------------------
Hola a todos!
Me dirijo por primera vez al grupo para hacer una consulta sobre el apagado de una maquina XP, llevo unos dias intentando hacer un programa para apagar un pc y he llegado al siguiente codigo, que falla en el momento en que comprueba la identidad del usuario que intenta apagar la maquina, puede alguien decirme donde falla este codigo? o mandar al grupo el codigo utilizado para apagar un win XP?
Muchas gracias
--------------------------------------------------------------------
Francisco Tudel
Systems Engineer
Ingeniero de sistemas
----------- Code
public const int SE_PRIVILEGE_ENABLED = 0x00000002;
public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
public const int ANYSIZE_ARRAY = 1;
public const int EWX_LOGOFF = 0;
public const int EWX_SHUTDOWN = 1;
public const int EWX_REBOOT = 2;
public const int EWX_FORCE = 4;
public const int EWX_POWEROFF = 8;
public const int EWX_FORCEIFHUNG = 16;
[DllImport("user32.dll")]
public static extern System.Boolean ExitWindowsEx(
int uFlags,
int dwReserved);
[StructLayout(LayoutKind.Sequential)]
public struct LUID{
public int LowPart;
public int HighPart;
}
[StructLayout(LayoutKind.Sequential)]
public struct LUID_AND_ATTRIBUTES{
public LUID Luid;
public int Attributes;
}
[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_PRIVILEGES{
public int PrivilegeCount;
[MarshalAs(UnmanagedType.ByValArray,SizeConst=ANYSIZE_ARRAY)]
public LUID_AND_ATTRIBUTES[] Privileges;
}
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern System.Boolean OpenProcessToken(
int ProcessHandle,
int DesiredAccess,
ref int TokenHandle);
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern int GetCurrentProcess();
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern System.Boolean LookupPrivilegeValue(
string lpSystemName,
string lpName,
[MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern System.Boolean AdjustTokenPrivileges(
int TokenHandle,
int DisableAllPrivileges,
[MarshalAs(UnmanagedType.Struct)] ref TOKEN_PRIVILEGES NewState,
int BufferLength,
[MarshalAs(UnmanagedType.Struct)] ref TOKEN_PRIVILEGES PreviousState,
int ReturnLength);
private void ShutDown(){
int ltkpOld = 0;
int hToken =0;
TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES();
tkp.Privileges = new LUID_AND_ATTRIBUTES[ANYSIZE_ARRAY];
TOKEN_PRIVILEGES tkpOld = new TOKEN_PRIVILEGES();
tkpOld.Privileges = new LUID_AND_ATTRIBUTES[ANYSIZE_ARRAY];
LUID tLUID = new LUID();
if(LookupPrivilegeValue(null , SE_SHUTDOWN_NAME, ref tLUID)){
int hProceso = GetCurrentProcess();
if(hProceso != 0){
if (OpenProcessToken(hProceso, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref hToken)) {
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
tkp.Privileges[0].Luid.HighPart = tLUID.HighPart;
tkp.Privileges[0].Luid.LowPart = tLUID.LowPart;
int ltkp = Marshal.SizeOf(typeof(TOKEN_PRIVILEGES));
if(AdjustTokenPrivileges(hToken, 0, ref tkp, ltkp, ref tkpOld, ref ltkpOld))
ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0);
}
}
}
}
----- Fin de codigo
|
|
|
| |
|
|
| |
| |
| NETMaster (VIP) |
| GOOD ANSWER |
Using PInvoke to AdjustTokenPrivileges and ExitWindowsEx: // ============================================================================== 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_POWEROFF ); // ==============================================================================
WMI method provided by Willy Denoyette (Click here to reveal e-mail address), Thanks! // ========================================================= using System.Management; using System.Runtime.InteropServices;
ManagementPath path = new ManagementPath( ); path.Server = "yourpcname"; path.NamespacePath = @"root\CIMV2"; // See Boot.ini for OS identification, System dir and HD system partition. path.RelativePath = @"Win32_OperatingSystem.Name=""Microsoft Windows 2000 Professional|C:\\WINNT|\\Device\\Harddisk0\\Partition1"""; ManagementObject o = new ManagementObject(path); ManagementBaseObject inParams = null; bool EnablePrivileges = o.Scope.Options.EnablePrivileges; o.Scope.Options.EnablePrivileges = true; ManagementBaseObject outParams = o.InvokeMethod("Shutdown", inParams, null); o.Scope.Options.EnablePrivileges = EnablePrivileges; // =========================================================
"Paco Tudel" <Click here to reveal e-mail address> wrote in message news:uwnH0jHsBHA.2660@tkmsftngp05... [Original message clipped]
|
|
|
| |
|
|
| |
|
|
|
|
|