Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2005

Tip: Looking for answers? Try searching our database.

Wrapper for unmanaged virtual methods probs

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Carl - 26 Jul 2005 21:47 GMT
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(dllexport) MyBase
{
public:
         bool DoIt2 (void) { return false ; }
 virtual bool DoIt3 (void) { return false ; }  
 virtual bool DoIt4 (void) = 0 ;
} ;

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

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

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

#include "..\UnManagedCode\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 --
> Ok.
 Boolean DoIt2 (Void) { return m_MyClass->DoIt2(); } ; // returns false --
> Ok.
 Boolean DoIt3 (Void) { return m_MyClass->DoIt3(); } ; // returns true  --
> !!!!!!!! Oops.
 Boolean DoIt4 (Void) { return m_MyClass->DoIt4(); } ; // returns true  --
> !!!!!!!! Oops.

 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++] - 27 Jul 2005 00:42 GMT
> I noticed an unexplained (for me) behaviour while wrapping unmanaged
> virtual
[quoted text clipped - 8 lines]
> MyClass when calling the DoItx method. See code parts below.
> Does anyone know the reason?

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
Marcus Heege - 27 Jul 2005 05:53 GMT
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 
William DePalo [MVP VC++] - 27 Jul 2005 06:07 GMT
> 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

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

Regards,
Will
Nishant Sivakumar - 27 Jul 2005 06:39 GMT
See http://www.codeproject.com/buglist/virtualboolbug.asp

Signature

Regards,
Nish [VC++ MVP]
http://www.voidnish.com
http://blog.voidnish.com

> Hello,
>
[quoted text clipped - 74 lines]
>  MyClass __nogc * m_MyClass ;
> } ;
Carl - 27 Jul 2005 07:51 GMT
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?
Nishant Sivakumar - 27 Jul 2005 08:07 GMT
It's been fixed in VC++ 2005 (as of Beta 2)

Signature

Regards,
Nish [VC++ MVP]
http://www.voidnish.com
http://blog.voidnish.com

> Thanks a lot. The mentioned articles described my problem.
>
[quoted text clipped - 6 lines]
>
> Is there a list somewhere about all the bugs you better should know about?
Carl - 27 Jul 2005 08:21 GMT
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."

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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.