private static IBuffer GetVisualPixelBuffer(IntPtr hEvent, IntPtr hMap, uint cbMap) { try { const int WAIT_OBJECT_0 = 0; const int WAIT_TIMEOUT = 0x00000102; uint waitRet = Win32Interop.WaitForSingleObject(hEvent, 5000); if (waitRet == WAIT_OBJECT_0) { IntPtr pbMap = Win32Interop.MapViewOfFile(hMap, (uint)Win32Interop.FileMapAccess.FileMapRead, 0, 0, IntPtr.Zero); if (pbMap != IntPtr.Zero) { byte[] managedBuffer = new byte[cbMap]; Marshal.Copy(pbMap, managedBuffer, 0, (int)cbMap); return(managedBuffer.AsBuffer()); // Note that the finally block still executes } else { return(null); } } else if (waitRet == WAIT_TIMEOUT) { throw new TimeoutException("RenderVisual Timed Out!"); } else { throw new InvalidOperationException($"RenderVisual event wait failed (0x{waitRet:x8}): {Marshal.GetLastWin32Error()}"); } } finally { Win32Interop.CloseHandle(hMap); Win32Interop.CloseHandle(hEvent); } }
public static int PInvokeTest() { Win32Interop.CloseHandle(IntPtr.Zero); return(Marshal.GetLastWin32Error()); }
private void VideoCleanup(IntPtr opaque) { myBitmap = null; Win32Interop.UnmapViewOfFile(opaque); Win32Interop.CloseHandle(myBitmapSectionPointer); }