//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(); } } }
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); }
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(); } } }
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(); } } }