/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public void Dispose() { if (!_memoryBlock.IsInvalid) { _memoryBlock.Dispose(); _memoryBlock = _invalidBlock; } }
public void SafeHGlobalHandleTest() { var ptr = Marshal.AllocHGlobal(5); var h = new SafeHGlobalHandle(ptr, 5); Assert.That(!h.IsClosed && !h.IsInvalid); Assert.That(h.Size, Is.EqualTo(5)); h.Dispose(); Assert.That(h.IsClosed && h.IsInvalid); h = Marshal.AllocHGlobal(5); Assert.That(!h.IsClosed && !h.IsInvalid); Assert.That(h, Is.Not.EqualTo(SafeHGlobalHandle.Null)); h.Dispose(); Assert.That(h.IsClosed && h.IsInvalid); }
public void SafeHGlobalHandleTest1() { var h = new SafeHGlobalHandle(Marshal.AllocHGlobal(5), 5); Assert.That(!h.IsClosed && !h.IsInvalid); Assert.That(h.Size, Is.EqualTo(5)); h.Dispose(); Assert.That(h.IsClosed && h.IsInvalid); }
public void SafeHGlobalHandleTest3() { var h = new SafeHGlobalHandle(new[] { (IntPtr)1, (IntPtr)2, (IntPtr)3, (IntPtr)4, (IntPtr)5 }); Assert.That(!h.IsClosed && !h.IsInvalid); Assert.That(h.Size, Is.EqualTo(5 * IntPtr.Size)); h.Dispose(); Assert.That(h.IsClosed && h.IsInvalid); }
public void SafeHGlobalHandleTest4() { var h = new SafeHGlobalHandle("0123456789"); Assert.That(!h.IsClosed && !h.IsInvalid); Assert.That(h.Size, Is.EqualTo(11 * Marshal.SystemDefaultCharSize)); h.Dispose(); Assert.That(h.IsClosed && h.IsInvalid); }
public void SafeHGlobalHandleTest2() { var h = new SafeHGlobalHandle(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }); Assert.That(!h.IsClosed && !h.IsInvalid); Assert.That(h.Size, Is.EqualTo(10)); h.Dispose(); Assert.That(h.IsClosed && h.IsInvalid); }
public void Dispose_NullPointerWorks() { //Act var target = new SafeHGlobalHandle(); target.Dispose(); //Assert - No real way to confirm the memory was released target.Pointer.Should().BeZero(); target.IsInvalid.Should().BeTrue(); }
public void FiberTest() { TestContext.WriteLine($"IsFiber:{IsThreadAFiber()}"); var id = FlsAlloc(Callback); var mem = new SafeHGlobalHandle(64); mem.Fill(1); Assert.That(FlsSetValue(id, (IntPtr)mem), Is.True); Assert.That(FlsGetValue(id), Is.EqualTo((IntPtr)mem)); Assert.That(FlsFree(id), Is.True); mem.Dispose(); }
public void Dispose_AttachedPointerWorks() { var ptr = AllocateMemory(10); //Act var target = new SafeHGlobalHandle(); target.Attach(ptr); target.Dispose(); //Assert - doesn't really confirm the memory was released target.Pointer.Should().BeZero(); target.IsInvalid.Should().BeTrue(); }
protected virtual void Dispose(bool disposing) { if (_disposedValue) { return; } if (disposing) { Handle.Dispose(); } _disposedValue = true; }
private SafeHGlobalHandle ToBgra() { SafeHGlobalHandle result = new SafeHGlobalHandle(_header.ImageWidth * _header.ImageHeight * 4); try { ToBgra(result); } catch { result.Dispose(); throw; } return(result); }
private static unsafe void BuildChain(IntPtr hChainEngine, IntPtr pCertContext, X509Certificate2Collection extraStore, OidCollection applicationPolicy, OidCollection certificatePolicy, X509RevocationMode revocationMode, X509RevocationFlag revocationFlag, DateTime verificationTime, TimeSpan timeout, out System.IdentityModel.SafeCertChainHandle ppChainContext) { System.IdentityModel.SafeCertStoreHandle hAdditionalStore = ExportToMemoryStore(extraStore, pCertContext); System.IdentityModel.CAPI.CERT_CHAIN_PARA pChainPara = new System.IdentityModel.CAPI.CERT_CHAIN_PARA { cbSize = (uint)Marshal.SizeOf(typeof(System.IdentityModel.CAPI.CERT_CHAIN_PARA)) }; SafeHGlobalHandle invalidHandle = SafeHGlobalHandle.InvalidHandle; SafeHGlobalHandle handle3 = SafeHGlobalHandle.InvalidHandle; try { if ((applicationPolicy != null) && (applicationPolicy.Count > 0)) { pChainPara.RequestedUsage.dwType = 0; pChainPara.RequestedUsage.Usage.cUsageIdentifier = (uint)applicationPolicy.Count; invalidHandle = CopyOidsToUnmanagedMemory(applicationPolicy); pChainPara.RequestedUsage.Usage.rgpszUsageIdentifier = invalidHandle.DangerousGetHandle(); } if ((certificatePolicy != null) && (certificatePolicy.Count > 0)) { pChainPara.RequestedIssuancePolicy.dwType = 0; pChainPara.RequestedIssuancePolicy.Usage.cUsageIdentifier = (uint)certificatePolicy.Count; handle3 = CopyOidsToUnmanagedMemory(certificatePolicy); pChainPara.RequestedIssuancePolicy.Usage.rgpszUsageIdentifier = handle3.DangerousGetHandle(); } pChainPara.dwUrlRetrievalTimeout = (uint)timeout.Milliseconds; System.Runtime.InteropServices.ComTypes.FILETIME pTime = new System.Runtime.InteropServices.ComTypes.FILETIME(); *((long *)&pTime) = verificationTime.ToFileTime(); uint dwFlags = MapRevocationFlags(revocationMode, revocationFlag); if (!System.IdentityModel.CAPI.CertGetCertificateChain(hChainEngine, pCertContext, ref pTime, hAdditionalStore, ref pChainPara, dwFlags, IntPtr.Zero, out ppChainContext)) { int hr = Marshal.GetLastWin32Error(); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(hr)); } } finally { if (invalidHandle != null) { invalidHandle.Dispose(); } if (handle3 != null) { handle3.Dispose(); } hAdditionalStore.Close(); } }
public static TimImage Read(TimHeader header, Stream input) { TimImageHeader imageHeader = TimImageHeader.Read(header, input); GLTexture layer = TimTexture2DReader.Read(header, imageHeader, input); //SafeHGlobalHandle pixels = new SafeHGlobalHandle(imageHeader.Width * imageHeader.Height * header.OutputPixelFormat.BitsPerPixel / 8); SafeHGlobalHandle pixels = new SafeHGlobalHandle(imageHeader.ContentSize); try { using (UnmanagedMemoryStream output = new UnmanagedMemoryStream(pixels, 0, (long)pixels.ByteLength, FileAccess.Write)) TimPixelFormatsConverter.Convert(header, imageHeader, input, output); } catch { pixels.Dispose(); throw; } return(new TimImage(imageHeader, layer)); }
public override void Dispose() { _handle.Dispose(); _patternHandle.Dispose(); }
public override void Dispose() { _searchTextUniPtr.Dispose(); base.Dispose(); }
public override void Dispose() { _patternPtr.Dispose(); base.Dispose(); }
static unsafe void BuildChain(IntPtr hChainEngine, IntPtr pCertContext, X509Certificate2Collection extraStore, OidCollection applicationPolicy, OidCollection certificatePolicy, X509RevocationMode revocationMode, X509RevocationFlag revocationFlag, DateTime verificationTime, TimeSpan timeout, out SafeCertChainHandle ppChainContext) { SafeCertStoreHandle hCertStore = ExportToMemoryStore(extraStore, pCertContext); CAPI.CERT_CHAIN_PARA ChainPara = new CAPI.CERT_CHAIN_PARA(); ChainPara.cbSize = (uint)Marshal.SizeOf(typeof(CAPI.CERT_CHAIN_PARA)); // Application policy SafeHGlobalHandle applicationPolicyHandle = SafeHGlobalHandle.InvalidHandle; SafeHGlobalHandle certificatePolicyHandle = SafeHGlobalHandle.InvalidHandle; try { if (applicationPolicy != null && applicationPolicy.Count > 0) { ChainPara.RequestedUsage.dwType = CAPI.USAGE_MATCH_TYPE_AND; ChainPara.RequestedUsage.Usage.cUsageIdentifier = (uint)applicationPolicy.Count; applicationPolicyHandle = CopyOidsToUnmanagedMemory(applicationPolicy); ChainPara.RequestedUsage.Usage.rgpszUsageIdentifier = applicationPolicyHandle.DangerousGetHandle(); } // Certificate policy if (certificatePolicy != null && certificatePolicy.Count > 0) { ChainPara.RequestedIssuancePolicy.dwType = CAPI.USAGE_MATCH_TYPE_AND; ChainPara.RequestedIssuancePolicy.Usage.cUsageIdentifier = (uint)certificatePolicy.Count; certificatePolicyHandle = CopyOidsToUnmanagedMemory(certificatePolicy); ChainPara.RequestedIssuancePolicy.Usage.rgpszUsageIdentifier = certificatePolicyHandle.DangerousGetHandle(); } ChainPara.dwUrlRetrievalTimeout = (uint)timeout.Milliseconds; FILETIME ft = new FILETIME(); *((long *)&ft) = verificationTime.ToFileTime(); uint flags = MapRevocationFlags(revocationMode, revocationFlag); // Build the chain. if (!CAPI.CertGetCertificateChain(hChainEngine, pCertContext, ref ft, hCertStore, ref ChainPara, flags, IntPtr.Zero, out ppChainContext)) { int error = Marshal.GetLastWin32Error(); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(error)); } } finally { if (applicationPolicyHandle != null) { applicationPolicyHandle.Dispose(); } if (certificatePolicyHandle != null) { certificatePolicyHandle.Dispose(); } hCertStore.Close(); } }