Compiler bug when mixing managed and unmanaged code 
Author Message
 Compiler bug when mixing managed and unmanaged code

Let's say my main is compiled as managed (/clr). From it,
I call a function in another file compiled as not managed
(without /clr). When the function I call is virtual AND
its return type is bool, even if the function returns
false, the calling one always receives true !

If y change the return type from bool to, let's
say "unsigned char", everything works fine, even though
the generated assembler code seems very similar.

Is it a known bug ?  Is there a workaround ?

Here is a sample that demonstrates the bug:

==== c.h =======
class C
{
public:
   virtual bool VirtualFunction();

Quote:
};

==== c.cpp =====
// Compile without /clr

#include <c.h>

bool C::VirtualFunction()
{
return false;

Quote:
}

==== test.cpp =====
// Compile with /clr
// will print "true" !!! (instead of "false")

#include <c.h>
#include <stdio.h>

void main()
{
C* c = new C();

if (c->VirtualFunction())
   printf("true\n");
else
   printf("false\n");

Quote:
}



Sun, 03 Jul 2005 02:56:59 GMT  
 Compiler bug when mixing managed and unmanaged code

Yes, it is a known bug. I've reported this bug couple of
months ago and the response below shows the work around
(I've tested it and it works). I've also been told that
this problem will be fixed in the next release but I've
tested on Everett and the problem is still there (when
will this be fixed????).

Quote:
>-----Original Message-----

>This is a known bug.

>Workaroud is to set the EAX register to 255 or less
before the return
>'false' from unmanaged to managed code (one way to do
this):

>#pragma unmanaged
>int ForceEAX()
>{
>    //return 256; //A::F() returns incorrect true
>     return 255; //A::F() returns correct false
>}

>bool A::F()
>{
>    ForceEAX();
>    return false;
>}

>The lower byte of EAX is handled properly, it's only

when one of the upper

- Show quoted text -

Quote:
>24 bits is non-zero that the return value is bogus.

>Hope this helps.

>Thank you,
>Bobby Mattappally
>Microsoft VC++/C# Team

>This posting is provided "AS IS" with no warranties, and
confers no rights.

>-----Original Message-----
>Let's say my main is compiled as managed (/clr). From
it,
>I call a function in another file compiled as not
managed
>(without /clr). When the function I call is virtual AND
>its return type is bool, even if the function returns
>false, the calling one always receives true !

>If y change the return type from bool to, let's
>say "unsigned char", everything works fine, even though
>the generated assembler code seems very similar.

>Is it a known bug ?  Is there a workaround ?

>Here is a sample that demonstrates the bug:

>==== c.h =======
>class C
>{
>public:
>   virtual bool VirtualFunction();
>};

>==== c.cpp =====
>// Compile without /clr

>#include <c.h>

>bool C::VirtualFunction()
>{
>return false;
>}

>==== test.cpp =====
>// Compile with /clr
>// will print "true" !!! (instead of "false")

>#include <c.h>
>#include <stdio.h>

>void main()
>{
>C* c = new C();

>if (c->VirtualFunction())
>   printf("true\n");
>else
>   printf("false\n");
>}
>.



Sun, 03 Jul 2005 05:58:52 GMT  
 Compiler bug when mixing managed and unmanaged code

Quote:

> I've also been told that this problem will be fixed in the next release
> but I've tested on Everett and the problem is still there (when will this
> be fixed????).

We are fixing this problem right now. It is questionable whether the fix
will make it into the Everett RTM. If it isn't in RTM, the fix will be in
the first service pack.

I regret that this hasn't been addressed sooner.

--
Brandon Bray                                          Visual C++ Compiler
This posting is provided AS IS with no warranties, and confers no rights.



Mon, 04 Jul 2005 02:57:43 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Fatal Error C1010 in Mixing Managed C++ and Unmanaged C++ Code

2. System.ExecutionEngineException - mixing managed/unmanaged code

3. Mixing Managed and Unmanaged code

4. Overhead of mixing managed/unmanaged code

5. Debugging mixed managed/unmanaged c++ code

6. Possible vc++ bug: using c++ references to managed objects in unmanaged code

7. bug for transition of bools between managed and unmanaged code

8. mixing unmanaged and managed C++

9. Try to get DirectX started in the Mixed Managed and Unmanaged C++ Programming

10. VC7++ - Detection of memory leaks, mixed DLL (unmanaged/managed)

11. Mixing Managed and Unmanaged C++

12. Referencing data from unmanaged code to managed code in C++ Wrapper class

 

 
Powered by phpBB® Forum Software