Добрый день, необходимо чтоб ехе искал в процессе хендл, и убивал его. Знаю только название хендла у процесса. Подскажите, как можно сделать?
? int processHandle = Хендл процесса; foreach (Process process in Process.GetProcesses()) { if (process.Handle.ToInt32() == processHandle) process.Kill(); } C# int processHandle = Хендл процесса; foreach (Process process in Process.GetProcesses()) { if (process.Handle.ToInt32() == processHandle) process.Kill(); }
вы наверное не поняли про какой хендл я имею ввиду, вот пример --- Сообщение объединено с предыдущим 13 ноя 2021
Убить процесс можно не только по имени (даже если имя меняется постоянно) или хендлу, но и так же по свойствам его характеристики и даже по пути.
r3xq1, да мне не нужно убивать его, мне надо убить хендл в процессе, чтоб я мог переименовать этот файл, потому что без этого, пишет, что файл занят другим процессом, но убивать мне его нельзя
https://www.pinvoke.net/default.aspx/Kernel32/CloseHandle.html namespace KillHandle { using System; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using System.Security; internal static class NativeMethods { [DllImport("kernel32.dll", SetLastError = true)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] [SuppressUnmanagedCodeSecurity] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool CloseHandle(IntPtr hObject); } } C# namespace KillHandle { using System; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using System.Security; internal static class NativeMethods { [DllImport("kernel32.dll", SetLastError = true)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] [SuppressUnmanagedCodeSecurity] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool CloseHandle(IntPtr hObject); } } В коде где тебе нужно прописываешь: namespace KillHandle { using System; using System.IO; public static class Program { [STAThread] public static void Main() { Console.Title = "Kill Handle Process"; Console.Write("Введите Handle процесса: "); IntPtr name = ConvertStringToIntPtr(Console.ReadLine()); if (NativeMethods.CloseHandle(name)) { Console.WriteLine("Успешно!"); File.Move(@"D:\KillHandle\bin\Release\Test.txt", @"D:\KillHandle\bin\Release\Test2.txt"); } else { Console.WriteLine("Не смогли завершить!"); } Console.Read(); Console.Clear(); Main(); } public static IntPtr ConvertStringToIntPtr(string hexString) { // удаляем 0х для конвертации hexString = hexString.Replace("0x", ""); // конвертируем в 10-ую систему счисления, это необходимо для вызова конструктора структуры long decAgain = long.Parse(hexString, System.Globalization.NumberStyles.HexNumber); return new IntPtr(decAgain); } } } C# namespace KillHandle { using System; using System.IO; public static class Program { [STAThread] public static void Main() { Console.Title = "Kill Handle Process"; Console.Write("Введите Handle процесса: "); IntPtr name = ConvertStringToIntPtr(Console.ReadLine()); if (NativeMethods.CloseHandle(name)) { Console.WriteLine("Успешно!"); File.Move(@"D:\KillHandle\bin\Release\Test.txt", @"D:\KillHandle\bin\Release\Test2.txt"); } else { Console.WriteLine("Не смогли завершить!"); } Console.Read(); Console.Clear(); Main(); } public static IntPtr ConvertStringToIntPtr(string hexString) { // удаляем 0х для конвертации hexString = hexString.Replace("0x", ""); // конвертируем в 10-ую систему счисления, это необходимо для вызова конструктора структуры long decAgain = long.Parse(hexString, System.Globalization.NumberStyles.HexNumber); return new IntPtr(decAgain); } } } У меня был занят текстовый файл другим процессом, после завершения хендла, смог переименовать.
darknether, у тебя есть type, у тебя есть name, у тебя есть свойства файла процесса и расширение .tmp. Смотри по этим параметрам, находи хендл и закрывай.