Wrapper for unmanaged virtual methods probs

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

    Wrapper for unmanaged virtual methods probs


    Hello,

    I noticed an unexplained (for me) behaviour while wrapping unmanaged virtual
    methods.

    This is the test situation:
    An unmanaged class has four methods defined DoIt1 .. DoIt4. Each method just
    returns false.
    DoIt3 and DoIt4 are virtual methods defined in the base class MyBase of my
    unmanaged code.
    MyClass is an unmanaged class I derived from MyBase.

    MMyClass is the managed wrapper class for MyClass.

    I only get the correct return value, when I explicitly give the name of
    MyClass when calling the DoItx method. See code parts below.
    Does anyone know the reason?

    Thank you
    Carl


    ---------------------------------------------------------

    class __declspec(dlle xport) MyBase
    {
    public:
    bool DoIt2 (void) { return false ; }
    virtual bool DoIt3 (void) { return false ; }
    virtual bool DoIt4 (void) = 0 ;
    } ;

    ---------------------------------------------------------

    class __declspec(dlle xport) MyClass : public MyBase
    {
    public:
    bool DoIt1 (void) { return false ; }
    bool DoIt3 (void) { return false ; }
    bool DoIt4 (void) { return false ; }
    } ;

    ---------------------------------------------------------

    #include "..\UnManagedCo de\MyClass.h"

    using namespace System ;

    public __gc class MMyClass

    {

    public:
    MMyClass (Void) { m_MyClass = new MyClass() ; } ;
    ~MMyClass (Void) { delete m_MyClass ; } ;

    Boolean DoIt1 (Void) { return m_MyClass->DoIt1(); } ; // returns false --[color=blue]
    > Ok.[/color]
    Boolean DoIt2 (Void) { return m_MyClass->DoIt2(); } ; // returns false --[color=blue]
    > Ok.[/color]
    Boolean DoIt3 (Void) { return m_MyClass->DoIt3(); } ; // returns true --[color=blue]
    > !!!!!!!! Oops.[/color]
    Boolean DoIt4 (Void) { return m_MyClass->DoIt4(); } ; // returns true --[color=blue]
    > !!!!!!!! Oops.[/color]

    Boolean DoIt5 (Void) { return m_MyClass->MyClass::DoIt1 (); } ; // returns
    false -- > Ok.
    Boolean DoIt6 (Void) { return m_MyClass->MyClass::DoIt2 (); } ; // returns
    false -- > Ok.
    Boolean DoIt7 (Void) { return m_MyClass->MyClass::DoIt3 (); } ; // returns
    false -- > Ok.
    Boolean DoIt8 (Void) { return m_MyClass->MyClass::DoIt4 (); } ; // returns
    false -- > Ok.

    private:
    MyClass __nogc * m_MyClass ;
    } ;
  • William DePalo [MVP VC++]

    #2
    Re: Wrapper for unmanaged virtual methods probs

    "Carl" <Carl@discussio ns.microsoft.co m> wrote in message
    news:92AE0123-E5D7-4857-99E4-5B3A4C9DE02B@mi crosoft.com...[color=blue]
    > I noticed an unexplained (for me) behaviour while wrapping unmanaged
    > virtual
    > methods.
    >
    > This is the test situation:
    > An unmanaged class has four methods defined DoIt1 .. DoIt4. Each method
    > just
    > returns false.
    > ...
    > I only get the correct return value, when I explicitly give the name of
    > MyClass when calling the DoItx method. See code parts below.
    > Does anyone know the reason?[/color]

    Well, there is a problem marshalling booleans between managed and unmanaged
    code. I'm not sure if it is the cause of your grief. But you can insert this
    line

    _asm xor eax, eax

    just before this line

    return false;

    in your code.

    If that doesn't help you, please post again and perhaps someone will have a
    better idea.

    Regards,
    Will



    Comment

    • Marcus Heege

      #3
      Re: Wrapper for unmanaged virtual methods probs

      You are describing the so called virtual bool bug. The next VS service pack
      will fix this problem. You can also download a hotfix from [1]

      Marcus Heege

      [1] http://support.microsoft.com/kb/823071/en-us


      Comment

      • William DePalo [MVP VC++]

        #4
        Re: Wrapper for unmanaged virtual methods probs

        "Marcus Heege" <NOSPAM@heege.n et> wrote in message
        news:%23p2Sycmk FHA.2852@TK2MSF TNGP15.phx.gbl. ..[color=blue]
        > You are describing the so called virtual bool bug. The next VS service
        > pack will fix this problem. You can also download a hotfix from [1]
        >
        > Marcus Heege
        >
        > [1] http://support.microsoft.com/kb/823071/en-us[/color]

        Just for the sake of completeness, note that a method does not have to be
        virtual to exhibit this behavior.

        Regards,
        Will


        Comment

        • Nishant Sivakumar

          #5
          Re: Wrapper for unmanaged virtual methods probs

          See http://www.codeproject.com/buglist/virtualboolbug.asp

          --
          Regards,
          Nish [VC++ MVP]
          Nish’s thoughts on technology leadership, cloud architecture, and APIs, among other things




          "Carl" <Carl@discussio ns.microsoft.co m> wrote in message
          news:92AE0123-E5D7-4857-99E4-5B3A4C9DE02B@mi crosoft.com...[color=blue]
          >
          > Hello,
          >
          > I noticed an unexplained (for me) behaviour while wrapping unmanaged
          > virtual
          > methods.
          >
          > This is the test situation:
          > An unmanaged class has four methods defined DoIt1 .. DoIt4. Each method
          > just
          > returns false.
          > DoIt3 and DoIt4 are virtual methods defined in the base class MyBase of my
          > unmanaged code.
          > MyClass is an unmanaged class I derived from MyBase.
          >
          > MMyClass is the managed wrapper class for MyClass.
          >
          > I only get the correct return value, when I explicitly give the name of
          > MyClass when calling the DoItx method. See code parts below.
          > Does anyone know the reason?
          >
          > Thank you
          > Carl
          >
          >
          > ---------------------------------------------------------
          >
          > class __declspec(dlle xport) MyBase
          > {
          > public:
          > bool DoIt2 (void) { return false ; }
          > virtual bool DoIt3 (void) { return false ; }
          > virtual bool DoIt4 (void) = 0 ;
          > } ;
          >
          > ---------------------------------------------------------
          >
          > class __declspec(dlle xport) MyClass : public MyBase
          > {
          > public:
          > bool DoIt1 (void) { return false ; }
          > bool DoIt3 (void) { return false ; }
          > bool DoIt4 (void) { return false ; }
          > } ;
          >
          > ---------------------------------------------------------
          >
          > #include "..\UnManagedCo de\MyClass.h"
          >
          > using namespace System ;
          >
          > public __gc class MMyClass
          >
          > {
          >
          > public:
          > MMyClass (Void) { m_MyClass = new MyClass() ; } ;
          > ~MMyClass (Void) { delete m_MyClass ; } ;
          >
          > Boolean DoIt1 (Void) { return m_MyClass->DoIt1(); } ; // returns false --[color=green]
          >> Ok.[/color]
          > Boolean DoIt2 (Void) { return m_MyClass->DoIt2(); } ; // returns false --[color=green]
          >> Ok.[/color]
          > Boolean DoIt3 (Void) { return m_MyClass->DoIt3(); } ; // returns true --[color=green]
          >> !!!!!!!! Oops.[/color]
          > Boolean DoIt4 (Void) { return m_MyClass->DoIt4(); } ; // returns true --[color=green]
          >> !!!!!!!! Oops.[/color]
          >
          > Boolean DoIt5 (Void) { return m_MyClass->MyClass::DoIt1 (); } ; // returns
          > false -- > Ok.
          > Boolean DoIt6 (Void) { return m_MyClass->MyClass::DoIt2 (); } ; // returns
          > false -- > Ok.
          > Boolean DoIt7 (Void) { return m_MyClass->MyClass::DoIt3 (); } ; // returns
          > false -- > Ok.
          > Boolean DoIt8 (Void) { return m_MyClass->MyClass::DoIt4 (); } ; // returns
          > false -- > Ok.
          >
          > private:
          > MyClass __nogc * m_MyClass ;
          > } ;[/color]


          Comment

          • Carl

            #6
            RE: Wrapper for unmanaged virtual methods probs


            Thanks a lot. The mentioned articles described my problem.

            But I'm still shocked and very Very VERY concerned about other bugs like
            this in my code which I don't know about yet.

            The workaround seems not to be easy if you can't change the unmanaged code.
            I will see if I can get the Microsoft hotfix.

            Is there a list somewhere about all the bugs you better should know about?

            Comment

            • Nishant Sivakumar

              #7
              Re: Wrapper for unmanaged virtual methods probs

              It's been fixed in VC++ 2005 (as of Beta 2)

              --
              Regards,
              Nish [VC++ MVP]
              Nish’s thoughts on technology leadership, cloud architecture, and APIs, among other things




              "Carl" <Carl@discussio ns.microsoft.co m> wrote in message
              news:0428C270-A1EE-49F7-BC36-61D02DBD577B@mi crosoft.com...[color=blue]
              >
              > Thanks a lot. The mentioned articles described my problem.
              >
              > But I'm still shocked and very Very VERY concerned about other bugs like
              > this in my code which I don't know about yet.
              >
              > The workaround seems not to be easy if you can't change the unmanaged
              > code.
              > I will see if I can get the Microsoft hotfix.
              >
              > Is there a list somewhere about all the bugs you better should know about?
              >[/color]


              Comment

              • Carl

                #8
                Re: Wrapper for unmanaged virtual methods probs


                Good to know, but I don't see that as an acceptable solution for the problem.

                "I'm not going to buy a new car just because the cigarette lighter is broken."


                "Nishant Sivakumar" wrote:
                [color=blue]
                > It's been fixed in VC++ 2005 (as of Beta 2)[/color]

                Comment

                Working...