本文整理汇总了C#中System.Runtime.InteropServices.SafeHandle.SetHandleAsInvalid方法的典型用法代码示例。如果您正苦于以下问题:C# SafeHandle.SetHandleAsInvalid方法的具体用法?C# SafeHandle.SetHandleAsInvalid怎么用?C# SafeHandle.SetHandleAsInvalid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Runtime.InteropServices.SafeHandle
的用法示例。
在下文中一共展示了SafeHandle.SetHandleAsInvalid方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CloseInvalidOutSafeHandle
internal static void CloseInvalidOutSafeHandle(SafeHandle handle)
{
if (handle != null)
{
handle.SetHandleAsInvalid();
}
}
示例2: CloseInvalidOutSafeHandleCritical
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();
}
}
示例3: CloseInvalidOutSafeHandle
[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();
}
}
示例4: QueryContextAttributes_SECURITY
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;
}
示例5: QueryContextAttributes
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;
}
示例6: Initialise
/// <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");
}
}
示例7: QueryContextAttributes_SECURITY
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;
}
示例8: QueryContextAttributes
//
// 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;
}
示例9: QueryContextAttributes_SCHANNEL
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;
}