Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Enumerate (list) USB Webcams

Enumerate (list) USB Webcams

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionannouncement
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Howard Anderson
    wrote on last edited by
    #1

    I need to be able to list webcams that are plugged in. I can use DeviceChange to tell when a webcam is plugged or unplugged and I am then using the function shown below to create a list of all video devices. This works for all "normal" webcam devices since they appear and disappear from the list when I call the function after they are plugged in or unplugged. Unfortunately, I also have an older webcam, Logitech "Quickcam", that is ALWAYS listed regardless of whether it is plugged in or not... That represents a pathological case. I need a way to determine if it is really plugged in WITHOUT disrupting it in case my software is currently using it. I have scoured all 5 device driver and USB books I know of, scoured MSDN, scoured the internet, tried LOTS of code including USBView which ALMOST provides the information but DISPLAYS A DIFFERENT NAME (not the "FriendlyName" I expected!) so I cannot correlate the information. Anyone know of a solution? (I think this is a REALLY tough problem... I will be unavailable for a couple of weeks but I really need to find a solution and will respond as soon as possible...) VideoDeviceList *CMotionDetectDlg::GetVideoDeviceList() { static VideoDeviceList VideoList; USES_CONVERSION; UINT uIndex = 0; HRESULT hr; BOOL bCheck = FALSE; //Clear all previous video list entries for(int i = 0; i < NUMELMS(VideoList.videoMoniker); i++) { if(VideoList.videoMoniker[i]){ VideoList.videoMoniker[i]->Release(); VideoList.videoMoniker[i] = NULL; } } VideoList.videoString.RemoveAll(); VideoList.numberOfDevices = 0; // enumerate all video capture devices ICreateDevEnum *pCreateDevEnum = 0; hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void**)&pCreateDevEnum); if(hr != NOERROR) { MessageBox(NULL, "Error Creating Device Enumerator"); return NULL; } IEnumMoniker *pEm = 0; hr = pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEm, 0); if(hr != NOERROR) { CString message; message = CString("No Video capture device was found.\r\n\r\n") + CString("Please plug in a webcam or other video capture device."); //MessageBox(message, "Notice"); if(pEm)pEm->Release(); return NULL; } pEm->Reset(); ULONG cFetched; IMoniker *pM; while(hr = pEm->Next(1, &pM, &cFetched), hr==S_OK) { IPropertyBag *pBag=0; hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pBag); if(SUCCEEDED(hr)) { //VARIANT vardes; //vardes.vt = VT_BSTR; //hr = pBag->Read(L"Description", &vardes, NULL);

    F 1 Reply Last reply
    0
    • H Howard Anderson

      I need to be able to list webcams that are plugged in. I can use DeviceChange to tell when a webcam is plugged or unplugged and I am then using the function shown below to create a list of all video devices. This works for all "normal" webcam devices since they appear and disappear from the list when I call the function after they are plugged in or unplugged. Unfortunately, I also have an older webcam, Logitech "Quickcam", that is ALWAYS listed regardless of whether it is plugged in or not... That represents a pathological case. I need a way to determine if it is really plugged in WITHOUT disrupting it in case my software is currently using it. I have scoured all 5 device driver and USB books I know of, scoured MSDN, scoured the internet, tried LOTS of code including USBView which ALMOST provides the information but DISPLAYS A DIFFERENT NAME (not the "FriendlyName" I expected!) so I cannot correlate the information. Anyone know of a solution? (I think this is a REALLY tough problem... I will be unavailable for a couple of weeks but I really need to find a solution and will respond as soon as possible...) VideoDeviceList *CMotionDetectDlg::GetVideoDeviceList() { static VideoDeviceList VideoList; USES_CONVERSION; UINT uIndex = 0; HRESULT hr; BOOL bCheck = FALSE; //Clear all previous video list entries for(int i = 0; i < NUMELMS(VideoList.videoMoniker); i++) { if(VideoList.videoMoniker[i]){ VideoList.videoMoniker[i]->Release(); VideoList.videoMoniker[i] = NULL; } } VideoList.videoString.RemoveAll(); VideoList.numberOfDevices = 0; // enumerate all video capture devices ICreateDevEnum *pCreateDevEnum = 0; hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void**)&pCreateDevEnum); if(hr != NOERROR) { MessageBox(NULL, "Error Creating Device Enumerator"); return NULL; } IEnumMoniker *pEm = 0; hr = pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEm, 0); if(hr != NOERROR) { CString message; message = CString("No Video capture device was found.\r\n\r\n") + CString("Please plug in a webcam or other video capture device."); //MessageBox(message, "Notice"); if(pEm)pEm->Release(); return NULL; } pEm->Reset(); ULONG cFetched; IMoniker *pM; while(hr = pEm->Next(1, &pM, &cFetched), hr==S_OK) { IPropertyBag *pBag=0; hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pBag); if(SUCCEEDED(hr)) { //VARIANT vardes; //vardes.vt = VT_BSTR; //hr = pBag->Read(L"Description", &vardes, NULL);

      F Offline
      F Offline
      FlyingTinman
      wrote on last edited by
      #2

      If you try to BindToObject() after verifying the success of BindToStorage() wouldn't that succeed of fail depending on the presence of the device? (I could be wrong but I don't think it will disrupt the device if it is in use) [edit] I can confirm after testing, this does not distrupt a running device. I can't confirm that BindToObject will fail if the device is not plugged in; I don't have a device that enumerates if not plugged in.[/edit] e.g. ...

      __hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pBag);
      __if(SUCCEEDED(hr))
      __{
      ____IBaseFilter *pDevice;
      ____hr = pM->BindToObject(0, 0, IID_IBaseFilter, (void**)&pDevice);
      ____if(SUCCEEDED(hr))
      ____{
      ______pDevice->Release();
      ______...
      ______...

      Steve T -- modified at 13:53 Thursday 20th October, 2005

      H 1 Reply Last reply
      0
      • F FlyingTinman

        If you try to BindToObject() after verifying the success of BindToStorage() wouldn't that succeed of fail depending on the presence of the device? (I could be wrong but I don't think it will disrupt the device if it is in use) [edit] I can confirm after testing, this does not distrupt a running device. I can't confirm that BindToObject will fail if the device is not plugged in; I don't have a device that enumerates if not plugged in.[/edit] e.g. ...

        __hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pBag);
        __if(SUCCEEDED(hr))
        __{
        ____IBaseFilter *pDevice;
        ____hr = pM->BindToObject(0, 0, IID_IBaseFilter, (void**)&pDevice);
        ____if(SUCCEEDED(hr))
        ____{
        ______pDevice->Release();
        ______...
        ______...

        Steve T -- modified at 13:53 Thursday 20th October, 2005

        H Offline
        H Offline
        Howard Anderson
        wrote on last edited by
        #3

        Hi, Finally I have returned... Sorry for delayed response. I know if I use AddFilter to add to the capture graph, I can tell whether the device is there. However, I think that means I have to disrupt it, i.e., eliminate it from the capture graph and then reconnect it. That would interrupt the video. I tried your suggestion but it answers S_OK even when the camera is not attached! Like I said, this is a pathological case caused by the method used by this older QuickCam. It is set up so that you have to use "Unplug or Eject Hardware" to make the driver go away. Newer systems handle this automatically. Wonder if I can figure out what the "Unplug or Eject Hardware" function is doing? If I could, then maybe I could use that to determine the status. I will continue looking at functions that might be used to determine status. Your idea gave me some other ideas to try... Thanks, Howard Howard C. Anderson HTTP://www.astroshow.com

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups