/// <summary> /// Retrieves a string from a header /// </summary> /// <param name="wma">WMA file instance being edited</param> private static string GetStringTag(Win32.IWMHeaderInfo3 wma, string attribute) { ushort stream = 0; // Stream number IntPtr ptData; // Data buffer pointer ushort cbData = 0; // Length of data buffer Win32.WMT_ATTR_DATATYPE type; // Attribute type // Determine the size of the buffer we need to allocate and then go get the attribute string ATTR = attribute + "\0"; try { wma.GetAttributeByName(ref stream, ATTR, out type, IntPtr.Zero, ref cbData); } catch { return(null); } if (type != Win32.WMT_ATTR_DATATYPE.WMT_TYPE_STRING) { return(null); } ptData = Marshal.AllocCoTaskMem((int)cbData); try { wma.GetAttributeByName(ref stream, ATTR, out type, ptData, ref cbData); return(Marshal.PtrToStringUni(ptData)); } finally { Marshal.FreeCoTaskMem(ptData); } // Release allocated memory }
/// <summary> /// Retrieves the Duration from a header /// </summary> /// <param name="wma">WMA file instance being edited</param> private static long GetTrackDuration(Win32.IWMHeaderInfo3 wma) { ushort stream = 0; // Stream number IntPtr ptData; // Data buffer pointer ushort cbData = 0; // Length of data buffer Win32.WMT_ATTR_DATATYPE type; // Attribute type // Determine the size of the buffer we need to allocate, and of course if the // WM/TrackNumber attribute even exists in this header const string ATTR = "Duration\0"; try { wma.GetAttributeByName(ref stream, ATTR, out type, IntPtr.Zero, ref cbData); } catch { return(0); } ptData = Marshal.AllocCoTaskMem((int)cbData); try { wma.GetAttributeByName(ref stream, ATTR, out type, ptData, ref cbData); switch (type) { case Win32.WMT_ATTR_DATATYPE.WMT_TYPE_DWORD: return((long)Marshal.ReadInt32(ptData)); case Win32.WMT_ATTR_DATATYPE.WMT_TYPE_QWORD: return(Marshal.ReadInt64(ptData)); case Win32.WMT_ATTR_DATATYPE.WMT_TYPE_STRING: return(Convert.ToInt64(Marshal.PtrToStringUni(ptData))); default: throw new Exception("Unknown Duration data type detected"); } } finally { Marshal.FreeCoTaskMem(ptData); } // Release allocated memory }