LINQ query fails on COM objects

0

I am currenty dealing with WIA Interop library to implement scanning functionality for the project I am working at. Doing some refactoring according to my collegue advice I tried to rewrite this function:

private static string GetManufacturer(WIA.DeviceInfo info)
{
            string manufacturer = "";
            foreach (WIA.Property p in info.Properties)
            {
                if (p.Name == "Manufacturer")
                {
                    manufacturer = ((WIA.IProperty)p).get_Value().ToString();
                    break;
                }
            }    
            return manufacturer;
}

using linq this way:

private static string GetProperty(IDeviceInfo info, string name)
            => info.Properties.ToArray()
                .Select(property => ((Property)property))
                .FirstOrDefault(item => item.Name == name)
                .get_Value()
                .ToString();

However this code fails with an exception:

Unable to cast COM object of type 'System.__ComObject' to interface type 'WIA.Property'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{706038DC-9F4B-4E45-88E2-5EB7D665B815}' failed due to the following error: Interface is not supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Have I forgotten some casting or is LINQ by default not supported by COM objects?

linq
com
wia
comobject
asked on Stack Overflow Feb 18, 2018 by Illania

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0