如何投射IDispatch在C#
我一直在开发Web服务在C#链接网站与本地数据库。本地应用程序的SDK是一个COM对象。我打破我的心就如何转换下面的VB代码的第二日(在SDK协议给予),以C#相当于:如何投射IDispatch在C#
XML_DOM := CreateOleObject('MSXML2.DOMDocument.3.0') as IXMLDOMDocumen
GetData(XML_DOM as Idispatch)
我已经试过这样:
System.Type objType = System.Type.GetTypeFromProgID("PERCo_S20_SDK.ExchangeMain");
dynamic comObject = System.Activator.CreateInstance(objType);
if (comObject.SetConnect("192.168.1.14", "211", "ADMIN", "") != 0)
{
//Could not connect to server!
}
XmlDocument dep_xml = new XmlDocument();
XmlDeclaration dep_xml_decl = dep_xml.CreateXmlDeclaration("1.0", "UTF-8", "yes");
XmlElement root = dep_xml.DocumentElement;
dep_xml.InsertBefore(dep_xml_decl, root);
XmlElement element = dep_xml.CreateElement(string.Empty, "documentrequest", string.Empty);
element.SetAttribute("type", "subdiv");
dep_xml.AppendChild(element);
comObject.GetData(dep_xml as IDispatch);
comObject.Disconnect();
与
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00020400-0000-0000-C000-000000000046")]
private interface IDispatch
{
[PreserveSig]
int GetTypeInfoCount(out int Count);
[PreserveSig]
int GetTypeInfo
(
[MarshalAs(UnmanagedType.U4)] int iTInfo,
[MarshalAs(UnmanagedType.U4)] int lcid,
out System.Runtime.InteropServices.ComTypes.ITypeInfo typeInfo
);
[PreserveSig]
int GetIDsOfNames
(
ref Guid riid,
[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr)]
string[] rgsNames,
int cNames,
int lcid,
[MarshalAs(UnmanagedType.LPArray)] int[] rgDispId
);
[PreserveSig]
int Invoke
(
int dispIdMember,
ref Guid riid,
uint lcid,
ushort wFlags,
ref System.Runtime.InteropServices.ComTypes.DISPPARAMS pDispParams,
out object pVarResult,
ref System.Runtime.InteropServices.ComTypes.EXCEPINFO pExcepInfo,
out UInt32 pArgErr
);
}
,但我得到的错误
An exception of type 'System.ArgumentException' occurred in System.Dynamic.dll but was not handled in user code
Additional information: Could not convert argument 0 for call to GetData.
请帮我解决一下如何在C#中投射IDispatch。
An exception of type 'System.ArgumentException' occurred in System.Dynamic.dll but was not handled in user code
Additional information: Could not convert argument 0 for call to GetData.
你的问题是在这里:
XmlDocument dep_xml = ...
comObject.GetData(dep_xml as IDispatch); // <------ BANG!!
XmlDocument
不是一个标准的OLE自动化/调度方式。这不是一个:
int
float
char
BSTR
SAFEARRAY
- etc
...或物体暴露IDispatch
,XmlDocument
未实现的COM接口。 XmlDocument
需要[ComVisible]
出席。
如果对象是COM兼容的,那么你可以只:
comObject.SomethingOrOther(myComCompliantObject);
...没有不必要的演员。
解决方法是什么?我应该使用哪个对象来实现IDispatch? –
我能想到的唯一解决方法是如果你有权访问COM库的源代码,并且能够使它成为C++/CLI,那么你可以将任何旧的'object'传递给COM方法,然后一旦进入你可以将'IUnknown'强制转换为一个已知类型的被管理对象。我必须用遗留代码做一次。我将在明天发布代码片段 – MickyD
不幸的是,我无法访问COM的源代码。可以用以下方法调用所需的COM方法:comObject.GetType()。InvokeMember(“GetData”.....如果是,那么如何? –
很明显,您正在使用.NET XmlDocument类,而不是它的非托管味道(MSXML2.DOMDocument.3.0)。它不能转换为IDispatch。项目>添加参考> COM选项卡,选择“Microsoft XML,v3.0” –
@HansPassant添加对“Microsoft XML,v3.0”的引用。什么应该是传递参数部分:GetData(XML_DOM作为Idispatch)? –
使用'new MSXML2.DOMDocument30()'创建对象,不需要强制转换。 –