problem with bool in unmanaged/managed interop

  • Thread starter Thread starter Ian
  • Start date Start date
I

Ian

When I have an unmanaged class (compiled without /clr or
with #pragma unmanaged) with return type virtual bool,
the return value always comes out true when called in
managed code. Only happens with virtual and bool. Sample
code below.

Is anyone aware of a fix, workaround, etc?

Sample Code:
/*-----------------------------------------------------*/
#pragma unmanaged

struct test1
{
bool value() { return false; }
};

struct test2
{
virtual bool value() { return false; }
};

struct test3
{
virtual unsigned char value() { return false; }
};

struct test4
{
virtual bool value() { return true; }
};

struct test6
{
bool value();
};

bool test6::value() { return false; }

template<class T>
bool value( T * t ) { return t->value(); }

#pragma managed

#using <mscorlib.dll>

__nogc struct test5
{
virtual bool value() { return false; }
};



int main()
{
test1 *t1 = new test1();
test2 *t2 = new test2();
test3 *t3 = new test3();
test4 *t4 = new test4();
test5 *t5 = new test5();
test6 *t6 = new test6();

System::Console::WriteLine("t1 value: {0} ;
should be {1}", __box(t1->value()), __box(value(t1)));
System::Console::WriteLine("t2 value: {0} ;
should be {1}", __box(t2->value()), __box(value(t2)));
System::Console::WriteLine("t3 value: {0} ;
should be {1}", __box(bool(t3->value())), __box(value
(t3)));
System::Console::WriteLine("t4 value: {0} ;
should be {1}", __box(t4->value()), __box(value(t4)));
System::Console::WriteLine("t5 value: {0} ;
should be {1}", __box(t5->value()), __box(value(t5)));
System::Console::WriteLine("t6 value: {0} ;
should be {1}", __box(t6->value()), __box(value(t6)));


System::Console::WriteLine( "Done" );

while(1);

}
/*-----------------------------------------------------*/
 
Back
Top