.NET MSHTML:如何通过一个BSTR SAFEARRAY? [英] .NET mshtml: How to pass a BSTR SAFEARRAY?

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

问题描述

在Microsoft.mshtml.dll组件的类mshtml.HTMLDocumentClass有一个方法:

 公共虚拟无效写入(params对象[] psarray);
 

避免真正的问题了一会儿,你会用什么code调用的write()?你会使用:

 字符串的HTML =< HTML><身体GT;您好,世界< / BODY>< / HTML>中;
mshtml.HTMLDocumentClass文档;
...
doc.write(HTML);
 

或你会使用:

 字符串的HTML =< HTML><身体GT;您好,世界< / BODY>< / HTML>中;
mshtml.HTMLDocumentClass文档;
...
[对象] PARAMS =新的对象[1];
PARAMS [0] =的html;
doc.write(PARAMS);
 

由于这两个抛出异常。 (类型不匹配。0x80020005)

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

  

的IHTMLDocument2 :: Write方法

     

写入一个或多个HTML EX pressions到   在指定的窗口的文档。

     

语法

  HRESULT写(
   SAFEARRAY * psarray
);
 

     

参数

     

psarray

  [中] A ** ** BSTR指定的文本和HTML标记来写。
 

因此​​,在现实中写方法需要一个指向一个SAFEARRAY,即使微软的 Microsoft.mshtml 互操作程序集定义的方法,采取定期的数组:

 公共虚拟无效写入(params对象[] psarray);
 

忽略MSHTML互操作的声明,我要建立一个 SAFEARRAY 的对象(诗的对象数组),有一个BSTR字符串(诗一字符串)填充它,和东西到一个参数是必须是一个对象数组。


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

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

  [对象] ARRAY1 =新的对象[1];
ARRAY1 [0] =阿尔法;
[对象] ARRAY2 =新的对象[1];
ARRAY2 [0] =喝彩;
[对象] ARRAY3 =新的对象[1];
ARRAY3 [0] =查理;
[对象] array4 =新的对象[1];
array4 [0] =增量;

doc.write(ARRAY1,ARRAY2,ARRAY3,array4);
 

或者是Object []的方法,其中多个参数传递,你必须从字面上创建一个数组?

  [对象] PARAMS =新的对象[4]。
PARAMS [0] =阿尔法;
PARAMS [1] =喝彩;
PARAMS [2] =查理;
PARAMS [3] =增量;
doc.write(PARAMS);
 

或者是数组[]只是一个诱饵,真正传递:

  doc.write(α,布拉沃,查理,三角洲);
 


当我最初使用这个code,从本机Win32应用程序,该BSTR被放置在一个SAFEARRAY。在IDispatch的基础自动化,一切都是一个数组中。在这种情况下,后期绑定code:

  doc.write(HTML);
 

转化由编译成一个SAFEARRAY,其中零个元素包含一个BSTR字符串(这是一个长度prefixed单向code字符串)。

我的问题成为试图构建一个SAFEARRAY,将字符串转换成BSTR,放置BSTR到SAFEARRAY的零个元素,并通过包含一个SAFEARRAY一个变量只接受一个对象阵列中的一个(对象[])。

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


  

Microsoft.mshtml

     

C:\ PROGRAM   文件\ Microsoft.NET \ Primary互操作程序   大会\ Microsoft.mshtml.dll

解决方案

的声明上的 的IHTMLDocument2 通过TLBIMP创建的接口/ VS.NET不正确。它应该是:

 无效写([中,的MarshalAs(UnmanagedType.SafeArray)] [对象] psarray);
 

您必须定义在code此接口,然后使用它。

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

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

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

or would you use:

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

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

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

IHTMLDocument2::write Method

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

Syntax

HRESULT write(
   SAFEARRAY *psarray
);

Parameters

psarray

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

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

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.


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

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


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

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

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[]).

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


Microsoft.mshtml

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

解决方案

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天全站免登陆