Getting CLR object property raises Unable to cast COM object of type 'System.__ComObject'

0

I'm trying to get the value of a property via reflection, but end up getting the following exception:

System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'

Inner Exception: InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Kinect.Interop.INuiColorCameraSettings'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00A4B392-E315-470C-90B7-F7B4C3CE00C4}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

My code looks like the following, where src is an istance of Microsoft.Kinect.ColorCameraSettings, and propName = "Brightness":

var prop = src.GetType().GetProperty(propName);
if (prop != null)
{
    return prop.GetValue(src, null);
}

I've also tried the supposedly more COM-friendly method, but with the same issue:

return src.GetType().InvokeMember(propName, System.Reflection.BindingFlags.GetProperty, null, src, null);

Most interestingly, there is nothing to suggest that src is a COM object: src.GetType().IsCOMObject returns false

But it seems from the inner exception stack trace that the src object internally interacts with a COM object.

   at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
   at Microsoft.Kinect.Interop.INuiColorCameraSettings.GetBrightness(Double& pBrightness)
   at Microsoft.Kinect.NuiColorCameraSettings.GetBrightness()
   at Microsoft.Kinect.ColorCameraSettings.get_Brightness()

How can I get the value of this property via reflection at runtime?

Edit: Here is what the Threads window looks like when the exception is thrown enter image description here


EDIT: Been meaning to update this. Solution here is to run the code on the correct thread, which in this case is a worker from the thread pool. Simply wrapping the call with a Task.Run() works well. The exception (for me at least) isn't very clear about the underlying cause.

c#
com
kinect
asked on Stack Overflow Mar 15, 2019 by Josh • edited Jun 20, 2020 by Community

2 Answers

0

Been meaning to update this. Solution here is to run the code on the correct thread, which in this case is a worker from the thread pool. Simply wrapping the call with a Task.Run() works well. The exception (for me at least) isn't very clear about the underlying cause.

answered on Stack Overflow Apr 1, 2019 by Josh
0

I was struggling with getting the property values of a System.__ComObject too. Initially, the ComObject was a java object with a few properties. The GetType() solution didn't work for me, so I searched more and found TypeDescriptor as very useful and comfortable.

According to your setup, I think it would be:

if(TypeDescriptor.GetProperties(src).Find(propName, false) != null)
{
   propval = TypeDescriptor.GetProperties(src).Find(propName, false).GetValue(src).ToString();
}

Hopefully, it will help someone.

answered on Stack Overflow Nov 28, 2019 by Johri87

User contributions licensed under CC BY-SA 3.0