Esempio n. 1
0
        /// <summary>
        /// Sets a property value.
        /// </summary>
        /// <param name="propertyStore">The property store to set the property in.</param>
        /// <param name="property">The property to set.</param>
        /// <param name="value">The value to set the property to.</param>
        private static void SetPropertyValue(IPropertyStore propertyStore, PropertyKey property, string value)
        {
            var variant = new PropertyVariant(value);

            propertyStore.SetValue(ref property, ref variant);
            variant.Dispose();
        }
Esempio n. 2
0
        private long GetLength(MFSourceReader reader)
        {
            lock (_lockObj)
            {
                try
                {
                    if (reader == null)
                    {
                        return(0);
                    }

                    using (
                        PropertyVariant value =
                            reader.GetPresentationAttribute(NativeMethods.MF_SOURCE_READER_MEDIASOURCE,
                                                            MediaFoundationAttributes.MF_PD_DURATION))


                    {
                        //bug: still, depending on the decoder, this returns imprecise values.
                        return(((value.HValue) * _waveFormat.BytesPerSecond) / 10000000L);
                    }
                }
                catch (Exception)
                {
                    //if (e.Result == (int)HResult.MF_E_ATTRIBUTENOTFOUND)
                    //    return 0;
                    //throw;
                    return(0);
                }
            }
        }
Esempio n. 3
0
        public static void CreateShortcut(string location, string target, IDictionary <PropertyKey, string> properties = null)
        {
            var shellLink = new CShellLink();

            try
            {
                var shellLinkW = (IShellLinkW)shellLink;
                shellLinkW.SetPath(target);

                if (properties != null && properties.Count > 0)
                {
                    if (shellLink is IPropertyStore propertyStore)
                    {
                        foreach (var key in properties.Keys)
                        {
                            var value    = properties[key];
                            var property = new PropertyVariant();
                            property.SetValue(value);
                            propertyStore.SetValue(key, property);
                        }
                    }
                    else
                    {
                        //Properties were specified but the platform does not support them.
                    }
                }

                var persistFile = (IPersistFile)shellLink;
                persistFile.Save(location, true);
            }
            finally
            {
                Marshal.FinalReleaseComObject(shellLink);
            }
        }
Esempio n. 4
0
        private long GetLength(MFSourceReader reader)
        {
            lock (_lockObj)
            {
                try
                {
                    if (reader == null)
                    {
                        return(0);
                    }

                    PropertyVariant value  = reader.GetPresentationAttribute(MFInterops.MF_SOURCE_READER_MEDIASOURCE, MediaFoundationAttributes.MF_PD_DURATION);
                    var             length = ((value.HValue) * _waveFormat.BytesPerSecond) / 10000000L;
                    value.Dispose();
                    return(length);
                }
                catch (Exception)
                {
                    //if (e.Result == (int)HResult.MF_E_ATTRIBUTENOTFOUND)
                    //    return 0;
                    //throw;
                    return(0);
                }
            }
        }
        //Get the current default audio device
        public static AudioDeviceSummary GetDefaultDevice()
        {
            try
            {
                IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator) new MMDeviceEnumerator();
                IMMDevice.IMMDevice deviceItem       = deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);

                //Get the audio device id
                string deviceId = deviceItem.GetId();

                //Get the audio device name
                PropertyVariant propertyVariant = new PropertyVariant();
                IPropertyStore  propertyStore   = deviceItem.OpenPropertyStore(STGM.STGM_READ);
                propertyStore.GetValue(ref PKEY_Device_FriendlyName, out propertyVariant);
                string deviceName = Marshal.PtrToStringUni(propertyVariant.pwszVal);

                return(new AudioDeviceSummary()
                {
                    Identifier = deviceId, Name = deviceName
                });
            }
            catch
            {
                Debug.WriteLine("Failed to get the default audio device.");
                return(null);
            }
        }
