Re: MarshalAs for Properties?

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Idael Cardoso (nospam.yetiicb_at_hotmail.com)
Date: 04/11/04

  • Next message: Duncan McCreadie: "Outlook Custom Forms Controls and .net"
    Date: Sun, 11 Apr 2004 18:46:41 +0200
    
    

    If your property is read only property then you can do the following:

    public interface YourInterface

    {

      ..

      string YourProperty{ [return: MarshalAs(UnmanagedType.BStr)] get; }

    }

    This is not documented but works and generate and IDL similar to the one
    generate by TLBIMP. If your property is read/write you can't specify the
    MarshalAs attribute for the value parameter of the C# set accessor.

    In manager C++ you can do the following:

    __gc __interface YourInterface

    {

        [MarshalAs(UnmanagedType::BStr)] __property String*
    get_YourProperty();

        __property void set_YourProperty([MarshalAs(UnmanagedType::BStr)]String*
    Value);

    }

    In VB.NET you can't specified the MarshalAs attribute to the result of the
    get accessor but you can do it in the set accessor as following:

    Public WriteOnly Property Name() As String

        Set(<MarshalAs(UnmanagedType.BStr)> ByVal Value As String)

          m_Name = Value

        End Set

     End Property

    I advise you that if you have you interface defined in IDL or already have
    the library or tlb who defines it use TLBIMP to obtain the managed
    definition of your interface. If you need more special control of imported
    library you can use the combination of ILDASM and ILASM to fix some details.

    Hope that this can help.

    Ideal.
    "James Han***" <nospamjhan***@darwinproductions.ca> wrote in message
    news:ehWB8tzHEHA.2756@TK2MSFTNGP10.phx.gbl...
    > I have an interface that I'm mapping to .net
    >
    > one of the properties on the interface is:
    >
    > [DispId(HTMLConsts.DISPID_IHTMLDOCUMENT2_DESIGNMODE)]
    > string DesignMode{get; set;}
    >
    >
    > I get a ComException error with this. Presumably because it's supposed to
    be
    > a Bstr.
    >
    > When I'm doing Method mapping (my word) I can pass a parameter and
    including
    > [MarshalAs(UnmanagedType.Bstr)] and all is well. How do I go about the
    same
    > thing with Properties? It tells me I can't do it that way.
    >
    > Thanks!
    > James Han***
    >
    >


  • Next message: Duncan McCreadie: "Outlook Custom Forms Controls and .net"
  • Quantcast