How to access methods or properties of a dispinterface object?

Manoj 86 Reputation points
2022-03-07T16:31:07.757+00:00

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?

Microsoft 365 and Office | Development | Other
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,926 Reputation points Volunteer Moderator
    2022-03-07T17:47:04.327+00:00

    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


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.