Call to ManagementObjectSearchert.Get() crashes with InvalidCastException

1

I am trying to query attached USB devices as follows (C#, .NET 4.5, Win10)

using (ManagementObjectSearcher mos = new ManagementObjectSearcher("select DeviceID, Model from Win32_DiskDrive where InterfaceType='USB'"))
{
    ManagementObjectCollection coll = mos.Get();
    foreach (ManagementObject drive in coll)
    {
    }
}

The interesting part is that I get an InvalidCastException on the Get() function, without any further info on the inner exception.

System.InvalidCastException ist aufgetreten. HResult=0x80004002
Nachricht = Die angegebene Umwandlung ist ungültig. Quelle = Stapelüberwachung:

Translation of above error message to English done in bing.com

System.InvalidCastException has occurred. HResult = 0x80004002 Message = the specified cast is not valid. Source = stack trace:

The more interesting part: If I step through, leaving a bit time between the construction of the ManagementObjectSearch and the collection of the Get() command, it works.

This sounds to me as if Get() runs asynchronously. Can anybody shed some light on this?

Update July, 2nd, 2017:

I checked on another system, and got another more informative error message (sorry for the german error message, maybe some of you can help me nevertheless :-):

Assistent für verwaltetes Debuggen "DisconnectedContext" ist aufgetreten. HResult=0x00000000 Nachricht = Assistent für verwaltetes Debuggen "DisconnectedContext" : "Folgender Fehler beim Übergang in den COM-Kontext 0x16d4d48 für diesen RuntimeCallableWrapper: Ein ausgehender Aufruf kann nicht ausgeführt werden, da die Anwendung einen eingabe-synchronisierten Aufruf weiterleitet. (Ausnahme von HRESULT: 0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL)). Dieser tritt in der Regel auf, da der COM-Kontext 0x16d4d48, in dem dieser RuntimeCallableWrapper erstellt wurde, getrennt wurde oder aufgrund eines anderen Vorgangs ausgelastet ist und den Kontextübergang deshalb nicht verarbeiten kann. Es wird kein Proxy verwendet, um die Anfrage an die COM-Komponente zu verarbeiten, und Aufrufe werden direkt an die COM-Komponente gesendet. Dies kann Datenbeschädigung oder -verlust zur Folge haben. Um dieses Problem zu vermeiden, müssen Sie sicherstellen, dass alle COM-Kontexte/Apartments/Threads so lange beibehalten werden und für den Kontextübergang verfügbar sind, bis die Anwendung alle RuntimeCallableWrappers, die in ihnen enthaltene COM-Komponenten darstellen, vollständig verarbeitet hat."

Error Translated to English in bing.com for quick response:

Managed debugging assistant "DisconnectedContext" has occurred. " HResult = 0x00000000 message = "DisconnectedContext managed debugging assistant ': "Folgender error during the transition to the COM context of 0x16d4d48 for this RuntimeCallableWrapper: an outgoing call can not run, because the application routes a call to input synchronized. " (Exception from HRESULT: 0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL)). This usually occurs because the COM context therefore cannot process 0x16d4d48, in which this RuntimeCallableWrapper was created, disconnected or is exhausted due to other operation and the context transition. It uses no proxy to process the request on the COM component, and calls are sent directly to the COM component. May result in data corruption or loss result. "To avoid this problem, you must ensure that all COM contexts/apartments/threads so long be retained and available for the context transition, until the application has completely processed all RuntimeCallableWrappers that represent COM components contained in them. "

c#
.net
asked on Stack Overflow Jun 29, 2017 by JoeyD • edited Jul 2, 2017 by Siva Gopal

1 Answer

0

Found the solution with the new error code RPC_E_CANTCALLOUT_ININPUTSYNCCALL when trying to access USB device

Don't really understand why this works though...

answered on Stack Overflow Jul 2, 2017 by JoeyD

User contributions licensed under CC BY-SA 3.0