Exemplo n.º 1
0
        //determines whether it's object.
        private static Boolean IsObject(Int32 pID, IntPtr remoteHandle, IEnumerable <String> fileNames)
        {
            NativeNormalHandle handle = null;

            try
            {
                handle = HandleCopy(pID, remoteHandle);
                if (handle != null && IsFileOrDirectoryHandle(handle.DangerousGetHandle()))
                {
                    var fileName = GetFileNameFromHandle(handle.DangerousGetHandle());
                    foreach (var item in fileNames)
                    {
                        if (item.Equals(fileName))
                        {
                            return(true);
                        }
                    }
                }
                return(false);
            }
            finally
            {
                if (handle != null)
                {
                    handle.Close();
                }
            }
        }
Exemplo n.º 2
0
 public static Boolean TryOpenProcess(Int32 pID, out NativeNormalHandle handle, Boolean inheritHandle = false)
 {
     handle = OpenProcess(ProcessAccessFlags.All, inheritHandle, pID);
     if (handle.IsInvalid)
     {
         handle = null;
         return(false);
     }
     return(true);
 }
Exemplo n.º 3
0
 public static Boolean TryDuplicateHandle(NativeNormalHandle sourceProcessHandle, IntPtr sourceHandle, IntPtr targetProcessHandle, out NativeNormalHandle targetHandle, Boolean removeSourceHandle = false)
 {
     if (!DuplicateHandle(sourceProcessHandle.DangerousGetHandle(), sourceHandle, targetProcessHandle, out targetHandle, 0, false, DuplicateOptions.DuplicateSameAccess | (removeSourceHandle ? DuplicateOptions.DuplicateCloseSource : DuplicateOptions.None)))
     {
         if (targetHandle.IsInvalid)
         {
             targetHandle.Close();
         }
         targetHandle = null;
         return(false);
     }
     return(true);
 }
Exemplo n.º 4
0
        private static NativeNormalHandle HandleCopy(Int32 pID, IntPtr remoteHandle, Boolean removeSourceHandle = false)
        {
            NativeNormalHandle pHandle = null;

            try
            {
                if (NativeAPI.TryOpenProcess(pID, out pHandle) && NativeAPI.TryDuplicateHandle(pHandle, remoteHandle, NativeAPI.GetCurrentProcess(), out var lHandle, removeSourceHandle))
                {
                    return(lHandle);
                }
                return(null);
            }

            finally
            {
                if (pHandle != null)
                {
                    pHandle.Close();
                }
            }
        }
Exemplo n.º 5
0
        private static Boolean TryReleaseHandle(Int32 pID, IntPtr remoteHandle)
        {
            NativeNormalHandle handle = null;

            try
            {
                handle = HandleCopy(pID, remoteHandle, true);
                if (handle != null)
                {
                    return(true);
                }
                return(false);
            }
            finally
            {
                if (handle != null)
                {
                    handle.Close();
                }
            }
        }
Exemplo n.º 6
0
 private static extern Boolean DuplicateHandle(IntPtr hSourceProcessHandle, IntPtr hSourceHandle, IntPtr hTargetProcessHandle, out NativeNormalHandle lpTargetHandle, UInt32 dwDesiredAccess, Boolean bInheritHandle, DuplicateOptions dwOptions);