0

When I execute the following line of code

dispatch.GetTypeInfoCount(ref typeInfoCount);

The following exception is thrown

Object of type 'System.Int32' cannot be converted to type 'System.UInt32&'

using System.Runtime.InteropServices;
using ComTypes2 = System.Runtime.InteropServices.ComTypes;

public class ComHelper
{

[ComImport(), Guid("00020400-0000-0000-c000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    private interface IDispatch
    {
        void GetTypeInfoCount(ref uint pctinfo);
        void GetTypeInfo(uint itinfo, uint lcid, ref IntPtr pptinfo);
        void stub_GetIDsOfNames();
        void Invoke(int dispIdMember, 
            ref Guid riid, 
            uint lcid, 
            ushort dwFlags, 
            ref ComTypes2.DISPPARAMS pDispParams, 
            ref object pVarResult, 
            ref IntPtr pExcepInfo, 
            ref uint pArgErr);
    }

   public static bool CheckIfComPropertyOrMethodExists<T1>(T1 objectToCheck, string propertyOrMethodName)
    {

        ComTypes2.ITypeInfo objectTypeInfo = null;
        var objectITypeInfo = default(IntPtr);
        var pFuncDesc = default(IntPtr);

        try
        {

            //  Convert the object to IDispatch
            var dispatch = (IDispatch)objectToCheck;
            uint typeInfoCount = 0;

            //  Attempt to get the objects TypeInfo
            dispatch.GetTypeInfoCount(ref typeInfoCount);

            if (typeInfoCount < 1) throw new ApplicationException("No type info");

            dispatch.GetTypeInfo(0, 0, ref objectITypeInfo);

            if (objectITypeInfo == IntPtr.Zero) throw new ApplicationException("No ITypeInfo");

            objectTypeInfo = (ComTypes2.ITypeInfo)Marshal.GetTypedObjectForIUnknown(objectITypeInfo, typeof(ComTypes2.ITypeInfo));

            var pTypeAttr = default(IntPtr);
            objectTypeInfo.GetTypeAttr(out pTypeAttr);

            var typeAttr = (ComTypes2.TYPEATTR)Marshal.PtrToStructure(pTypeAttr, typeof(ComTypes2.TYPEATTR));

            objectTypeInfo.ReleaseTypeAttr(pTypeAttr);

            //  Find the method we're looking for in the list of COM objects methods
            for (var iFunc = 0; iFunc <= typeAttr.cFuncs - 1; iFunc++)
            {
                objectTypeInfo.GetFuncDesc(iFunc, out pFuncDesc);
                var funcDesc = (ComTypes2.FUNCDESC)Marshal.PtrToStructure(pFuncDesc, typeof(ComTypes2.FUNCDESC));

                string[] names = { string.Empty };
                int pcNames;

                objectTypeInfo.GetNames(funcDesc.memid, names, 1, out pcNames);

                var funcName = names[0];

                if (funcName == propertyOrMethodName)
                {
                    return true;
                }

                objectTypeInfo.ReleaseFuncDesc(pFuncDesc);
            }

            return false;

        }
        finally
        {
            if (objectTypeInfo != null)
            {
                objectTypeInfo.ReleaseFuncDesc(pFuncDesc);
            }
            Marshal.Release(objectITypeInfo);
        }

    }

}

I'm sure that the answer is simple but I can't work it out at the moment. The parameter for GetTypeInfoCount is a uint. The local variable typeInfoCount that is passed by reference to the GetTypeInfoCount method is also a uint. Why am I getting a type conversion exception?

This question relates to the following

How to check if a COM property or method exists without generating an exception?

Useful links so far

http://msdn.microsoft.com/en-us/library/ebbff4bc-36b2-4861-9efa-ffa45e013eb5%28VS.85%29

http://en.wikipedia.org/wiki/IDispatch

Community
  • 1
  • 1
dior001
  • 751
  • 1
  • 13
  • 32
  • Could you please share which namespace IDispatch is in? – AksharRoop Jul 26 '12 at 09:12
  • Hi please see the following MSDN page. http://msdn.microsoft.com/en-us/library/ebbff4bc-36b2-4861-9efa-ffa45e013eb5%28VS.85%29 . I can't find the namespace. I don't know if unmanaged code even has namespaces. I am a novice when it comes to dealing with COM. Unfortunately I'm working with Office Addins at the moment which require interacting with unmanaged code via interop assemblies. – dior001 Jul 26 '12 at 11:18

1 Answers1

2

Try using out instead of ref:

void GetTypeInfoCount(out uint pctinfo);

and then:

uint typeInfoCount;
dispatch.GetTypeInfoCount(out typeInfoCount);

By the way you have the same issue with the GetTypeInfo method. You are using ref for the pointer but it should be out:

void GetTypeInfo(uint itinfo, uint lcid, out IntPtr pptinfo);

Here's the correct P/Invoke wrapper for the IDispatch interface:

[Guid("00020400-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IDispatch
{
    void GetTypeInfoCount(out uint pctinfo);
    void GetTypeInfo(uint iTInfo, int lcid, out IntPtr info);
    void GetIDsOfNames(ref Guid iid, [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPWStr, SizeParamIndex=2)] string[] names, uint cNames, int lcid, [Out, MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.I4, SizeParamIndex=2)] int[] rgDispId);
    void Invoke(int dispIdMember, ref Guid riid, int lcid, INVOKEKIND wFlags, ref DISPPARAMS pDispParams, IntPtr pvarResult, IntPtr pExcepInfo, IntPtr puArgErr);
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thank you for your comprehensive answer. I'm unable to cast transparent proxy to type 'IDispatch'. Do you have any suggestions about how to improve my code so that I can pass any object in the Microsoft.Office.Interop.Excel namespace to the CheckIfComPropertyOrMethodExists method so that I can check if a property or method exists in that object without generating a COM exception? I've been stuck on this piece of code for days now and can't crack it. See http://stackoverflow.com/questions/11540553/how-to-check-if-a-com-property-or-method-exists-without-generating-an-exception – dior001 Jul 27 '12 at 11:26