نمایش نتایج 1 تا 4 از 4

نام تاپیک: چگونه مانع kill شدن برنامه مان شویم

  1. #1
    کاربر دائمی آواتار fjm11100
    تاریخ عضویت
    خرداد 1387
    محل زندگی
    تهران
    سن
    43
    پست
    658

    چگونه مانع kill شدن برنامه مان شویم

    شاید برای شما هم پیش آمده است که می خواهید یک پروسس را ببندید اما با پیام Unable to terminate process که میگه The operation could be completed. Access is denied روبرو شدید.
    حتما هم پیش آمده که بخواهید از بسته شدن برنامه تان جلوگیری کنید(البته اصلا توصیه نمی کنم بلاخره Admin باید بتونه برنامه را kill کنه)
    در زیر چگونگی انجام این کار تشریح شده است.
    ابتدا چند خط زیر را در قسمت using قرار بدهید.
    using System.Runtime.InteropServices;
    using System.Security;
    using System.Security.AccessControl;
    using System.Security.Principal;


    ما برای اینکار از advapi32.dll استفاده می کنیم که یکجورایی مدیر امنیتی اجرای برنامه هاست(Run as admin هم از گور همین بلند میشه)
    تکه کد زیر را در کلاس خودتان بنویسید.

    [DllImport("advapi32.dll", SetLastError = true)]
    static extern bool GetKernelObjectSecurity(IntPtr Handle, int securityInformation, [Out] byte[] pSecurityDescriptor,
    uint nLength, out uint lpnLengthNeeded);

    public static RawSecurityDescriptor GetProcessSecurityDescriptor(IntPtr processHandle)
    {
    const int DACL_SECURITY_INFORMATION = 0x00000004;
    byte[] psd = new byte[0];
    uint bufSizeNeeded;
    // Call with 0 size to obtain the actual size needed in bufSizeNeeded
    GetKernelObjectSecurity(processHandle, DACL_SECURITY_INFORMATION, psd, 0, out bufSizeNeeded);
    if (bufSizeNeeded < 0 || bufSizeNeeded > short.MaxValue)
    throw new Win32Exception();
    // Allocate the required bytes and obtain the DACL
    if (!GetKernelObjectSecurity(processHandle, DACL_SECURITY_INFORMATION,
    psd = new byte[bufSizeNeeded], bufSizeNeeded, out bufSizeNeeded))
    throw new Win32Exception();
    // Use the RawSecurityDescriptor class from System.Security.AccessControl to parse the bytes:
    return new RawSecurityDescriptor(psd, 0);
    }

    [DllImport("advapi32.dll", SetLastError = true)]
    static extern bool SetKernelObjectSecurity(IntPtr Handle, int securityInformation, [In] byte[] pSecurityDescriptor);

    public static void SetProcessSecurityDescriptor(IntPtr processHandle, RawSecurityDescriptor dacl)
    {
    const int DACL_SECURITY_INFORMATION = 0x00000004;
    byte[] rawsd = new byte[dacl.BinaryLength];
    dacl.GetBinaryForm(rawsd, 0);
    if (!SetKernelObjectSecurity(processHandle, DACL_SECURITY_INFORMATION, rawsd))
    throw new Win32Exception();
    }

    [DllImport("kernel32.dll")]
    public static extern IntPtr GetCurrentProcess();

    [Flags]
    public enum ProcessAccessRights
    {
    PROCESS_CREATE_PROCESS = 0x0080, // Required to create a process.
    PROCESS_CREATE_THREAD = 0x0002, // Required to create a thread.
    PROCESS_DUP_HANDLE = 0x0040, // Required to duplicate a handle using DuplicateHandle.
    PROCESS_QUERY_INFORMATION = 0x0400, // Required to retrieve certain information about a process, such as its token, exit code, and priority class (see OpenProcessToken, GetExitCodeProcess, GetPriorityClass, and IsProcessInJob).
    PROCESS_QUERY_LIMITED_INFORMATION = 0x1000, // Required to retrieve certain information about a process (see QueryFullProcessImageName). A handle that has the PROCESS_QUERY_INFORMATION access right is automatically granted PROCESS_QUERY_LIMITED_INFORMATION. Windows Server 2003 and Windows XP/2000: This access right is not supported.
    PROCESS_SET_INFORMATION = 0x0200, // Required to set certain information about a process, such as its priority class (see SetPriorityClass).
    PROCESS_SET_QUOTA = 0x0100, // Required to set memory limits using SetProcessWorkingSetSize.
    PROCESS_SUSPEND_RESUME = 0x0800, // Required to suspend or resume a process.
    PROCESS_TERMINATE = 0x0001, // Required to terminate a process using TerminateProcess.
    PROCESS_VM_OPERATION = 0x0008, // Required to perform an operation on the address space of a process (see VirtualProtectEx and WriteProcessMemory).
    PROCESS_VM_READ = 0x0010, // Required to read memory in a process using ReadProcessMemory.
    PROCESS_VM_WRITE = 0x0020, // Required to write to memory in a process using WriteProcessMemory.
    DELETE = 0x00010000, // Required to delete the object.
    READ_CONTROL = 0x00020000, // Required to read information in the security descriptor for the object, not including the information in the SACL. To read or write the SACL, you must request the ACCESS_SYSTEM_SECURITY access right. For more information, see SACL Access Right.
    SYNCHRONIZE = 0x00100000, // The right to use the object for synchronization. This enables a thread to wait until the object is in the signaled state.
    WRITE_DAC = 0x00040000, // Required to modify the DACL in the security descriptor for the object.
    WRITE_OWNER = 0x00080000, // Required to change the owner in the security descriptor for the object.
    STANDARD_RIGHTS_REQUIRED = 0x000f0000,
    PROCESS_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xFFF),// All possible access rights for a process object.
    }


    حال با بدست آوردن هندل پروسس خود بکمک Kernel32.dll و صدا زدن متد SetProcessSecurityDescriptor پروسس شما غیر قابل بستن می شود.
    کد زیر اینکار را می کند آنرا می توانید در load فرم و یا در رویداد کلیک یک دکمه بگذارید.

    // Get the current process handle
    IntPtr hProcess = GetCurrentProcess();
    // Read the DACL
    var dacl = GetProcessSecurityDescriptor(hProcess);
    // Insert the new ACE
    dacl.DiscretionaryAcl.InsertAce(
    0,
    new CommonAce(
    AceFlags.None,
    AceQualifier.AccessDenied,
    (int)ProcessAccessRights.PROCESS_ALL_ACCESS,
    new SecurityIdentifier(WellKnownSidType.WorldSid, null),
    false,
    null)
    );
    // Save the DACL
    SetProcessSecurityDescriptor(hProcess, dacl);


    حالا برنامه را از فولدر اجرا کنید و سعی کنید آنرا ببندید!

  2. #2
    کاربر دائمی آواتار sirvan-me
    تاریخ عضویت
    اردیبهشت 1389
    محل زندگی
    استان البرز
    پست
    251

    نقل قول: چگونه مانع kill شدن برنامه مان شویم

    ممنون جالب بود حالا اگه بخوایم پروسه رو ببنیدیم باید چه کار کنیم ؟ راهی نداره .... ؟!

  3. #3
    کاربر دائمی آواتار fjm11100
    تاریخ عضویت
    خرداد 1387
    محل زندگی
    تهران
    سن
    43
    پست
    658

    نقل قول: چگونه مانع kill شدن برنامه مان شویم

    توی خود برنامه یک دکمه برای close بزار مثل همون دکمه X بالای فرم

  4. #4
    کاربر دائمی آواتار Pedram_Parsian
    تاریخ عضویت
    مرداد 1392
    محل زندگی
    پشت کامپیوتر
    پست
    430

    نقل قول: چگونه مانع kill شدن برنامه مان شویم

    با تشکر از دوست عزیزمون ولی من همین کد رو روی برنامه ام قرار دادم و باز بسته شد..
    لطفا اگه میشه یه پروژه مثال برام قرار بدین...
    خیلی ضروری هست ... با تشکر فرررررراوووووان

تاپیک های مشابه

  1. پاسخ: 25
    آخرین پست: سه شنبه 07 آذر 1385, 08:11 صبح
  2. چگونه از اجرا شدن برنامه بوسیله فایل اجرایی آن جلو گیری کنیم ؟
    نوشته شده توسط dibaj_ir در بخش کامپوننت های سایر شرکت ها، و توسعه کامپوننت
    پاسخ: 4
    آخرین پست: دوشنبه 10 مهر 1385, 11:17 صبح
  3. مشکل در کامپایل شدن برنامه ها
    نوشته شده توسط Harry در بخش برنامه نویسی در Delphi
    پاسخ: 2
    آخرین پست: جمعه 07 آذر 1382, 16:12 عصر
  4. چگونه می توان نام برنامه را از ویندوز پاک کرد؟
    نوشته شده توسط منصور بزرگمهر در بخش VB.NET
    پاسخ: 1
    آخرین پست: سه شنبه 10 تیر 1382, 21:06 عصر

برچسب های این تاپیک

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •