private void TestAttr() { short i1, i2; short wStream = 0; m_head.GetAttributeCount(wStream, out i1); m_head.SetAttribute(wStream, "asdf", AttrDataType.DWORD, BitConverter.GetBytes(1234), 4); m_head.SetAttribute(wStream, "fdsa", AttrDataType.DWORD, BitConverter.GetBytes(4321), 4); m_head.GetAttributeCount(wStream, out i2); Debug.Assert(i2 - i1 == 2); short pLen = 0; AttrDataType pType; m_head.GetAttributeByName(ref wStream, "asdf", out pType, null, ref pLen); byte [] b = new byte[pLen]; m_head.GetAttributeByName(ref wStream, "asdf", out pType, b, ref pLen); Debug.Assert(BitConverter.ToInt32(b, 0) == 1234); short sNameLen = 0; m_head.GetAttributeByIndex((short)(i1 + 1), ref wStream, null, ref sNameLen, out pType, null, ref pLen); StringBuilder sb = new StringBuilder(sNameLen); byte [] b2 = new byte[pLen]; m_head.GetAttributeByIndex((short)(i1 + 1), ref wStream, sb, ref sNameLen, out pType, b2, ref pLen); Debug.Assert(sb.ToString() == "fdsa" && pType == AttrDataType.DWORD && BitConverter.ToInt32(b2, 0) == 4321); }
/// <summary> /// Get the attribute by index of specific stream. Wraps Number of markers in the header info object. Wraps IWMHeaderInfo.GetAttibuteByIndex /// </summary> /// <param name="StreamNumber">Stream number. Zero return a file level atrtibute</param> /// <param name="index">Attribute index</param> /// <returns>Desired attribute <see cref="Yeti.WMFSdk.WM_Attr"/></returns> public WM_Attr GetAttribute(int StreamNumber, int index) { WMT_ATTR_DATATYPE type; StringBuilder name; object obj; ushort stream = (ushort)StreamNumber; ushort namelen = 0; ushort datalen = 0; m_HeaderInfo.GetAttributeByIndex((ushort)index, ref stream, null, ref namelen, out type, IntPtr.Zero, ref datalen); name = new StringBuilder(namelen); switch (type) { case WMT_ATTR_DATATYPE.WMT_TYPE_BOOL: case WMT_ATTR_DATATYPE.WMT_TYPE_DWORD: obj = (uint)0; break; case WMT_ATTR_DATATYPE.WMT_TYPE_GUID: obj = Guid.NewGuid(); break; case WMT_ATTR_DATATYPE.WMT_TYPE_QWORD: obj = (ulong)0; break; case WMT_ATTR_DATATYPE.WMT_TYPE_WORD: obj = (ushort)0; break; case WMT_ATTR_DATATYPE.WMT_TYPE_STRING: case WMT_ATTR_DATATYPE.WMT_TYPE_BINARY: obj = new byte[datalen]; break; default: throw new InvalidOperationException(string.Format("Not supported data type: {0}", type.ToString())); } GCHandle h = GCHandle.Alloc(obj, GCHandleType.Pinned); try { IntPtr ptr = h.AddrOfPinnedObject(); m_HeaderInfo.GetAttributeByIndex((ushort)index, ref stream, name, ref namelen, out type, ptr, ref datalen); switch (type) { case WMT_ATTR_DATATYPE.WMT_TYPE_STRING: obj = Marshal.PtrToStringUni(ptr); break; case WMT_ATTR_DATATYPE.WMT_TYPE_BOOL: obj = ((uint)obj != 0); break; } } finally { h.Free(); } return(new WM_Attr(name.ToString(), type, obj)); }
//------------------------------------------------------------------------------ // Name: CWMVCopy::CopyAttribute() // Desc: Copies all the header attributes from the reader file to the writer. //------------------------------------------------------------------------------ protected void CopyAttribute() { short wStreamNumber; short wAttributeCount; AttrDataType enumType; short cbNameLength = 0; short cbValueLength = 0; StringBuilder pwszName = null; byte[] pbValue = null; for (int i = 0; i <= m_dwStreamCount; i++) { if (i == m_dwStreamCount) { // // When the stream number is 0, the attribute is a file-level attribute // wStreamNumber = 0; } else { wStreamNumber = m_pwStreamNumber[i]; } // // Get the attribute count // m_pReaderHeaderInfo.GetAttributeCount(wStreamNumber, out wAttributeCount); // // Copy all attributes of this stream // for (short j = 0; j < wAttributeCount; j++) { // // Get the attribute name and value length // m_pReaderHeaderInfo.GetAttributeByIndex(j, ref wStreamNumber, null, ref cbNameLength, out enumType, null, ref cbValueLength); pwszName = new StringBuilder(cbNameLength); pbValue = new byte[cbValueLength]; // // Get the attribute name and value // m_pReaderHeaderInfo.GetAttributeByIndex(j, ref wStreamNumber, pwszName, ref cbNameLength, out enumType, pbValue, ref cbValueLength); // // Set the attribute for the writer // try { m_pWriterHeaderInfo.SetAttribute(wStreamNumber, pwszName.ToString(), enumType, pbValue, cbValueLength); } catch (Exception e) { int hr = Marshal.GetHRForException(e); if (E_InvalidArgument == hr) { // // Some attributes are read-only; we cannot set them. // They'll be set automatically by the writer. // } else { throw; } } } } }