Old image broken after setting new camport

You can also use the following helper function which does exactly what you want all the time:

/// Opens port with the given \a portNumber on the given \a driver.
/// \return \b nullptr on error; \b IMG handle of opened driver port.
IMG OpenDriverPort(const char *driver, int portNumber)
{
  IMG hImg = nullptr;
  
  if(LoadImageFile(driver, hImg))
  {
    cvbdim_t currentPort = -1;
    if(CS2GetCamPort(hImg, currentPort) >= 0 && currentPort != portNumber)
    {
      IMG hNewPort = nullptr;
      if(CS2SetCamPort(hImg, portNumber, 0, hNewPort) >= 0)
      {
        ReleaseObject(hImg);
        hImg = hNewPort;
      }
    }
  }

  return hImg;
}

You can call it like this:

IMG hFirstCamera = OpenDriverPort("GenICam.vin", 0);
IMG hSecondCamera = OpenDriverPort("GenICam.vin", 1);
1 Like