简体   繁体   English

“System.Int32”类型的对象无法转换为“System.UInt32&”类型

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

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&' “System.Int32”类型的对象无法转换为“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. GetTypeInfoCount的参数是一个uint。 The local variable typeInfoCount that is passed by reference to the GetTypeInfoCount method is also a uint. 通过引用GetTypeInfoCount方法传递的局部变量typeInfoCount也是一个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? 如何在不生成异常的情况下检查COM属性或方法是否存在?

Useful links so far 迄今为止有用的链接

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

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

Try using out instead of ref : 尝试使用out ,而不是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. 顺便说一下, GetTypeInfo方法存在同样的问题。 You are using ref for the pointer but it should be out : 您正在使用ref的指针,但它应该是out

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

Here's the correct P/Invoke wrapper for the IDispatch interface: 这是IDispatch接口的正确P / Invoke包装器:

[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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型&#39;System.Int16&#39;的对象不能转换为类型&#39;System.Nullable`1 [System.Int32] - Object of type 'System.Int16' cannot be converted to type 'System.Nullable`1[System.Int32] Expression.Convert:'System.Int64'类型的对象无法转换为'System.Int32'类型 - Expression.Convert: Object of type 'System.Int64' cannot be converted to type 'System.Int32' 无法将类型为“ System.Reflection.ParameterInfo”的对象转换为类型为“ System.Int32”的对象 - Object of type 'System.Reflection.ParameterInfo' cannot be converted to type 'System.Int32' “System.Int32”类型的对象无法转换为“System.Web.Security.Cryptography.Purpose”类型 - Object of type 'System.Int32' cannot be converted to type 'System.Web.Security.Cryptography.Purpose' 无法将类型为“ System.Double”的对象转换为类型为“ System.Int32”的对象 - Object of type 'System.Double' cannot be converted to type 'System.Int32' ASP.NET FormView:“类型&#39;System.Int32&#39;的对象不能转换为类型&#39;System.String” - ASP.NET FormView: “Object of type 'System.Int32' cannot be converted to type 'System.String” 类型'System.Int32'的表达式不能用于返回类型'System.Object' - Expression of type 'System.Int32' cannot be used for return type 'System.Object' Linq和Equality Operator:类型'System.Int32'的表达式不能用于'System.Object'类型的参数 - Linq and the Equality Operator: Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' 错误“无法将'System.Int32'类型的对象强制转换为'System.Collections.Generic.List`1 [System.Int32]'' - error “Unable to cast object of type 'System.Int32' to type 'System.Collections.Generic.List`1[System.Int32]'” 类型'System.Int32'的表达式不能用于方法'Boolean Equals(System.Object)'的'System.Object'类型的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM