This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: paroxsitic on Aug 5th, 2011  |  syntax: C#  |  size: 2.22 KB  |  views: 267  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security.Permissions;
  6. using Microsoft.Win32;
  7. using System.Runtime.InteropServices;
  8. using System.Diagnostics;
  9. using System.Collections;
  10. using System.Threading;
  11. using System.Security.AccessControl;
  12. using System.Security.Principal;
  13. using Microsoft.Win32.SafeHandles;
  14. namespace EnumerateMutexACL
  15. {
  16.     class Win
  17.     {
  18.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  19.         public static extern SafeWaitHandle OpenMutex(UInt32 desiredAccess, bool inheritHandle, string name);
  20.  
  21.         [DllImport("kernel32.dll", SetLastError = true)]
  22.         [return: MarshalAs(UnmanagedType.Bool)]
  23.         public static extern bool DuplicateHandle(IntPtr hSourceProcessHandle,
  24.            IntPtr hSourceHandle, IntPtr hTargetProcessHandle, out IntPtr lpTargetHandle,
  25.            uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwOptions);
  26.  
  27.         [DllImport("kernel32.dll", SetLastError = true)]
  28.         [return: MarshalAs(UnmanagedType.Bool)]
  29.         public static extern bool CloseHandle(IntPtr hObject);
  30.     }
  31.  
  32.     class Program
  33.     {
  34.         static void Main(string[] args)
  35.         {
  36.             const UInt32 MUTEX_ALL_ACCESS = 0x1F0001;
  37.             const uint DUPLICATE_CLOSE_SOURCE = 0x00000001;
  38.  
  39.  
  40.             Process proc = Process.GetProcessesByName("notepad")[0];
  41.             IntPtr p = proc.Handle;
  42.  
  43.             SafeWaitHandle hMutex = Win.OpenMutex(MUTEX_ALL_ACCESS, false, "MSCTF.Asm.MutexDefault1");            
  44.             IntPtr pp = hMutex.DangerousGetHandle();
  45.  
  46.             IntPtr ppp = Process.GetCurrentProcess().Handle;
  47.             IntPtr pppp = IntPtr.Zero; // System.Diagnostics.Process.Start("calc").Handle;
  48.             Win.DuplicateHandle(p, pp, ppp, out pppp, MUTEX_ALL_ACCESS, false, DUPLICATE_CLOSE_SOURCE);
  49.             Win.CloseHandle(pppp);      
  50.  
  51.             if (Win.OpenMutex(MUTEX_ALL_ACCESS, false, "MSCTF.Asm.MutexDefault1").DangerousGetHandle() == IntPtr.Zero)
  52.                 Console.Write("Success");
  53.             else
  54.                 Console.Write("Failed");
  55.  
  56.  
  57.             Console.Read();
  58.  
  59.             return;
  60.         }
  61.     }
  62. }
clone this paste RAW Paste Data