![](/img/trans.png)
[英]Object of type 'System.Int16' cannot be converted to type 'System.Nullable`1[System.Int32]
[英]Object of type 'System.Int32' cannot be converted to type 'System.UInt32&'
當我執行以下代碼行時
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
嘗試使用out
,而不是ref
:
void GetTypeInfoCount(out uint pctinfo);
接着:
uint typeInfoCount;
dispatch.GetTypeInfoCount(out typeInfoCount);
順便說一下, GetTypeInfo
方法存在同樣的問題。 您正在使用ref
的指針,但它應該是out
:
void GetTypeInfo(uint itinfo, uint lcid, out IntPtr pptinfo);
這是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.