.NET mshtml: How to pass a BSTR SAFEARRAY?

前端 未结 3 1305
孤独总比滥情好
孤独总比滥情好 2021-01-22 12:21

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

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

Avoiding

相关标签:
3条回答
  • 2021-01-22 12:39

    The params keyword indicates that you can supply multiple parameters in this place, and it will group automatically. For example, if I had a function thus:

    public int SumNumbers(params int[] value)
    {
           //Logic.
    }
    

    then I could call it like this:

    int myValue = SumNumbers(1,2,3,4,5,6,7,8,9,10);
    

    The array is constructed automagically. So hypothetically, you could call

    mshtml.HTMLDocumentClass doc;
    ...
    doc.write('H','I',' ','M','O','M');
    

    And it would work. Not really practical though. I suppose you've tried calling

    doc.write(myString.ToCharArray());
    

    ? I don't know anything about SAFEARRAYS, but its possible you might not have to know, either, depending on how the compiler helps/hinders here.

    0 讨论(0)
  • 2021-01-22 12:55

    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.

    0 讨论(0)
  • It works like a charm this way :

    [Guid("332C4425-26CB-11D0-B483-00C04FD90119")]
    [ComImport]
    [TypeLibType((short)4160)]
    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
    internal interface IHTMLDocument2
    {
        [DispId(1054)]
        void write([MarshalAs(UnmanagedType.BStr)] string psArray); //modified 
        //void write([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_VARIANT)] object[] psarray); //instead of
    
    0 讨论(0)
提交回复
热议问题