当我执行以下代码行时
dispatch.GetTypeInfoCount(ref typeInfoCount);
抛出以下异常
“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。为什么会出现类型转换异常?
这个问题与以下内容有关
到目前为止有用的链接
http://msdn.microsoft.com/en-us/library/ebbff4bc-36b2-4861-9efa-ffa45e013eb5%28VS.85%29