.NET mshtml:如何传递BSTR SAFEARRAY? [英] .NET mshtml: How to pass a BSTR SAFEARRAY?

查看:287
本文介绍了.NET mshtml:如何传递BSTR SAFEARRAY?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft.mshtml.dll程序集中的类mshtml.HTMLDocumentClass有一个方法:

The class mshtml.HTMLDocumentClass in Microsoft.mshtml.dll assembly has a method:

public virtual void write(params object[] psarray);

避免真正的问题,你会用什么代码调用 write ?您会使用:

Avoiding the real question for a moment, what code would you use to call write()? Would you use:

String html = "<html><body>Hello, world!</body></html>";
mshtml.HTMLDocumentClass doc;
...
doc.write(html);

或您将使用:

String html = "<html><body>Hello, world!</body></html>";
mshtml.HTMLDocumentClass doc;
...
object[] params = new Object[1];
params[0] = html;
doc.write(params);

因为这两个都抛出异常。 (type mismatch。0x80020005)

Because both of those throw an exception. (Type mismatch. 0x80020005)

HTMLDocumentClass.write 方法实际上来自 IHTMLDocument2接口,其记录为:

The HTMLDocumentClass.write method actually comes from IHTMLDocument2 interface, which is documented as:


IHTMLDocument2 :: write方法

IHTMLDocument2::write Method

在指定窗口中将一个或多个HTML表达式写入
文档。

Writes one or more HTML expressions to a document in the specified window.

语法

HRESULT write(
   SAFEARRAY *psarray
);

参数

psarray

   [in] A **BSTR** that specifies the text and HTML tags to write.


所以在现实中写方法需要一个指向SAFEARRAY的指针,即使Microsoft的 Microsoft.mshtml 互操作程序集将方法定义为使用常规数组:

So in reality the write method needs a pointer to a SAFEARRAY, even though Microsoft's Microsoft.mshtml interop assembly define the write method as taking a regular array:

public virtual void write(params object[] psarray);

忽略mshtml interop声明,我必须构造一个 SAFEARRAY 对象对于一个对象数组),用一个BSTR字符串(对于一个字符串)填充它,并将其填充到一个必须是对象数组的参数。

Ignoring the mshtml interop declaration, i have to construct a SAFEARRAY object (verses an object array), fill it with a BSTR string (verses a String), and stuff it into a parameter that must be an object array.

注意:我不确定 params 关键字的含义。它用于指示可变数量的参数。

Note: i'm unsure of the meaning of the params keyword. It is used to indicate a variable number of parameters.

这是否意味着它可以接受多个数组参数?

Does that mean that it can take multiple array parameters?

object[] array1 = new Object[1];
array1 [0] = alpha;
object[] array2 = new Object[1];
array2 [0] = bravo;
object[] array3 = new Object[1];
array3 [0] = charlie;
object[] array4 = new Object[1];
array4 [0] = delta;

doc.write(array1, array2, array3, array4);

或者是object []传递多个参数的方法,你必须逐字地创建一个数组?

Or is object[] the method in which multiple parameters are passed, and you must literally create an array?

object[] params = new Object[4];
params[0] = alpha;
params[1] = bravo;
params[2] = charlie;
params[3] = delta;
doc.write(params);

或者数组[]只是一个诱饵,你真的可以通过:

Or is the array[] just a decoy, and really you pass:

doc.write(alpha, bravo, charlie, delta);






当我最初使用这个代码时, Win32应用程序,BSTR被放置在一个SAFEARRAY。在基于IDispatch的自动化中,一切都在数组内。在这种情况下,后期绑定代码:


When i originally used this code, from a native Win32 app, the BSTR was placed inside a SAFEARRAY. In IDispatch based automation, everything is inside an array. In this case the late binding code:

doc.write(html);

转换为SAFEARRAY,其中第零个元素包含一个BSTR字符串是一个长度前缀的unicode字符串)。

was converted by the compiler into a SAFEARRAY, where the zero-th element contains a BSTR string (which is a length prefixed unicode string).

我的问题成为一个尝试构造一个SAFEARRAY,将一个字符串转换为BSTR,将BSTR放置到第零元素的SAFEARRAY,并将包含SAFEARRAY的变量传递给只接受对象数组(object [])的变量。

My problem becomes one of trying to construct a SAFEARRAY, converting a String into a BSTR, placing the BSTR into the zero-th element of the SAFEARRAY, and passing a variable that contains a SAFEARRAY to one that only accepts an object array (object[]).

这是真正的问题: 如何创建BSTR SAFEARRAY?

This is the real question: how to create a BSTR SAFEARRAY?


Microsoft.mshtml

Microsoft.mshtml

C:\Program
Files \Microsoft.NET \Primary Interop
Assemblies\Microsoft.mshtml.dll

C:\Program Files\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll


推荐答案

IHTMLDocument2 由TLBIMP / VS.NET创建的界面是不正确的。它应该是:

The declaration for the write method on the IHTMLDocument2 interface created by TLBIMP/VS.NET is incorrect. It should be:

void Write([In, MarshalAs(UnmanagedType.SafeArray)] object[] psarray);

您必须在代码中定义此接口,然后使用。

You will have to define this interface in code and then use that.

这篇关于.NET mshtml:如何传递BSTR SAFEARRAY?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