internal static void CloseInvalidOutSafeHandle(SafeHandle handle) { if (handle != null) { handle.SetHandleAsInvalid(); } }
static void CloseInvalidOutSafeHandleCritical(SafeHandle handle) { // Workaround for 64-bit CLR if (handle != null) { Fx.Assert(handle.IsInvalid, "CloseInvalidOutSafeHandle called with a valid handle!"); // Calls SuppressFinalize. handle.SetHandleAsInvalid(); } }
static void CloseInvalidOutSafeHandleCritical(SafeHandle handle) { // Workaround for 64-bit CLR bug VSWhidbey 546830 - sometimes invalid SafeHandles come back null. if (handle != null) { Fx.Assert(handle.IsInvalid, "CloseInvalidOutSafeHandle called with a valid handle!"); // Calls SuppressFinalize. handle.SetHandleAsInvalid(); } }
[SuppressMessage(FxCop.Category.Security, FxCop.Rule.TransparentMethodsMustNotReferenceCriticalCode)] // we got APTCA approval with no requirement to fix this transparency warning internal static void CloseInvalidOutSafeHandle(SafeHandle handle) { // Workaround for 64-bit CLR if (handle != null) { #pragma warning disable 618 Fx.Assert(handle.IsInvalid, "CloseInvalidOutSafeHandle called with a valid handle!"); #pragma warning restore 618 // Calls SuppressFinalize. handle.SetHandleAsInvalid(); } }
[SuppressMessage(FxCop.Category.Security, FxCop.Rule.TransparentMethodsMustNotReferenceCriticalCode)] // we got APTCA approval with no requirement to fix this transparency warning internal static void CloseInvalidOutSafeHandle(SafeHandle handle) { // Workaround for 64-bit CLR bug VSWhidbey 546830 - sometimes invalid SafeHandles come back null. if (handle != null) { #pragma warning disable 618 Fx.Assert(handle.IsInvalid, "CloseInvalidOutSafeHandle called with a valid handle!"); #pragma warning restore 618 // Calls SuppressFinalize. handle.SetHandleAsInvalid(); } }
private unsafe static int QueryContextAttributes_SECURITY( SafeDeleteContext phContext, Interop.Secur32.ContextAttribute contextAttribute, byte* buffer, SafeHandle refHandle) { int status = (int)Interop.SecurityStatus.InvalidHandle; try { bool ignore = false; phContext.DangerousAddRef(ref ignore); status = Interop.Secur32.QueryContextAttributesW(ref phContext._handle, contextAttribute, buffer); } finally { phContext.DangerousRelease(); } if (status == 0 && refHandle != null) { if (refHandle is SafeFreeContextBuffer) { ((SafeFreeContextBuffer)refHandle).Set(*(IntPtr*)buffer); } else { ((SafeFreeCertContext)refHandle).Set(*(IntPtr*)buffer); } } if (status != 0 && refHandle != null) { refHandle.SetHandleAsInvalid(); } return status; }
public static unsafe int QueryContextAttributes(SafeDeleteContext phContext, ContextAttribute contextAttribute, byte* buffer, SafeHandle refHandle) { int num = -2146893055; bool success = false; RuntimeHelpers.PrepareConstrainedRegions(); try { phContext.DangerousAddRef(ref success); } catch (Exception exception) { if (success) { phContext.DangerousRelease(); success = false; } if (!(exception is ObjectDisposedException)) { throw; } } finally { if (success) { num = QueryContextAttributesW(ref phContext._handle, contextAttribute, (void*) buffer); phContext.DangerousRelease(); } if ((num == 0) && (refHandle != null)) { if (refHandle is SafeFreeContextBuffer) { if (contextAttribute == ContextAttribute.SessionKey) { IntPtr ptr = Marshal.ReadIntPtr(new IntPtr((void*) buffer), SecPkgContext_SessionKey.SessionkeyOffset); ((SafeFreeContextBuffer) refHandle).Set(ptr); } else { ((SafeFreeContextBuffer) refHandle).Set(*((IntPtr*) buffer)); } } else { ((SafeFreeCertContext) refHandle).Set(*((IntPtr*) buffer)); } } if ((num != 0) && (refHandle != null)) { refHandle.SetHandleAsInvalid(); } } return num; }
/// <summary> /// Initialises the device /// </summary> /// <param name="strPath">Path to the device</param> private void Initialise(string strPath) { // Create the file from the device path m_hHandle = Kernel32.CreateFile(strPath, Kernel32.GENERIC_READ | Kernel32.GENERIC_WRITE, 0, IntPtr.Zero, Kernel32.OPEN_EXISTING, Kernel32.FILE_FLAG_OVERLAPPED, 0); if (!m_hHandle.IsInvalid) // if the open worked... { IntPtr lpData; if (Hid.HidD_GetPreparsedData(m_hHandle, out lpData)) // get windows to read the device data into an internal buffer { try { HIDP_CAPS oCaps; Hid.HidP_GetCaps(lpData, out oCaps); // extract the device capabilities from the internal buffer m_nInputReportLength = oCaps.InputReportByteLength; // get the input... m_nOutputReportLength = oCaps.OutputReportByteLength; // ... and output report lengths //m_oFile = new FileStream(m_hHandle, FileAccess.Read | FileAccess.Write, true, m_nInputReportLength, true); // wrap the file handle in a .Net file stream m_oFile = new FileStream(new SafeFileHandle(m_hHandle.DangerousGetHandle(), false), FileAccess.Read | FileAccess.Write, m_nInputReportLength, true); // wrap the file handle in a .Net file stream BeginAsyncRead(); // kick off the first asynchronous read } finally { Hid.HidD_FreePreparsedData(lpData); // before we quit the funtion, we must free the internal buffer reserved in GetPreparsedData } } else // GetPreparsedData failed? Chuck an exception { throw HIDDeviceException.GenerateWithWinError("GetPreparsedData failed"); } } else // File open failed? Chuck an exception { m_hHandle.SetHandleAsInvalid(); throw HIDDeviceException.GenerateWithWinError("Failed to create device file"); } }
private unsafe static int QueryContextAttributes_SECURITY( SafeDeleteContext phContext, ContextAttribute contextAttribute, byte* buffer, SafeHandle refHandle) { int status = (int)SecurityStatus.InvalidHandle; bool b = false; // We don't want to be interrupted by thread abort exceptions or unexpected out-of-memory errors failing to jit // one of the following methods. So run within a CER non-interruptible block. RuntimeHelpers.PrepareConstrainedRegions(); try { phContext.DangerousAddRef(ref b); } catch(Exception e) { if (b) { phContext.DangerousRelease(); b = false; } if (!(e is ObjectDisposedException)) throw; } finally { if (b) { status = UnsafeNclNativeMethods.SafeNetHandles_SECURITY.QueryContextAttributesW(ref phContext._handle, contextAttribute, buffer); phContext.DangerousRelease(); } if (status == 0 && refHandle != null) { if (refHandle is SafeFreeContextBuffer) { ((SafeFreeContextBuffer)refHandle).Set(*(IntPtr*)buffer); } else { ((SafeFreeCertContext)refHandle).Set(*(IntPtr*)buffer); } } if (status != 0 && refHandle != null) { refHandle.SetHandleAsInvalid(); } } return status; }
// // After PInvoke call the method will fix the refHandle.handle with the returned value. // The caller is responsible for creating a correct SafeHandle template or null can be passed if no handle is returned. // // This method is run as non-interruptible. // public static unsafe int QueryContextAttributes(SafeDeleteContext phContext, ContextAttribute contextAttribute, byte* buffer, SafeHandle refHandle) { int status = (int)SecurityStatus.InvalidHandle; bool b = false; // We don't want to be interrupted by thread abort exceptions or unexpected out-of-memory errors failing to jit // one of the following methods. So run within a CER non-interruptible block. RuntimeHelpers.PrepareConstrainedRegions(); try { phContext.DangerousAddRef(ref b); } catch (Exception e) { if (System.Runtime.Fx.IsFatal(e)) throw; if (b) { phContext.DangerousRelease(); b = false; } if (!(e is ObjectDisposedException)) throw; } finally { if (b) { // PreSharp Bug: Call 'Marshal.GetLastWin32Error' or 'Marshal.GetHRForLastWin32Error' before any other interop call. #pragma warning suppress 56523 // The API does not set Win32 Last Error. The API returns a error code. status = SafeFreeContextBuffer.QueryContextAttributesW(ref phContext._handle, contextAttribute, buffer); phContext.DangerousRelease(); } if (status == 0 && refHandle != null) { if (refHandle is SafeFreeContextBuffer) { if (contextAttribute == ContextAttribute.SessionKey) { IntPtr keyPtr = Marshal.ReadIntPtr(new IntPtr(buffer), SecPkgContext_SessionKey.SessionkeyOffset); ((SafeFreeContextBuffer)refHandle).Set(keyPtr); } else { ((SafeFreeContextBuffer)refHandle).Set(*(IntPtr*)buffer); } } else { ((SafeFreeCertContext)refHandle).Set(*(IntPtr*)buffer); } } if (status != 0 && refHandle != null) { refHandle.SetHandleAsInvalid(); } } return status; }
private static unsafe int QueryContextAttributes_SCHANNEL(SafeDeleteContext phContext, ContextAttribute contextAttribute, byte* buffer, SafeHandle refHandle) { int num = -2146893055; bool success = false; RuntimeHelpers.PrepareConstrainedRegions(); try { phContext.DangerousAddRef(ref success); } catch (Exception exception) { if (success) { phContext.DangerousRelease(); success = false; } if (!(exception is ObjectDisposedException)) { throw; } } finally { if (success) { num = UnsafeNclNativeMethods.SafeNetHandles_SCHANNEL.QueryContextAttributesA(ref phContext._handle, contextAttribute, (void*) buffer); phContext.DangerousRelease(); } if ((num == 0) && (refHandle != null)) { if (refHandle is SafeFreeContextBuffer) { ((SafeFreeContextBuffer) refHandle).Set(*((IntPtr*) buffer)); } else { ((SafeFreeCertContext) refHandle).Set(*((IntPtr*) buffer)); } } if ((num != 0) && (refHandle != null)) { refHandle.SetHandleAsInvalid(); } } return num; }