类型'System.Int32的'的对象不能转换为类型'System.UInt32和放大器;'

编程入门 行业动态 更新时间:2024-10-27 12:24:17
本文介绍了类型'System.Int32的'的对象不能转换为类型'System.UInt32和放大器;'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我执行的code以下行

When I execute the following line of code

dispatch.GetTypeInfoCount(ref typeInfoCount);

下面的异常被抛出

The following exception is thrown

类型'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); } } }

我敢肯定,答案很简单,但我不能工作了的时刻。对于GetTypeInfoCount参数为uint。由参照GetTypeInfoCount方法通过局部变量typeInfoCount也是为uint。为什么我得到一个类型转换的异常?

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?

这个问题涉及到以下

How检查是否存在COM属性或方法,而不会产生异常?

有用的链接至今

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

en.wikipedia/wiki/IDispatch

推荐答案

中尝试使用退出,而不是 REF :

Try using out instead of ref:

void GetTypeInfoCount(out uint pctinfo);

然后:

uint typeInfoCount; dispatch.GetTypeInfoCount(out typeInfoCount);

顺便说一句,你必须用 GetTypeInfo的方法相同的问题。您正在使用 REF 为指针,但它应该是退出:

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

下面是正确的P / Invoke包装IDispatch接口:

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

更多推荐

类型'System.Int32的'的对象不能转换为类型'System.UInt32和放大器;'

本文发布于:2023-05-31 12:02:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/391371.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   放大器   转换为   对象   System

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!