Esempio n. 6
0
        private long GetLength(MFSourceReader reader)
        {
            lock (_lockObj)
            {
                try
                {
                    if (reader == null)
                    {
                        return(0);
                    }

                    using (
                        PropertyVariant value =
                            reader.GetPresentationAttribute(NativeMethods.MF_SOURCE_READER_MEDIASOURCE,
                                                            MediaFoundationAttributes.MF_PD_DURATION))
                    {
                        //bug: still, depending on the decoder, this returns imprecise values.
                        return(NanoSecond100UnitsToBytes(value.HValue));
                    }
                }
                catch (Exception)
                {
                    return(0);
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Retrieves an attribute at the specified index.
        /// </summary>
        public unsafe PropertyVariant GetItemByIndex(int index, out Guid key)
        {
            PropertyVariant value = default(PropertyVariant);

            MediaFoundationException.Try(GetItemByIndexNative(index, out key, new IntPtr(&value)), c, "GetItemByIndex");
            return(value);
        }
Esempio n. 8
0
        public static void CreateShortcut(string location, string target, IDictionary <PropertyKey, string> properties = null)
        {
            var shellLink     = new CShellLink();
            var shellLinkW    = (IShellLinkW)shellLink;
            var propertyStore = (IPropertyStore)shellLink;
            var persistFile   = (IPersistFile)shellLink;

            try
            {
                shellLinkW.SetPath(target);

                foreach (var key in properties.Keys)
                {
                    var value    = properties[key];
                    var property = new PropertyVariant();
                    property.SetValue(value);
                    propertyStore.SetValue(key, property);
                }

                persistFile.Save(location, true);
            }
            finally
            {
                Marshal.FinalReleaseComObject(shellLink);
            }
        }
Esempio n. 9
0
 /// <summary>
 /// Queries whether a stored attribute value equals a specified PROPVARIANT.
 /// </summary>
 /// <returns>HRESULT</returns>
 public unsafe int CompareItem(Guid key, PropertyVariant value, out NativeBool result)
 {
     fixed(NativeBool *pr = &result)
     {
         return(InteropCalls.CalliMethodPtr(_basePtr, &key, &value, pr, ((void **)(*(void **)_basePtr))[5]));
     }
 }
Esempio n. 10
0
            public void Dispose()
            {
                PropertyVariant var = this;

                UnsafeNativeMethods.PropVariantClear(ref var);

                valueType    = (ushort)VarEnum.VT_EMPTY;
                wReserved1   = wReserved2 = wReserved3 = 0;
                valueData    = IntPtr.Zero;
                valueDataExt = 0;
            }
Esempio n. 11
0
        private static PropertyVariant CreatePropertyValue(string value)
        {
            PropertyVariant prop = new PropertyVariant();

            byte[] data    = Encoding.GetEncoding(1251).GetBytes(value + "\0");
            IntPtr ptrData = Marshal.AllocHGlobal(data.Length);

            Marshal.Copy(data, 0, ptrData, data.Length);
            prop.vt = VARTYPE.VT_LPSTR;
            prop.unionmember.pszVal = ptrData;
            return(prop);
        }
Esempio n. 12
0
        public void CanSetAndGetValues()
        {
            var random = new Random();

            using (var instance = new MFAttributes(0))
            {
                Guid guid = Guid.NewGuid();
                instance.SetDouble(guid, 10d);
                Assert.AreEqual(10d, instance.GetDouble(guid));

                guid = Guid.NewGuid();
                instance.SetGuid(guid, guid);
                Assert.AreEqual(guid, instance.GetGuid(guid));

                guid = Guid.NewGuid();
                instance.SetString(guid, "Hello World");
                Assert.AreEqual("Hello World", instance.GetString(guid));

                guid = Guid.NewGuid();
                instance.SetUINT32(guid, 10);
                Assert.AreEqual(10, instance.GetUINT32(guid));

                guid = Guid.NewGuid();
                instance.SetUINT64(guid, 10L);
                Assert.AreEqual(10L, instance.GetUINT64(guid));

                guid = Guid.NewGuid();
                var value = new PropertyVariant()
                {
                    DataType  = VarEnum.VT_UI4,
                    UIntValue = 10
                };
                instance.SetItem(guid, value);
                Assert.AreEqual(value, instance.GetItem(guid));

                guid = Guid.NewGuid();
                byte[] inputBlob = new byte[100];
                random.NextBytes(inputBlob);
                instance.Set(guid, inputBlob);
                CollectionAssert.AreEqual(inputBlob, instance.Get <byte[]>(guid));

                guid = Guid.NewGuid();
                TestStruct testStructValue = new TestStruct()
                {
                    Field0 = Guid.NewGuid(),
                    Field1 = random.Next(),
                    Field2 = random.NextDouble() * random.Next(),
                };
                instance.Set(guid, testStructValue);
                Assert.AreEqual(testStructValue, instance.Get <TestStruct>(guid));
            }
        }
Esempio n. 13
0
 private void SetPosition(long value)
 {
     if (CanSeek)
     {
         lock (_lockObj)
         {
             long hnsPos          = (10000000L * value) / WaveFormat.BytesPerSecond;
             var  propertyVariant = PropertyVariant.CreateLong(hnsPos);
             _reader.SetCurrentPosition(Guid.Empty, propertyVariant);
             _decoderBufferCount  = 0;
             _decoderBufferOffset = 0;
             _position            = value;
         }
     }
 }
Esempio n. 14
0
        //Get the AppUserModelId from window handle
        public static string GetAppUserModelIdFromWindowHandle(IntPtr targetWindowHandle)
        {
            try
            {
                PropertyVariant propertyVariant   = new PropertyVariant();
                Guid            propertyStoreGuid = typeof(IPropertyStore).GUID;

                SHGetPropertyStoreForWindow(targetWindowHandle, ref propertyStoreGuid, out IPropertyStore propertyStore);
                propertyStore.GetValue(ref PKEY_AppUserModel_ID, out propertyVariant);

                return(Marshal.PtrToStringUni(propertyVariant.pwszVal));
            }
            catch { }
            return(string.Empty);
        }
Esempio n. 15
0
 private void SetPosition(long value)
 {
     if (CanSeek)
     {
         lock (_lockObj)
         {
             long hnsPos          = (10000000L * value) / WaveFormat.BytesPerSecond;
             var  propertyVariant = new PropertyVariant {
                 HValue = hnsPos, DataType = VarEnum.VT_I8
             };
             _reader.SetCurrentPosition(Guid.Empty, propertyVariant);
             _decoderBufferCount  = 0;
             _decoderBufferOffset = 0;
             _position            = value;
         }
     }
 }
Esempio n. 16
0
        private void SetPosition(long value)
        {
            if (CanSeek)
            {
                lock (_lockObj)
                {
                    long hnsPos          = SamplesToNanoSecond100Units(value);
                    var  propertyVariant = new PropertyVariant {
                        HValue = hnsPos, DataType = VarEnum.VT_I8
                    };
                    _reader.SetCurrentPosition(Guid.Empty, propertyVariant);
                    _decoderBufferCount  = 0;
                    _decoderBufferOffset = 0;
                    _position            = value;

                    _positionChanged = true;
                }
            }
        }
Esempio n. 17
0
        //Get all the playback audio devices
        public static List <AudioDeviceSummary> ListAudioDevices()
        {
            try
            {
                List <AudioDeviceSummary> deviceListSummary = new List <AudioDeviceSummary>();
                IMMDeviceEnumerator       deviceEnumerator  = (IMMDeviceEnumerator) new MMDeviceEnumerator();
                IMMDeviceCollection       deviceCollection  = deviceEnumerator.EnumAudioEndpoints(EDataFlow.eRender, DeviceState.ACTIVE);

                uint deviceCount = deviceCollection.GetCount();
                for (uint deviceIndex = 0; deviceIndex < deviceCount; deviceIndex++)
                {
                    IMMDevice.IMMDevice deviceItem = deviceCollection.Item(deviceIndex);

                    //Get the audio device id
                    string deviceId = deviceItem.GetId();

                    //Get the audio device name
                    PropertyVariant propertyVariant = new PropertyVariant();
                    IPropertyStore  propertyStore   = deviceItem.OpenPropertyStore(STGM.STGM_READ);
                    propertyStore.GetValue(ref PKEY_Device_FriendlyName, out propertyVariant);
                    string deviceName = Marshal.PtrToStringUni(propertyVariant.pwszVal);

                    //Add device to summary list
                    deviceListSummary.Add(new AudioDeviceSummary()
                    {
                        Identifier = deviceId, Name = deviceName
                    });
                }

                return(deviceListSummary);
            }
            catch
            {
                Debug.WriteLine("Failed to get audio devices.");
                return(null);
            }
        }
Esempio n. 18
0
        public static void AddProperty(string path, OrgPropXml properties)
        {
            IStorage Is;

            if (StgOpenStorage(path, null, (int)(Stgm.ShareExclusive | Stgm.ReadWrite), IntPtr.Zero, 0, out Is) == 0 &&
                Is != null)
            {
                IPropertySetStorage pss;
                if (StgCreatePropSetStg(Is, 0, out pss) == 0)
                {
                    var FMTID_CustomInformation = new Guid("{D170DF2E-1117-11D2-AA01-00805FFE11B8}");
                    var pCLSID = new Guid("{00000000-0000-0000-0000-000000000000}");
                    IPropertyStorage ps;
                    pss.Create(ref FMTID_CustomInformation, ref pCLSID, (uint)grfFlags.PROPSETFLAG_DEFAULT,
                               (uint)(Stgm.ShareExclusive | Stgm.ReadWrite), out ps);

                    if (ps == null)
                    {
                        throw new Exception("Don't create container custom property");
                    }

                    PropertySpec[]    propSpec    = new PropertySpec[12];
                    PropertyVariant[] propVariant = new PropertyVariant[12];

                    propSpec[0]  = CreateProperty(OrgPropXml.tagOrgRegnum);
                    propSpec[1]  = CreateProperty(OrgPropXml.tagOrgName);
                    propSpec[2]  = CreateProperty(OrgPropXml.tagRepyear);
                    propSpec[3]  = CreateProperty(OrgPropXml.tagDirectorType);
                    propSpec[4]  = CreateProperty(OrgPropXml.tagDirectorFIO);
                    propSpec[5]  = CreateProperty(OrgPropXml.tagBookkeeperFIO);
                    propSpec[6]  = CreateProperty(OrgPropXml.tagPerformer);
                    propSpec[7]  = CreateProperty(OrgPropXml.tagOperatorName);
                    propSpec[8]  = CreateProperty(OrgPropXml.tagDate);
                    propSpec[9]  = CreateProperty(OrgPropXml.tagVersion);
                    propSpec[10] = CreateProperty(OrgPropXml.tagProgramName);
                    propSpec[11] = CreateProperty(OrgPropXml.tagProgramVersion);

                    propVariant[0]  = CreatePropertyValue(properties.orgRegnum);
                    propVariant[1]  = CreatePropertyValue(properties.orgName);
                    propVariant[2]  = CreatePropertyValue(properties.repYear);
                    propVariant[3]  = CreatePropertyValue(properties.directorType);
                    propVariant[4]  = CreatePropertyValue(properties.directorFIO);
                    propVariant[5]  = CreatePropertyValue(properties.bookkeeperFIO);
                    propVariant[6]  = CreatePropertyValue(properties.performer);
                    propVariant[7]  = CreatePropertyValue(properties.operatorName);
                    propVariant[8]  = CreatePropertyValue(properties.date.ToString("dd.MM.yyyy H:mm:ss"));
                    propVariant[9]  = CreatePropertyValue(properties.version);
                    propVariant[10] = CreatePropertyValue(properties.programName);
                    propVariant[11] = CreatePropertyValue(properties.programVersion);

                    ps.WriteMultiple(12, propSpec, propVariant, 2);
                    ps.Commit(0);

                    ReleaseProperties(propSpec, propVariant);
                    Marshal.FinalReleaseComObject(pss);
                }
                else
                {
                    Console.WriteLine("Could not create property set storage");
                }

                Marshal.FinalReleaseComObject(Is);
            }
            else
            {
                Console.WriteLine("File does not contain a structured storage");
            }

            GC.Collect();
        }
Esempio n. 19
0
 /// <summary>
 /// Associates an attribute value with a key.
 /// </summary>
 public void SetItem(Guid key, PropertyVariant value)
 {
     MediaFoundationException.Try(SetItemNative(key, value), c, "SetItem");
 }
Esempio n. 20
0
 public static extern void PropVariantClear(ref PropertyVariant pvar);
Esempio n. 21
0
 /// <summary>
 /// Gets an attribute from the underlying media source.
 /// </summary>
 /// <returns>HRESULT</returns>
 public unsafe int GetPresentationAttributeNative(int streamIndex, Guid guidAttribute, out PropertyVariant variant)
 {
     variant = default(PropertyVariant);
     fixed(void *ptr = &variant)
     {
         return(InteropCalls.CalliMethodPtr(_basePtr, streamIndex, &guidAttribute, ptr, ((void **)(*(void **)_basePtr))[12]));
     }
 }
Esempio n. 22
0
 /// <summary>
 /// Seeks to a new position in the media source.
 /// </summary>
 public void SetCurrentPosition(Guid guidTimeFormat, PropertyVariant position)
 {
     MediaFoundationException.Try(SetCurrentPositionNative(guidTimeFormat, position), c, "SetCurrentPosition");
 }
Esempio n. 23
0
 /// <summary>
 /// Seeks to a new position in the media source.
 /// </summary>
 /// <returns>HRESULT</returns>
 public unsafe int SetCurrentPositionNative(Guid guidTimeFormat, PropertyVariant position)
 {
     return(InteropCalls.CalliMethodPtr(_basePtr, &guidTimeFormat, &position, ((void **)(*(void **)_basePtr))[8]));
 }
Esempio n. 24
0
 /// <summary>
 /// Sets a property value.
 /// </summary>
 /// <param name="propertyStore">The property store to set the property in.</param>
 /// <param name="property">The property to set.</param>
 /// <param name="value">The value to set the property to.</param>
 private static void SetPropertyValue(IPropertyStore propertyStore, PropertyKey property, string value)
 {
     var variant = new PropertyVariant(value);
     propertyStore.SetValue(ref property, ref variant);
     variant.Dispose();
 }
Esempio n. 25
0
 internal static extern void PropVariantClear([In, Out] ref PropertyVariant pvar);
Esempio n. 26
0
        public static OrgPropXml ReadProperty(string path)
        {
            IStorage   Is;
            OrgPropXml properties = null;

            if (StgOpenStorage(path, null, (int)(Stgm.ShareExclusive | Stgm.ReadWrite), IntPtr.Zero, 0, out Is) == 0 &&
                Is != null)
            {
                IPropertySetStorage pss;
                if (StgCreatePropSetStg(Is, 0, out pss) == 0)
                {
                    var FMTID_CustomInformation = new Guid("{D170DF2E-1117-11D2-AA01-00805FFE11B8}");
                    //var pCLSID = new Guid("{00000000-0000-0000-0000-000000000000}");
                    IPropertyStorage ps;
                    UInt32           propCount = 12;
                    pss.Open(ref FMTID_CustomInformation, (Stgm.ShareExclusive | Stgm.Read), out ps);
                    if (ps != null)
                    {
                        PropertySpec[]    propSpec    = new PropertySpec[propCount];
                        PropertyVariant[] propVariant = new PropertyVariant[propCount];

                        propSpec[0]  = CreateProperty(OrgPropXml.tagOrgRegnum);
                        propSpec[1]  = CreateProperty(OrgPropXml.tagOrgName);
                        propSpec[2]  = CreateProperty(OrgPropXml.tagRepyear);
                        propSpec[3]  = CreateProperty(OrgPropXml.tagDirectorType);
                        propSpec[4]  = CreateProperty(OrgPropXml.tagDirectorFIO);
                        propSpec[5]  = CreateProperty(OrgPropXml.tagBookkeeperFIO);
                        propSpec[6]  = CreateProperty(OrgPropXml.tagPerformer);
                        propSpec[7]  = CreateProperty(OrgPropXml.tagOperatorName);
                        propSpec[8]  = CreateProperty(OrgPropXml.tagDate);
                        propSpec[9]  = CreateProperty(OrgPropXml.tagVersion);
                        propSpec[10] = CreateProperty(OrgPropXml.tagProgramName);
                        propSpec[11] = CreateProperty(OrgPropXml.tagProgramVersion);

                        ps.ReadMultiple(propCount, propSpec, propVariant);

                        properties = new OrgPropXml();

                        properties.orgRegnum     = GetString(propVariant[0].unionmember.pszVal);
                        properties.orgName       = GetString(propVariant[1].unionmember.pszVal);
                        properties.repYear       = GetString(propVariant[2].unionmember.pszVal);
                        properties.directorType  = GetString(propVariant[3].unionmember.pszVal);
                        properties.directorFIO   = GetString(propVariant[4].unionmember.pszVal);
                        properties.bookkeeperFIO = GetString(propVariant[5].unionmember.pszVal);
                        properties.performer     = GetString(propVariant[6].unionmember.pszVal);
                        properties.operatorName  = GetString(propVariant[7].unionmember.pszVal);
                        properties.date          = DateTime.ParseExact(GetString(propVariant[8].unionmember.pszVal),
                                                                       "dd.MM.yyyy H:mm:ss",
                                                                       CultureInfo.InvariantCulture);
                        properties.version        = GetString(propVariant[9].unionmember.pszVal);
                        properties.programName    = GetString(propVariant[10].unionmember.pszVal);
                        properties.programVersion = GetString(propVariant[11].unionmember.pszVal);


                        Marshal.FinalReleaseComObject(ps);
                    }
                    else
                    {
                        Console.WriteLine("Could not open property storage");
                    }

                    Marshal.FinalReleaseComObject(pss);
                }
                else
                {
                    Console.WriteLine("Could not create property set storage");
                }

                Marshal.FinalReleaseComObject(Is);
            }
            else
            {
                Console.WriteLine("File does not contain a structured storage");
            }

            GC.Collect();
            return(properties);
        }
Esempio n. 27
0
 /// <summary>
 /// Associates an attribute value with a key.
 /// </summary>
 /// <returns>HRESULT</returns>
 public unsafe int SetItemNative(Guid key, PropertyVariant value)
 {
     return(InteropCalls.CalliMethodPtr(_basePtr, &key, &value, ((void **)(*(void **)_basePtr))[18]));
 }