VARIANT type problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ZhangZQ

    #1

    VARIANT type problem

    If I have a method in an OCX, the definition is

    [id(5), helpstring("Met hod Test1")] void Test1(VARIANT* a);

    After I add reference to this OCX in Visual studio .net, it generates 2
    dlls, and I look at the object browser, the definition of the above method
    in the wrapper class is

    Test1(ref object);

    Now I use the C# to call this method,

    object myObj = new object();
    MyOCX.Test1(ref myObj); // Then it will throw an exception with error
    message "type mismatch"

    and no matter which value I set to myObj it always throws the exception with
    the same error message.


    I want to know how to call that
    [id(5), helpstring("Met hod Test1")] void Test1(VARIANT* a); method?


    Thank you very much!


  • ZhangZQ

    #2
    Re: VARIANT type problem

    This is the IL of that RCW

    .method public hidebysig newslot virtual
    instance void Test1(object& a) cil managed
    {
    .maxstack 3
    IL_0000: ldarg.0
    IL_0001: ldfld class
    [Interop.ocx0308 20Lib]ocx030820Lib._D ocx030820
    Axocx030820Lib. Axocx030820::oc x
    IL_0006: brtrue.s IL_0014

    IL_0008: ldstr "Test1"
    IL_000d: ldc.i4.0
    IL_000e: newobj instance void
    [System.Windows. Forms]System.Windows. Forms.AxHost/InvalidActiveXS tateExcepti
    on::.ctor(strin g,

    valuetype
    [System.Windows. Forms]System.Windows. Forms.AxHost/ActiveXInvokeKi nd)
    IL_0013: throw

    IL_0014: ldarg.0
    IL_0015: ldfld class
    [Interop.ocx0308 20Lib]ocx030820Lib._D ocx030820
    Axocx030820Lib. Axocx030820::oc x
    IL_001a: ldarg.1
    IL_001b: callvirt instance void
    [Interop.ocx0308 20Lib]ocx030820Lib._D ocx030820::Test 1(object&)
    IL_0020: ret
    } // end of method Axocx030820::Te st1

    The exception will be thrown at IL_011b.



    "ZhangZQ" <zhangzq71@hotm ail.com> wrote in message
    news:OqueiNnoDH A.1948@TK2MSFTN GP12.phx.gbl...[color=blue]
    > If I have a method in an OCX, the definition is
    >
    > [id(5), helpstring("Met hod Test1")] void Test1(VARIANT* a);
    >
    > After I add reference to this OCX in Visual studio .net, it generates 2
    > dlls, and I look at the object browser, the definition of the above method
    > in the wrapper class is
    >
    > Test1(ref object);
    >
    > Now I use the C# to call this method,
    >
    > object myObj = new object();
    > MyOCX.Test1(ref myObj); // Then it will throw an exception with error
    > message "type mismatch"
    >
    > and no matter which value I set to myObj it always throws the exception[/color]
    with[color=blue]
    > the same error message.
    >
    >
    > I want to know how to call that
    > [id(5), helpstring("Met hod Test1")] void Test1(VARIANT* a); method?
    >
    >
    > Thank you very much!
    >
    >[/color]


    Comment

    • ZhangZQ

      #3
      Re: VARIANT type problem

      This is the IL of that RCW

      .method public hidebysig newslot virtual
      instance void Test1(object& a) cil managed
      {
      .maxstack 3
      IL_0000: ldarg.0
      IL_0001: ldfld class
      [Interop.ocx0308 20Lib]ocx030820Lib._D ocx030820
      Axocx030820Lib. Axocx030820::oc x
      IL_0006: brtrue.s IL_0014

      IL_0008: ldstr "Test1"
      IL_000d: ldc.i4.0
      IL_000e: newobj instance void
      [System.Windows. Forms]System.Windows. Forms.AxHost/InvalidActiveXS tateExcepti
      on::.ctor(strin g,

      valuetype
      [System.Windows. Forms]System.Windows. Forms.AxHost/ActiveXInvokeKi nd)
      IL_0013: throw

      IL_0014: ldarg.0
      IL_0015: ldfld class
      [Interop.ocx0308 20Lib]ocx030820Lib._D ocx030820
      Axocx030820Lib. Axocx030820::oc x
      IL_001a: ldarg.1
      IL_001b: callvirt instance void
      [Interop.ocx0308 20Lib]ocx030820Lib._D ocx030820::Test 1(object&)
      IL_0020: ret
      } // end of method Axocx030820::Te st1

      The exception will be thrown at IL_011b.



      "ZhangZQ" <zhangzq71@hotm ail.com> wrote in message
      news:OqueiNnoDH A.1948@TK2MSFTN GP12.phx.gbl...[color=blue]
      > If I have a method in an OCX, the definition is
      >
      > [id(5), helpstring("Met hod Test1")] void Test1(VARIANT* a);
      >
      > After I add reference to this OCX in Visual studio .net, it generates 2
      > dlls, and I look at the object browser, the definition of the above method
      > in the wrapper class is
      >
      > Test1(ref object);
      >
      > Now I use the C# to call this method,
      >
      > object myObj = new object();
      > MyOCX.Test1(ref myObj); // Then it will throw an exception with error
      > message "type mismatch"
      >
      > and no matter which value I set to myObj it always throws the exception[/color]
      with[color=blue]
      > the same error message.
      >
      >
      > I want to know how to call that
      > [id(5), helpstring("Met hod Test1")] void Test1(VARIANT* a); method?
      >
      >
      > Thank you very much!
      >
      >[/color]


      Comment

      • Peter Koen

        #4
        Re: VARIANT type problem

        "ZhangZQ" <zhangzq71@hotm ail.com> wrote in
        news:OqueiNnoDH A.1948@TK2MSFTN GP12.phx.gbl:
        [color=blue]
        > If I have a method in an OCX, the definition is
        >
        > [id(5), helpstring("Met hod Test1")] void Test1(VARIANT* a);
        >
        > After I add reference to this OCX in Visual studio .net, it generates
        > 2 dlls, and I look at the object browser, the definition of the above
        > method in the wrapper class is
        >
        > Test1(ref object);
        >
        > Now I use the C# to call this method,
        >
        > object myObj = new object();
        > MyOCX.Test1(ref myObj); // Then it will throw an exception with
        > error message "type mismatch"
        >
        > and no matter which value I set to myObj it always throws the
        > exception with the same error message.
        >
        >
        > I want to know how to call that
        > [id(5), helpstring("Met hod Test1")] void Test1(VARIANT* a); method?
        >
        >
        > Thank you very much!
        >
        >[/color]

        Hi,

        Either you use Convert.TOxxxx to get the value you want from that object
        or you make your own wrapper. For that you will have to use the
        MarshalAsAttrib ute to tell the Runtime what type you really want to pass.

        Details about the mapping of the different datatypes can be found here:

        http://msdn.microsoft.com/library/en-
        us/cpguide/html/cpconcomdatatyp es.asp

        Further Information about Marshaling can also be found here:
        http://msdn.microsoft.com/library/en-
        us/cpguide/html/cpconmarshaling datawithcominte rop.asp

        --
        ------ooo---OOO---ooo------

        Peter Koen - www.kema.at
        MCAD CAI/RS CASE/RS IAT

        ------ooo---OOO---ooo------

        Comment

        • Peter Koen

          #5
          Re: VARIANT type problem

          "ZhangZQ" <zhangzq71@hotm ail.com> wrote in
          news:OqueiNnoDH A.1948@TK2MSFTN GP12.phx.gbl:
          [color=blue]
          > If I have a method in an OCX, the definition is
          >
          > [id(5), helpstring("Met hod Test1")] void Test1(VARIANT* a);
          >
          > After I add reference to this OCX in Visual studio .net, it generates
          > 2 dlls, and I look at the object browser, the definition of the above
          > method in the wrapper class is
          >
          > Test1(ref object);
          >
          > Now I use the C# to call this method,
          >
          > object myObj = new object();
          > MyOCX.Test1(ref myObj); // Then it will throw an exception with
          > error message "type mismatch"
          >
          > and no matter which value I set to myObj it always throws the
          > exception with the same error message.
          >
          >
          > I want to know how to call that
          > [id(5), helpstring("Met hod Test1")] void Test1(VARIANT* a); method?
          >
          >
          > Thank you very much!
          >
          >[/color]

          Hi,

          Either you use Convert.TOxxxx to get the value you want from that object
          or you make your own wrapper. For that you will have to use the
          MarshalAsAttrib ute to tell the Runtime what type you really want to pass.

          Details about the mapping of the different datatypes can be found here:

          http://msdn.microsoft.com/library/en-
          us/cpguide/html/cpconcomdatatyp es.asp

          Further Information about Marshaling can also be found here:
          http://msdn.microsoft.com/library/en-
          us/cpguide/html/cpconmarshaling datawithcominte rop.asp

          --
          ------ooo---OOO---ooo------

          Peter Koen - www.kema.at
          MCAD CAI/RS CASE/RS IAT

          ------ooo---OOO---ooo------

          Comment

          Working...