
you need to create a com wrapper for the .net code you want to pass to the com object.
https://learn.microsoft.com/en-us/dotnet/standard/native-interop/cominterop
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have an object with type System.__ComObject
. I found out that it implements an interface called _IContactsAndGroupsCallback which is present in the UCCollaborationLib
namespace. As you can see in the link that it is a dispinterface
. I confirmed that the object implements that interface by printing the result of objName is _IContactsAndGroupsCallback
which is true
. But when I try to typecast the object like this:
_IContactsAndGroupsCallback itf = (_IContactsAndGroupsCallback)objName;
I get an exception:
Exception thrown: 'System.InvalidCastException' in mscorlib.dll
An exception of type 'System.InvalidCastException' occurred in mscorlib.dll and wasn't handled before a managed/native boundary
Specified cast is not valid.
Details about the exception:
System.InvalidCastException
HResult=0x80004002
Message=Specified cast is not valid.
Source=mscorlib
StackTrace:
at System.Runtime.InteropServices.Marshal.GetIDispatchForObjectNative(Object o, Boolean onlyInContext)
at System.Runtime.InteropServices.DispatchWrapper..ctor(Object obj)
at System.RuntimeType.WrapArgsForInvokeCall(Object[] aArgs, Int32[] aWrapperTypes)
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at UCCollaborationLib._IContactsAndGroupsCallback.OnLookup(ContactManager _source, Object _lookupResult, AsynchronousOperation _asyncOperation)
at OutlookPresenceProvider.IMClientContactManager.Lookup(String _lookupString, Object _contactsAndGroupsCallback, Object _state)
And when I try to access a method inside the _IContactsAndGroupsCallback
interface like this:
objName.GetType().InvokeMember("OnLookup", BindingFlags.InvokeMethod, null, objName, new object[] { this, null, asyncOperation });
Then I get this exception:
System.Reflection.TargetInvocationException
HResult=0x80131604
Message=COM target does not implement IDispatch.
Source=mscorlib
StackTrace:
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at OutlookPresenceProvider.IMClientContactManager.Lookup(String _lookupString, Object _contactsAndGroupsCallback, Object _state)
How can I invoke the methods of a dispinterface
object?
you need to create a com wrapper for the .net code you want to pass to the com object.
https://learn.microsoft.com/en-us/dotnet/standard/native-interop/cominterop