Nexor
Hectopat
- Katılım
- 30 Haziran 2020
- Mesajlar
- 238
- Çözümler
- 4
Daha fazla
- Cinsiyet
- Erkek
Kodu biraz kendine göre düzenlemen lazım.
C#:
/********************************************************/
/* */
/* Snaphot.cs */
/* */
/* Burak Akat (Nexor) */
/* 25.09.2021 */
/* Copyright (c) 2021 */
/* */
/* */
/* Windows apisini kullanarak 32 ve 64 bit */
/* uygulamaların işlemlerine izin verir */
/* */
/********************************************************/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace _808_Rpg.Utils
{
class Snapshot
{
[Flags]
private enum SnapshotFlags : uint
{
HeapList = 0x00000001,
Process = 0x00000002,
Thread = 0x00000004,
Module = 0x00000008,
Module32 = 0x00000010,
Inherit = 0x80000000,
All = 0x0000001F,
NoHeaps = 0x40000000
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct PROCESSENTRY32
{
const int MAX_PATH = 260;
internal UInt32 dwSize;
internal UInt32 cntUsage;
internal UInt32 th32ProcessID;
internal IntPtr th32DefaultHeapID;
internal UInt32 th32ModuleID;
internal UInt32 cntThreads;
internal UInt32 th32ParentProcessID;
internal Int32 pcPriClassBase;
internal UInt32 dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
internal string szExeFile;
}
[Flags]
public enum ProcessAccessFlags : uint
{
All = 0x001F0FFF,
Terminate = 0x00000001,
CreateThread = 0x00000002,
VirtualMemoryOperation = 0x00000008,
VirtualMemoryRead = 0x00000010,
VirtualMemoryWrite = 0x00000020,
DuplicateHandle = 0x00000040,
CreateProcess = 0x000000080,
SetQuota = 0x00000100,
SetInformation = 0x00000200,
QueryInformation = 0x00000400,
QueryLimitedInformation = 0x00001000,
Synchronize = 0x00100000
}
[DllImport("kernel32", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
static extern IntPtr CreateToolhelp32Snapshot([In] UInt32 dwFlags, [In] UInt32 th32ProcessID);
[DllImport("kernel32", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
static extern bool Process32First([In] IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
[DllImport("kernel32", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
static extern bool Process32Next([In] IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
[DllImport("kernel32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle([In] IntPtr hObject);
[DllImport("Kernel32.dll")]
static extern uint QueryFullProcessImageName(IntPtr hProcess, uint flags, StringBuilder text, out uint size);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr hWndChildAfter, string className, string windowTitle);
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);
[DllImport("psapi.dll")]
static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In][MarshalAs(UnmanagedType.U4)] int nSize);
/// <summary>
/// Bir uygulamanın çalışıp çalışmadığını kontrol eder.
/// (Islem Modu [0: gta_sa.exe] [1: GTA:SA:MP])
/// </summary>
public static List<Exe_Info> Check_Process(int islem_modu, params string[] islem_adi)
{
List<Exe_Info> veriler = new List<Exe_Info>();
IntPtr handleToSnapshot = IntPtr.Zero;
try
{
PROCESSENTRY32 procEntry = new PROCESSENTRY32();
procEntry.dwSize = (UInt32)Marshal.SizeOf(typeof(PROCESSENTRY32));
handleToSnapshot = CreateToolhelp32Snapshot((uint)SnapshotFlags.Process, 0);
if (Process32First(handleToSnapshot, ref procEntry))
{
do
{
if (islem_modu == 0)
{
for(int i = 0; i < islem_adi.Length; i++)
{
if (procEntry.szExeFile == islem_adi[i])
{
Exe_Info ekle = new Exe_Info(Convert.ToInt32(procEntry.th32ProcessID), Get_Exe_Path(Convert.ToInt32(procEntry.th32ProcessID)));
veriler.Add(ekle);
}
}
}
else
{
for (int i = 0; i < islem_adi.Length; i++)
{
IntPtr hwnd = IntPtr.Zero;
do
{
uint pid = 0;
hwnd = FindWindowEx(IntPtr.Zero, hwnd, null, islem_adi[i]);
GetWindowThreadProcessId(hwnd, out pid);
if (pid == procEntry.th32ProcessID)
{
if (hwnd != IntPtr.Zero)
{
Exe_Info ekle = new Exe_Info(Convert.ToInt32(procEntry.th32ProcessID), Get_Exe_Path(Convert.ToInt32(procEntry.th32ProcessID)));
veriler.Add(ekle);
}
}
}
while (hwnd != IntPtr.Zero);
}
}
}
while (Process32Next(handleToSnapshot, ref procEntry));
}
else
{
throw new ApplicationException(string.Format("Failed with win32 error code {0}", Marshal.GetLastWin32Error()));
}
}
catch (Exception ex)
{
throw new ApplicationException("Can't get the process.", ex);
}
finally
{
CloseHandle(handleToSnapshot);
}
return veriler;
}
/// <summary>
/// Arka planda çalışan tüm işlemleri alır.
/// </summary>
public static List<Exe_Info> Get_Process()
{
List<Exe_Info> veriler = new List<Exe_Info>();
IntPtr handleToSnapshot = IntPtr.Zero;
try
{
PROCESSENTRY32 procEntry = new PROCESSENTRY32();
procEntry.dwSize = (UInt32)Marshal.SizeOf(typeof(PROCESSENTRY32));
handleToSnapshot = CreateToolhelp32Snapshot((uint)SnapshotFlags.Process, 0);
if (Process32First(handleToSnapshot, ref procEntry))
{
do
{
IntPtr app = OpenProcess((uint)ProcessAccessFlags.QueryInformation, false, Convert.ToInt32(procEntry.th32ProcessID));
if (app != IntPtr.Zero)
{
StringBuilder konum = new StringBuilder(256);
GetModuleFileNameEx(app, IntPtr.Zero, konum, konum.Capacity);
// Aynı dosyadan varsa atla
int it = veriler.FindIndex(j => j.s_dizin == konum.ToString());
if(it != -1)
{
CloseHandle(app);
continue;
}
// Dosya büyükse atla
if (File.Exists(konum.ToString()))
{
FileInfo dosya = new FileInfo(konum.ToString());
if (((int)dosya.Length / 1024) >= (1024 * Anti_Cheat.Anti_Exe.max_dosya_boyutu))
{
CloseHandle(app);
continue;
}
}
Exe_Info ekle = new Exe_Info(Convert.ToInt32(procEntry.th32ProcessID), konum.ToString());
veriler.Add(ekle);
CloseHandle(app);
}
}
while (Process32Next(handleToSnapshot, ref procEntry));
}
else
{
throw new ApplicationException(string.Format("Failed with win32 error code {0}", Marshal.GetLastWin32Error()));
}
}
catch (Exception ex)
{
throw new ApplicationException("Can't get the process.", ex);
}
finally
{
CloseHandle(handleToSnapshot);
}
return veriler;
}
/// <summary>
/// Bir uygulamanın dosya konumunu alır.
/// </summary>
public static string Get_Exe_Path(int pid)
{
IntPtr app = OpenProcess((uint)ProcessAccessFlags.QueryInformation, false, pid);
if (app != IntPtr.Zero)
{
StringBuilder konum = new StringBuilder(256);
GetModuleFileNameEx(app, IntPtr.Zero, konum, konum.Capacity);
CloseHandle(app);
return konum.ToString();
}
return string.Empty;
}
/*
public static string Get_Exe_Path2(Process proc)
{
string pathToExe = string.Empty;
if (null != proc)
{
uint nChars = 256;
StringBuilder Buff = new StringBuilder((int)nChars);
uint success = QueryFullProcessImageName(proc.Handle, 0, Buff, out nChars);
if (0 != success)
{
pathToExe = Buff.ToString();
}
else
{
int error = Marshal.GetLastWin32Error();
pathToExe = ("Error = " + error + " when calling GetProcessImageFileName");
}
}
return pathToExe;
}
*/
}
public class Exe_Info
{
public Exe_Info(int pid, string dizin)
{
this.s_pid = pid;
this.s_dizin = dizin;
}
public int s_pid { get; set; }
public string s_dizin { get; set; }
}
}