Re: how to reboot, or shutdown the computer ?
- From: Herbert VON GRÜNENWALD <herbert.vongrunenwald@xxxxxxxxxxxxx>
- Date: Tue, 05 Apr 2005 17:12:20 +0200
Willy Denoyette [MVP] wrote:
"Lebesgue" <nospam@xxxxxxx> wrote in message news:e5GaZ0dOFHA.1040@xxxxxxxxxxxxxxxxxxxxxxxyes, it dosn't work, the version that works is (a little bit more longuer...):
In C#, it is still the same ExitWindowsEx API call.
class Class1 { [DllImport("user32.dll")] static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
[STAThread]
static void Main(string[] args)
{
ExitWindowsEx(1, 0); //this will cause the system to
shut down.
}
}
uFlags 4 = Force any applications to quit instead of prompting the user to close them. 0 = Log off the network. 8 = Shut down the system and, if possible, turn the computer off. 2 = Perform a full reboot of the system. 1 = Shut down the system.
"Herbert VON GRÜNENWALD" <herbert.vongrunenwald@xxxxxxxxxxxxx> wrote in message news:OICzQvdOFHA.2748@xxxxxxxxxxxxxxxxxxxxxxx
in c++ it was ExitWindowsEx()
thanks
Did you test your code? If you actualy did you would have noticed that this doesn't work.
The reason is simple, you have to enable the "SeShutdown" privilege using PInvoke, something more complicated than calling ExitWindowsEx.
Willy.
using System; using System.Runtime.InteropServices;
namespace WINAPIInterface
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class WINAPIProcess
{
public WINAPIProcess()
{
//
// TODO: Add constructor logic here
//
}
[StructLayout(LayoutKind.Sequential)]
public struct ProcessEntry32
{
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public IntPtr th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public int pcPriClassBase;
public uint dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)] public string szExeFile;
};
[DllImport("KERNEL32.DLL")]
public static extern int CreateToolhelp32Snapshot(uint flags, uint processid);
[DllImport("KERNEL32.DLL")]
public static extern int Process32First(int handle, ref ProcessEntry32 pe);
[DllImport("KERNEL32.DLL")]
public static extern int Process32Next(int handle, ref ProcessEntry32 pe); [DllImport("KERNEL32.DLL")]
public static extern int CloseHandle(int handle);public uint SNAPPROCESS = 2;
[DllImport("KERNEL32.DLL")]
public static extern int OpenProcess(uint dwDesiredAccess, int bInheritHandle, uint dwProcessId);
[StructLayout(LayoutKind.Sequential)]
public class PROCESS_MEMORY_COUNTERS
{
public int cb;
public int PageFaultCount;
public int PeakWorkingSetSize;
public int WorkingSetSize;
public int QuotaPeakPagedPoolUsage;
public int QuotaPagedPoolUsage;
public int QuotaPeakNonPagedPoolUsage;
public int QuotaNonPagedPoolUsage;
public int PagefileUsage;
public int PeakPagefileUsage;
}[DllImport("psapi.dll")]
public static extern int GetProcessMemoryInfo(int hProcess, [Out] PROCESS_MEMORY_COUNTERS counters, int size);
[StructLayout(LayoutKind.Sequential)]
public struct LUID
{
public uint LowPart;
public uint HighPart;
};public const int ANYSIZE_ARRAY = 1;
[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_PRIVILEGES
{
public uint PriviledgeCount;
public LUID Luid;
public uint Attributes;}
[DllImport("KERNEL32.DLL")]
public static extern int GetCurrentProcess(); [DllImport("advapi32.dll")]
public static extern System.Boolean OpenProcessToken(
int ProcessHandle,
uint DesiredAccess,
ref int Tokenhandle
); [DllImport("advapi32.dll")]
public static extern System.Boolean LookupPrivilegeValue(
string lpSsytemName,
string lpName,
ref LUID pluid); [DllImport("advapi32.dll")]
public static extern int AdjustTokenPrivileges(
int TokenHandle,
int DisableAllPrivileges,
[MarshalAs(UnmanagedType.Struct)]ref TOKEN_PRIVILEGES NewState,
int BufferLength,
int PreviousState,
int ReturnLength); [DllImport("user32.dll")]
public static extern bool ExitWindowsEx(int flg, int rea); public enum EWX_ENUM
{
EWX_LOGOFF = 0x00000000,
EWX_SHUTDOWN = 0x00000001,
EWX_REBOOT = 0x00000002,
EWX_FORCE = 0x00000004,
EWX_POWEROFF= 0x00000008,
EWX_FORCEIFHUNG = 0x00000010,
EWX_FORCEREBOOT = EWX_REBOOT | EWX_FORCE,
EWX_FORCEIFHUNGREBOOT = EWX_REBOOT | EWX_FORCEIFHUNG,
EWX_FORCESHUTDOWN = EWX_SHUTDOWN | EWX_FORCE,
EWX_FORCEIFHUNGSHUTDOWN = EWX_SHUTDOWN | EWX_FORCEIFHUNG,
EWX_FORCEPOWEROFF = EWX_POWEROFF | EWX_FORCE,
EWX_FORCEIFHUNGPOWEROFF = EWX_POWEROFF | EWX_FORCEIFHUNG,
EWX_FORCELOGOFF = EWX_LOGOFF | EWX_FORCE,
EWX_FORCEIFHUNGLOGOFF = EWX_LOGOFF | EWX_FORCEIFHUNG
}/// <summary>
/// Takes a snap shot of processes running on the computer, and searches for a
/// specified process name [name.exe].
/// </summary>
/// <param name="pname">Process name to be searched for.</param>
/// <returns>true if process found and false otherwise.</returns>
public static bool FindProcessByName(string pname)
{
int SnapShot = CreateToolhelp32Snapshot(0x00000002, 0); ProcessEntry32 pe32 = new ProcessEntry32(); pe32.dwSize = 296;
while(Process32Next(SnapShot, ref pe32) != 0)
{string xname = pe32.szExeFile.ToString();
if(pname.CompareTo(xname) == 0)
{
CloseHandle(SnapShot);
return true;
}
}
CloseHandle(SnapShot);
return false;
} public static int GetNumberRunning(string pname)
{
int ProcCount = 0;
int SnapShot = CreateToolhelp32Snapshot(0x00000002, 0);
ProcessEntry32 pe32 = new ProcessEntry32();
pe32.dwSize = 296;
while(Process32Next(SnapShot, ref pe32) != 0)
{
string xname = pe32.szExeFile.ToString();
if(pname.CompareTo(xname) == 0)
{
ProcCount++;
}
}
CloseHandle(SnapShot);
return ProcCount;
} public static uint GetProcessIdForProcess(string pname)
{
uint procid = 0;
int SnapShot = CreateToolhelp32Snapshot(0x00000002, 0);
ProcessEntry32 pe32 = new ProcessEntry32();
pe32.dwSize = 296;
while(Process32Next(SnapShot, ref pe32) != 0)
{
string xname = pe32.szExeFile.ToString();
if(pname.CompareTo(xname) == 0)
{
procid = pe32.th32ProcessID;
break;
}
}
CloseHandle(SnapShot);
return procid;
} public static int GetMemoryUsageForProcess(string pname)
{
try
{
return GetMemoryUsageForProcess(GetProcessIdForProcess(pname));
}
catch
{
return -1;
}
} public static int GetMemoryUsageForProcess(uint pid)
{
int mem = 0;
int pHandle = OpenProcess(0x0400 | 0x0010, 0, pid);
PROCESS_MEMORY_COUNTERS pmc = new PROCESS_MEMORY_COUNTERS();
if(GetProcessMemoryInfo(pHandle, pmc, 40) != 0) mem = pmc.WorkingSetSize;
CloseHandle(pHandle);
return mem;
} public static bool IsMoreThanOneRunning(string pname)
{
if(GetNumberRunning(pname) > 1) return true;
else return false;
} public static void ExitSystem(EWX_ENUM exval)
{
TOKEN_PRIVILEGES tp = new TOKEN_PRIVILEGES();
TOKEN_PRIVILEGES tpOld = new TOKEN_PRIVILEGES();
LUID luid = new LUID();LookupPrivilegeValue(null, "SeShutdownPrivilege",ref luid);
int processHandle = GetCurrentProcess();
int TokenHandle = 0;
OpenProcessToken(processHandle, 0x00000020 | 0x00000008, ref TokenHandle);
tp.PriviledgeCount = 1; tp.Attributes = 0x00000002; tp.Luid = luid;
int tpsz = Marshal.SizeOf(tp); tpsz = AdjustTokenPrivileges(TokenHandle, 0, ref tp, tpsz,0,0); ExitWindowsEx((int)exval, 0); } } } .
- Follow-Ups:
- Re: how to reboot, or shutdown the computer ?
- From: Willy Denoyette [MVP]
- Re: how to reboot, or shutdown the computer ?
- References:
- how to reboot, or shutdown the computer ?
- From: Herbert VON GRÜNENWALD
- Re: how to reboot, or shutdown the computer ?
- From: Lebesgue
- Re: how to reboot, or shutdown the computer ?
- From: Willy Denoyette [MVP]
- how to reboot, or shutdown the computer ?
- Prev by Date: Re: OCX from a class library?
- Next by Date: Re: parsing the output using string.split and current locale..
- Previous by thread: Re: how to reboot, or shutdown the computer ?
- Next by thread: Re: how to reboot, or shutdown the computer ?
- Index(es):
Relevant Pages
|
|