private static object CreateInstanceFromFileInternal(string filePath, Guid classId, Guid iid) { IntPtr hModule, getClassObjectPtr, classFactoryPtr = IntPtr.Zero; IClassFactory classFactory = null; try { hModule = NativeMethods.LoadLibrary(filePath); if (hModule == IntPtr.Zero) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } getClassObjectPtr = NativeMethods.GetProcAddress(hModule, "DllGetClassObject"); if (getClassObjectPtr == IntPtr.Zero) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } DllGetClassObjectSig DllGetClassObject = (DllGetClassObjectSig)Marshal.GetDelegateForFunctionPointer(getClassObjectPtr, typeof(DllGetClassObjectSig)); HResult hr = DllGetClassObject(classId, typeof(IClassFactory).GUID, out classFactoryPtr); hr.ThrowExceptionOnError(); classFactory = (IClassFactory)Marshal.GetObjectForIUnknown(classFactoryPtr); object result; hr = classFactory.CreateInstance(null, iid, out result); hr.ThrowExceptionOnError(); return(result); } finally { if (classFactory != null) { int i = Marshal.ReleaseComObject(classFactory); classFactory = null; } if (classFactoryPtr != IntPtr.Zero) { int i = Marshal.Release(classFactoryPtr); classFactoryPtr = IntPtr.Zero; } } }
public override void Write(byte[] buffer, int offset, int count) { if (buffer == null) { throw new ArgumentNullException("value", "value is null"); } if (count < 0 || offset < 0) { throw new ArgumentException("offset or count is negative."); } if (offset + count > buffer.Length) { throw new IndexOutOfRangeException("The sum of offset and count is larger than the value length."); } byte[] tempBuffer; // If offset is zero, write directly from value if (offset == 0) { tempBuffer = buffer; } else { tempBuffer = new byte[count]; Buffer.BlockCopy(buffer, offset, tempBuffer, 0, count); } HResult hr = m_comStream.Write(tempBuffer, count, IntPtr.Zero); hr.ThrowExceptionOnError(); }
/// <summary> /// Writes asynchronously data to a byte stream /// </summary> /// <param name="byteStream">A valid IMFByteStream instance.</param> /// <param name="bufferPtr">A pointer designating the memory location that contain the data to write.</param> /// <param name="bufferSize">The maximum number of bytes to write to the byte stream.</param> /// <returns>A task that represents the asynchronous write operation. The task's result contain the number of bytes written.</returns> public static Task <int> WriteAsync(this IMFByteStream byteStream, IntPtr bufferPtr, int bufferSize) { if (byteStream == null) { throw new ArgumentNullException("byteStream"); } TaskCompletionSource <int> tcs = new TaskCompletionSource <int>(); HResult hrBegin = byteStream.BeginWrite(bufferPtr, bufferSize, new MFAsyncCallback((ar) => { int bytesWritten = 0; try { HResult hrEnd = byteStream.EndWrite(ar, out bytesWritten); hrEnd.ThrowExceptionOnError(); tcs.SetResult(bytesWritten); } catch (Exception ex) { tcs.SetException(ex); } }), null); hrBegin.ThrowExceptionOnError(); return(tcs.Task); }
private void UpdateCachedSTATSTG() { Misc.STATSTG stat; HResult hr = m_comStream.Stat(out stat, Misc.STATFLAG.NoName); hr.ThrowExceptionOnError(); m_STATSTG = stat; }
/// <summary> /// Populate the camera dropdown with all available cameras once it is opened. /// </summary> /// <param name="sender">Camera combo box.</param> /// <param name="e">Event arguments.</param> private void cameraComboBox_DropDownOpened(object sender, EventArgs e) { ComboBox comboBox = sender as ComboBox; videoDeviceNameIndexDictionary.Clear(); object selectedItem = comboBox.SelectedItem; comboBox.Items.Clear(); comboBox.Items.Add("None"); // create device enumerable IMFActivate[] devices; HResult hr = MF.EnumVideoDeviceSources(out devices); hr.ThrowExceptionOnError(); // loop through all devices and add them as a combo box item for (int i = 0; i < devices.Length; i++) { string friendlyName; hr = devices[i].GetAllocatedString(MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME, out friendlyName); hr.ThrowExceptionOnError(); int deviceNumber = 1; while (videoDeviceNameIndexDictionary.ContainsKey(friendlyName)) { deviceNumber++; friendlyName = friendlyName + " " + deviceNumber; } videoDeviceNameIndexDictionary.Add(friendlyName, i); comboBox.Items.Add(friendlyName); if (selectedItem.ToString() == friendlyName) { comboBox.SelectedItem = friendlyName; } } if (comboBox.SelectedItem == null) { comboBox.SelectedIndex = 0; } }
public override long Seek(long offset, SeekOrigin origin) { IntPtr resultPtr = Marshal.AllocCoTaskMem(sizeof(long)); long result; try { HResult hr = m_comStream.Seek(offset, origin, resultPtr); hr.ThrowExceptionOnError(); result = Marshal.ReadInt64(resultPtr); } finally { Marshal.FreeCoTaskMem(resultPtr); } return(result); }
public override int Read(byte[] buffer, int offset, int count) { if (buffer == null) { throw new ArgumentNullException("value", "value is null"); } if (count < 0 || offset < 0) { throw new ArgumentException("offset or count is negative."); } if (offset + count > buffer.Length) { throw new IndexOutOfRangeException("The sum of offset and count is larger than the value length."); } IntPtr byteReadPtr = Marshal.AllocCoTaskMem(sizeof(int)); int byteRead; // If offset is zero, read directly into value byte[] tempBuffer = (offset == 0) ? buffer : new byte[count]; try { HResult hr = m_comStream.Read(tempBuffer, count, byteReadPtr); hr.ThrowExceptionOnError(); byteRead = Marshal.ReadInt32(byteReadPtr); } finally { Marshal.FreeCoTaskMem(byteReadPtr); } if (offset != 0) { Buffer.BlockCopy(tempBuffer, 0, buffer, offset, byteRead); } return(byteRead); }
public override void Flush() { HResult hr = m_comStream.Commit(Misc.STGC.Default); hr.ThrowExceptionOnError(); }