1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
[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
}
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(int EWX_VALUE)
{
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(EWX_VALUE, 0);
} |
Partager