none
Pass a ExpandoObject or Anonymous type to a COM method that takes a VARIANT argument RRS feed

  • Question

  • Consider a COM object MyClass with these IDispatch methods....

    HRESULT Open([in] VARIANT configInfo)

    HRESULT Close()

     

    From JavaScript, we can do the following:

    var myObject = new ActiveXobject("MyProgId");

    myObject.Open({

         name: "Bob",

         city: "Boston"

    });

     

    How can we do the same from C#?

     

    a) using an instance of an ExpandoObject ( I receive an IDispatch but cannot 'see' the members)

    b) using an instance of an anonymous type ( I get a E_OUTOFMEMORY in my C++ code)

     

    Obviously the method already suggested elsewhere in the forum works but is awkward...

    [ComVisible(true)]

    public class MyArg

    {

      public string name = "Bob";

      public string city = "Boston";

    }

     

    MyClass myObject = new MyClass(); // assume we have a static reference to the interop assembly

    myObject.Open(new MyArg());

     

     

     

     

     

     

    Tuesday, June 21, 2011 1:39 AM

Answers

  • Hi Subba Y,

     

    Welcome to the MSDN forum.

     

    According to your description, I think we can modify a little code of the suggested method to achieve your purpose.

     

    We can define the MyArg class as below:

    [ComVisible(true)]

    public class MyArg
    {
               public string name;
               public string city ;

    public MyArg(string Name, string City)

    {

    this.name=Name;

    this.city=City;

    }

    }

     

    Then we can instance COM object and call the Open method as below:

    MyClass myObject = new MyClass();

    myObject.Open(new MyArg("Bob","Boston"));

     

    I think this can help you.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Tuesday, June 28, 2011 8:08 AM
    Wednesday, June 22, 2011 9:26 AM

All replies

  • Hi Subba Y,

     

    Welcome to the MSDN forum.

     

    According to your description, I think we can modify a little code of the suggested method to achieve your purpose.

     

    We can define the MyArg class as below:

    [ComVisible(true)]

    public class MyArg
    {
               public string name;
               public string city ;

    public MyArg(string Name, string City)

    {

    this.name=Name;

    this.city=City;

    }

    }

     

    Then we can instance COM object and call the Open method as below:

    MyClass myObject = new MyClass();

    myObject.Open(new MyArg("Bob","Boston"));

     

    I think this can help you.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Tuesday, June 28, 2011 8:08 AM
    Wednesday, June 22, 2011 9:26 AM
  •  

    Hi,

     

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

     

    If you have got answers, please remember to mark answer.

    If not, any more concerns, please feel free to let us know.

     

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 27, 2011 2:22 AM