void ZelloPTTLib.IAudioStream.WriteSamples(object vData) { byte[] arr; try { System.Runtime.InteropServices.ComTypes.IStream comStream = vData as System.Runtime.InteropServices.ComTypes.IStream; if (comStream != null) { System.Runtime.InteropServices.ComTypes.STATSTG stg; comStream.Stat(out stg, 1 /*STATFLAG_NONAME*/); long lSize = stg.cbSize; sampleCount += Convert.ToInt32(lSize / 2); arr = new byte[lSize]; IntPtr pInt = (IntPtr)0; comStream.Read(arr, Convert.ToInt32(lSize), pInt); foreach (IAudioStreamSink sink in lstSinks) { sink.onAudioData(arr); } System.Runtime.InteropServices.Marshal.FinalReleaseComObject(comStream); comStream = null; } } catch (System.Exception _ex) { System.Diagnostics.Debug.WriteLine("Exception in AudioStreamImpl.WriteSamples : " + _ex.Message); } }
void readStream(System.Runtime.InteropServices.ComTypes.IStream ppstm, MemoryStream dest) { int size = 0; System.Runtime.InteropServices.ComTypes.STATSTG statstgstream; ppstm.Stat(out statstgstream, 1); size = (int)statstgstream.cbSize; IntPtr pm = new IntPtr(); byte[] buffer = new byte[1024]; int page = 1024; while (size > 0) { if (size < page) { page = size; } ppstm.Read(buffer, page, pm); dest.Write(buffer, 0, page); size -= page; } }
/// <summary> /// Initializes a new instance of the <see cref="ComStreamWrapper"/> class. /// </summary> /// <param name="istream">The istream to wrap.</param> /// <param name="isStreamOwner">If set to <c>true</c>, this instance should be the owner of the wrapped stream and thus is responsible for disposing it.</param> /// <exception cref="System.ArgumentNullException">istream is null.</exception> public ComStreamWrapper(System.Runtime.InteropServices.ComTypes.IStream istream, bool isStreamOwner) { _istream = istream ?? throw new ArgumentNullException(nameof(istream)); _isStreamOwner = isStreamOwner; _int64Ptr = Marshal.AllocCoTaskMem(8); _int32Ptr = Marshal.AllocCoTaskMem(4); _streamStatus = new System.Runtime.InteropServices.ComTypes.STATSTG(); _istream.Stat(out _streamStatus, STATFLAG.NONAME); }
private void button1_Click(object sender, EventArgs e) { if (istream != null) { System.Runtime.InteropServices.ComTypes.IStream i = (System.Runtime.InteropServices.ComTypes.IStream)istream.Stream; System.Runtime.InteropServices.ComTypes.STATSTG st; i.Stat(out st, 0); byte[] buffer = new byte[st.cbSize]; IntPtr ptr = Marshal.AllocHGlobal(sizeof(int)); i.Read(buffer, (int)st.cbSize, ptr); OnTestEvent("Size=" + st.cbSize.ToString() + "; read:" + Marshal.ReadIntPtr(ptr).ToString() + " bytes, starting:" + buffer[0].ToString("X")); } }
/// <summary> /// Initializes a new instance of the <see cref="ComStreamWrapper"/> class. /// </summary> /// <param name="istream">The istream to wrap.</param> /// <param name="isStreamOwner">If set to <c>true</c>, this instance should be the owner of the wrapped stream and thus is responsible for disposing it.</param> /// <exception cref="System.ArgumentNullException">istream is null.</exception> public ComStreamWrapper(System.Runtime.InteropServices.ComTypes.IStream istream, bool isStreamOwner) { if (null == istream) throw new ArgumentNullException("istream"); _isStreamOwner = isStreamOwner; _istream = istream; _int64Ptr = Marshal.AllocCoTaskMem(8); _int32Ptr = Marshal.AllocCoTaskMem(4); _streamStatus = new System.Runtime.InteropServices.ComTypes.STATSTG(); _istream.Stat(out _streamStatus, STATFLAG.NONAME); }