none
WEBCAM RRS feed

  • Question

  • HOW TO CHECK IF WEBCAM IS CONNECTED OR NOT TO MY PC using c# code in windows application
    • Edited by MONTYBHARNE Friday, October 26, 2018 8:21 AM
    Friday, October 26, 2018 8:19 AM

All replies

  • You can use DirectShow 

    Old article : Finding Your Web Cam with C# & DirectShow.NET

    Friday, October 26, 2018 9:09 AM
  • directshow method is not working.any another process for this
    Saturday, October 27, 2018 5:46 AM
  • DirectShow works for me on Windows 10.

    I have no Webcam to test, but you can try to replace 

    CLSID_AudioRendererCategory

    by

    CLSID_VideoInputDeviceCategory

    in this test which enumerates Audio devices :

    HRESULT hr = HRESULT.E_FAIL;
    Guid CLSID_SystemDeviceEnum = new Guid("62BE5D10-60EB-11D0-BD3B-00A0C911CE86");
    Type CreateDevEnumType = Type.GetTypeFromCLSID(CLSID_SystemDeviceEnum, true);
    object CreateDevEnum = Activator.CreateInstance(CreateDevEnumType);
    ICreateDevEnum pCreateDevEnum = (ICreateDevEnum)CreateDevEnum;
    
    Guid CLSID_VideoInputDeviceCategory = new Guid("860BB310-5D01-11D0-BD3B-00A0C911CE86");
    Guid CLSID_AudioRendererCategory = new Guid("E0F158E1-CB04-11D0-BD4E-00A0C911CE86");
    
    IEnumMoniker pEm = null;
    hr = pCreateDevEnum.CreateClassEnumerator(CLSID_AudioRendererCategory, out pEm, 0);
    if (hr == HRESULT.S_OK && pEm != null)
    {
        uint cFetched;
        IMoniker pM;
        while ((hr = pEm.Next(1, out pM, out cFetched)) == HRESULT.S_OK && cFetched > 0)                   
        {
            IntPtr pBag = IntPtr.Zero;
            Guid IID_IPropertyBag = new Guid("55272A00-42CB-11CE-8135-00AA004BB851");
            if (pM != null)
            {
                hr = pM.BindToStorage(IntPtr.Zero, null, IID_IPropertyBag, out pBag);
                if (hr == HRESULT.S_OK)
                {
                    IPropertyBag pPropertyBag = Marshal.GetObjectForIUnknown(pBag) as IPropertyBag;
                    PROPVARIANT var;
                    var.varType = (ushort)VARENUM.VT_BSTR;
                    hr = pPropertyBag.Read("FriendlyName", out var, null);
                    string sString = Marshal.PtrToStringUni(var.pwszVal);
                    Marshal.Release(pBag);
                    Console.WriteLine("Name = {0}", sString);
                }
                Marshal.ReleaseComObject(pM);
            }
            else break;                   
        }
        Marshal.ReleaseComObject(pEm);
    } 

    Declarations :

            public enum HRESULT : int                                                                                                                    
            {                                                                                                                                            
                S_OK = 0,                                                                                                                                
                S_FALSE = 1,                                                                                                                             
                E_NOINTERFACE = unchecked((int)0x80004002),                                                                                              
                E_NOTIMPL = unchecked((int)0x80004001),                                                                                                  
                E_FAIL = unchecked((int)0x80004005)                                                                                                      
            }                                                                                                                                            
                                                                                                                                                         
            [ComImport]                                                                                                                                  
            [Guid("29840822-5B84-11D0-BD3B-00A0C911CE86")]                                                                                               
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]                                                                                        
            public interface ICreateDevEnum                                                                                                              
            {                                                                                                                                            
                HRESULT  CreateClassEnumerator(ref Guid clsidDeviceClass, out IEnumMoniker ppEnumMoniker, int dwFlags);                                  
                // HRESULT CreateClassEnumerator(ref Guid clsidDeviceClass, out IntPtr ppEnumMoniker, int dwFlags);                                      
                                                                                                                                                         
            }                                                                                                                                            
                                                                                                                                                         
            [ComImport]                                                                                                                                  
            [Guid("00000102-0000-0000-C000-000000000046")]                                                                                               
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]                                                                                        
            public interface IEnumMoniker                                                                                                                
            {                                                                                                                                            
               HRESULT Next(uint celt, out IMoniker rgelt, out uint pceltFetched);                                                                       
               HRESULT  Skip(uint celt);                                                                                                                 
               HRESULT  Reset();                                                                                                                         
               HRESULT  Clone( out IEnumMoniker ppenum);                                                                                                 
            }                                                                                                                                            
                                                                                                                                                         
            [ComImport]                                                                                                                                  
            [Guid("0000000f-0000-0000-C000-000000000046")]                                                                                               
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]                                                                                        
            public interface IMoniker : IPersistStream                                                                                                   
            {                                                                                                                                            
                #region IPersist                                                                                                                         
                new HRESULT GetClassID([Out] out Guid pClassID);                                                                                         
                #endregion                                                                                                                               
                                                                                                                                                         
                #region IPersistStream                                                                                                                   
                new HRESULT IsDirty();                                                                                                                   
                new HRESULT Load(System.Runtime.InteropServices.ComTypes.IStream pStm);                                                                  
                new HRESULT Save(System.Runtime.InteropServices.ComTypes.IStream pStm, bool fClearDirty);                                                
                new  HRESULT GetSizeMax(out ULARGE_INTEGER pcbSize);                                                                                     
                #endregion                                                                                                                               
                                                                                                                                                         
                //HRESULT BindToObject(IBindCtx pbc, IMoniker pmkToLeft, ref Guid riidResult, ref IntPtr ppvResult);                                     
                HRESULT BindToObject(IntPtr pbc, IMoniker pmkToLeft, ref Guid riidResult, ref IntPtr ppvResult);                                         
                                                                                                                                                         
                //HRESULT BindToStorage(IBindCtx pbc, IMoniker pmkToLeft, ref Guid riid, out IntPtr ppvObj);                                             
                HRESULT BindToStorage(IntPtr pbc, IMoniker pmkToLeft, ref Guid riid, out IntPtr ppvObj);                                                 
                                                                                                                                                         
                //HRESULT Reduce(IBindCtx pbc, int dwReduceHowFar, ref IMoniker ppmkToLeft, out IMoniker ppmkReduced);                                   
                HRESULT Reduce(IntPtr pbc, int dwReduceHowFar, ref IMoniker ppmkToLeft, out IMoniker ppmkReduced);                                       
                                                                                                                                                         
                HRESULT ComposeWith(IMoniker pmkRight, bool fOnlyIfNotGeneric, out IMoniker ppmkComposite);                                              
                                                                                                                                                         
                HRESULT Enum(bool fForward, out IEnumMoniker ppenumMoniker);                                                                             
                                                                                                                                                         
                HRESULT IsEqual(IMoniker pmkOtherMoniker);                                                                                               
                                                                                                                                                         
                HRESULT Hash(out int pdwHash);                                                                                                           
                                                                                                                                                         
                //HRESULT IsRunning(IBindCtx pbc, IMoniker pmkToLeft, IMoniker pmkNewlyRunning);                                                         
                HRESULT IsRunning(IntPtr pbc, IMoniker pmkToLeft, IMoniker pmkNewlyRunning);                                                             
                                                                                                                                                         
                //HRESULT GetTimeOfLastChange(IBindCtx pbc, IMoniker pmkToLeft, out FILETIME pFileTime);                                                 
                HRESULT GetTimeOfLastChange(IntPtr pbc, IMoniker pmkToLeft, out System.Runtime.InteropServices.ComTypes.FILETIME pFileTime);             
                                                                                                                                                         
                HRESULT Inverse(out IMoniker ppmk);                                                                                                      
                                                                                                                                                         
                HRESULT CommonPrefixWith(IMoniker pmkOther, out IMoniker ppmkPrefix);                                                                    
                                                                                                                                                         
                HRESULT RelativePathTo(IMoniker pmkOther, out IMoniker ppmkRelPath);                                                                     
                                                                                                                                                         
                //HRESULT GetDisplayName(IBindCtx pbc, IMoniker pmkToLeft, out LPOLESTR ppszDisplayName);                                                
                HRESULT GetDisplayName(IntPtr pbc, IMoniker pmkToLeft, out string ppszDisplayName);                                                      
                                                                                                                                                         
                //HRESULT ParseDisplayName(IBindCtx pbc, IMoniker pmkToLeft, LPOLESTR pszDisplayName, out uint pchEaten, out IMoniker ppmkOut);          
                HRESULT ParseDisplayName(IntPtr pbc, IMoniker pmkToLeft, string pszDisplayName, out uint pchEaten, out IMoniker ppmkOut);                
                                                                                                                                                         
                HRESULT IsSystemMoniker(out int pdwMksys);                                                                                               
            }                                                                                                                                            
                                                                                                                                                         
            [ComImport]                                                                                                                                  
            [Guid("00000109-0000-0000-C000-000000000046")]                                                                                               
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]                                                                                        
            public interface IPersistStream : IPersist                                                                                                   
            {                                                                                                                                            
                #region IPersist                                                                                                                         
                new HRESULT GetClassID([Out] out Guid pClassID);                                                                                         
                #endregion                                                                                                                               
                HRESULT IsDirty();                                                                                                                       
                HRESULT Load(System.Runtime.InteropServices.ComTypes.IStream pStm);                                                                      
                HRESULT Save(System.Runtime.InteropServices.ComTypes.IStream pStm, bool fClearDirty);                                                    
                HRESULT GetSizeMax(out ULARGE_INTEGER pcbSize);                                                                                          
            }                                                                                                                                            
                                                                                                                                                         
            [StructLayout(LayoutKind.Explicit)]                                                                                                          
            public struct ULARGE_INTEGER                                                                                                                 
            {                                                                                                                                            
                [FieldOffset(0)]                                                                                                                         
                public int LowPart;                                                                                                                      
                [FieldOffset(4)]                                                                                                                         
                public int HighPart;                                                                                                                     
                [FieldOffset(0)]                                                                                                                         
                public long QuadPart;                                                                                                                    
            }                                                                                                                                            
                                                                                                                                                         
            [ComImport]                                                                                                                                  
            [Guid("0000010c-0000-0000-C000-000000000046")]                                                                                               
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]                                                                                        
            public interface IPersist                                                                                                                    
            {                                                                                                                                            
                 HRESULT GetClassID([Out] out Guid pClassID);                                                                                            
                                                                                                                                                         
            }                                                                                                                                            
                                                                                                                                                         
            [ComImport]                                                                                                                                  
            [Guid("55272A00-42CB-11CE-8135-00AA004BB851")]                                                                                               
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]                                                                                        
            public interface IPropertyBag                                                                                                                
            {                                                                                                                                            
                //HRESULT Read(string pszPropName, ref VARIANT* pVar, IErrorLog pErrorLog);                                                              
                //HRESULT Read(string pszPropName, ref IntPtr pVar, IErrorLog pErrorLog);                                                                
                HRESULT Read(string pszPropName, out PROPVARIANT pVar, IErrorLog pErrorLog);                                                             
                                                                                                                                                         
                //HRESULT Write(string pszPropName, VARIANT* pVar);                                                                                      
                HRESULT Write(string pszPropName, IntPtr pVar);                                                                                          
            }                                                                                                                                            
                                                                                                                                                         
            [ComImport]                                                                                                                                  
            [Guid("3127CA40-446E-11CE-8135-00AA004BB851")]                                                                                               
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]                                                                                        
            public interface IErrorLog                                                                                                                   
            {                                                                                                                                            
                //HRESULT AddError(LPCOLESTR pszPropName, System.Runtime.InteropServices.ComTypes.EXCEPINFO pExcepInfo);                                 
                HRESULT AddError([In, MarshalAs(UnmanagedType.LPWStr)] string pszPropName, System.Runtime.InteropServices.ComTypes.EXCEPINFO pExcepInfo);
            }                                                                                                                                            
                                                                                                                                                         
            [StructLayout(LayoutKind.Sequential)]                                                                                                        
            public struct PROPARRAY                                                                                                                      
            {                                                                                                                                            
                public UInt32 cElems;                                                                                                                    
                public IntPtr pElems;                                                                                                                    
            }                                                                                                                                            
                                                                                                                                                         
            [StructLayout(LayoutKind.Explicit, Pack = 1)]                                                                                                
            public struct PROPVARIANT                                                                                                                    
            {                                                                                                                                            
                [FieldOffset(0)]                                                                                                                         
                public ushort varType;                                                                                                                   
                [FieldOffset(2)]                                                                                                                         
                public ushort wReserved1;                                                                                                                
                [FieldOffset(4)]                                                                                                                         
                public ushort wReserved2;                                                                                                                
                [FieldOffset(6)]                                                                                                                         
                public ushort wReserved3;                                                                                                                
                                                                                                                                                         
                [FieldOffset(8)]                                                                                                                         
                public byte bVal;                                                                                                                        
                [FieldOffset(8)]                                                                                                                         
                public sbyte cVal;                                                                                                                       
                [FieldOffset(8)]                                                                                                                         
                public ushort uiVal;                                                                                                                     
                [FieldOffset(8)]                                                                                                                         
                public short iVal;                                                                                                                       
                [FieldOffset(8)]                                                                                                                         
                public UInt32 uintVal;                                                                                                                   
                [FieldOffset(8)]                                                                                                                         
                public Int32 intVal;                                                                                                                     
                [FieldOffset(8)]                                                                                                                         
                public UInt64 ulVal;                                                                                                                     
                [FieldOffset(8)]                                                                                                                         
                public Int64 lVal;                                                                                                                       
                [FieldOffset(8)]                                                                                                                         
                public float fltVal;                                                                                                                     
                [FieldOffset(8)]                                                                                                                         
                public double dblVal;                                                                                                                    
                [FieldOffset(8)]                                                                                                                         
                public short boolVal;                                                                                                                    
                [FieldOffset(8)]                                                                                                                         
                public IntPtr pclsidVal; // GUID ID pointer                                                                                              
                [FieldOffset(8)]                                                                                                                         
                public IntPtr pszVal; // Ansi string pointer                                                                                             
                [FieldOffset(8)]                                                                                                                         
                public IntPtr pwszVal; // Unicode string pointer                                                                                         
                [FieldOffset(8)]                                                                                                                         
                public IntPtr punkVal; // punkVal (interface pointer)                                                                                    
                [FieldOffset(8)]                                                                                                                         
                public PROPARRAY ca;                                                                                                                     
                [FieldOffset(8)]                                                                                                                         
                public System.Runtime.InteropServices.ComTypes.FILETIME filetime;                                                                        
            }                                                                                                                                            
                                                                                                                                                         
            public enum VARENUM                                                                                                                          
            {                                                                                                                                            
                VT_EMPTY = 0,                                                                                                                            
                VT_NULL = 1,                                                                                                                             
                VT_I2 = 2,                                                                                                                               
                VT_I4 = 3,                                                                                                                               
                VT_R4 = 4,                                                                                                                               
                VT_R8 = 5,                                                                                                                               
                VT_CY = 6,                                                                                                                               
                VT_DATE = 7,                                                                                                                             
                VT_BSTR = 8,                                                                                                                             
                VT_DISPATCH = 9,                                                                                                                         
                VT_ERROR = 10,                                                                                                                           
                VT_BOOL = 11,                                                                                                                            
                VT_VARIANT = 12,                                                                                                                         
                VT_UNKNOWN = 13,                                                                                                                         
                VT_DECIMAL = 14,                                                                                                                         
                VT_I1 = 16,                                                                                                                              
                VT_UI1 = 17,                                                                                                                             
                VT_UI2 = 18,                                                                                                                             
                VT_UI4 = 19,                                                                                                                             
                VT_I8 = 20,                                                                                                                              
                VT_UI8 = 21,                                                                                                                             
                VT_INT = 22,                                                                                                                             
                VT_UINT = 23,                                                                                                                            
                VT_VOID = 24,                                                                                                                            
                VT_HRESULT = 25,                                                                                                                         
                VT_PTR = 26,                                                                                                                             
                VT_SAFEARRAY = 27,                                                                                                                       
                VT_CARRAY = 28,                                                                                                                          
                VT_USERDEFINED = 29,                                                                                                                     
                VT_LPSTR = 30,                                                                                                                           
                VT_LPWSTR = 31,                                                                                                                          
                VT_RECORD = 36,                                                                                                                          
                VT_INT_PTR = 37,                                                                                                                         
                VT_UINT_PTR = 38,                                                                                                                        
                VT_FILETIME = 64,                                                                                                                        
                VT_BLOB = 65,                                                                                                                            
                VT_STREAM = 66,                                                                                                                          
                VT_STORAGE = 67,                                                                                                                         
                VT_STREAMED_OBJECT = 68,                                                                                                                 
                VT_STORED_OBJECT = 69,                                                                                                                   
                VT_BLOB_OBJECT = 70,                                                                                                                     
                VT_CF = 71,                                                                                                                              
                VT_CLSID = 72,                                                                                                                           
                VT_VERSIONED_STREAM = 73,                                                                                                                
                VT_BSTR_BLOB = 0xfff,                                                                                                                    
                VT_VECTOR = 0x1000,                                                                                                                      
                VT_ARRAY = 0x2000,                                                                                                                       
                VT_BYREF = 0x4000,                                                                                                                       
                VT_RESERVED = 0x8000,                                                                                                                    
                VT_ILLEGAL = 0xffff,                                                                                                                     
                VT_ILLEGALMASKED = 0xfff,                                                                                                                
                VT_TYPEMASK = 0xfff                                                                                                                      
            };

    Saturday, October 27, 2018 10:22 AM