[Coin-discuss] Using COIN code with .NET

Jan-Willem Goossens j.goossens at t75.nl
Fri Oct 28 14:17:42 EDT 2005


Hi all,

Everybody NOT using COIN code under microsoft .NET VS 2003 can stop reading 
right now..

However, for those (if there are any..) like me who use the COIN sources to 
make an unmanaged library, and link that into a .NET managed (mixed) (C++) 
dll, this could be important:

In short:  "if the unmanaged class has a virtual function that returns a 
bool, then irrespective of what value it returns, the managed caller 
*always* gets back true. "

see also: http://www.codeproject.com/buglist/virtualboolbug.asp

An example of where this happens is if your managed code calls the Osi solve 
interface functions like isProvenOptimal etc. In essence, my code looked 
(..) something like this:


    OsiSolverInterface *solver = new OsiClpSolverInterface();
....

    bool returnValue = solver->isProvenOptimal();

At some point I noticed that the value of "returnValue" was always True, 
even if on the isProvenOptimal-side False was returned..

The workaround I use is a variation of the one presented at the site 
mentioned above:
(include in the managed code)

    #pragma unmanaged
    //this is a workaround for the "virtual bool bug". The marshaller 
inproperly marshals
    //bool return values on virtual functions. The return value is always 
true.
    template <class T> int virtual_bool_workaround(T* imp,bool (T::*mf)() 
const)
    {
        __asm mov eax, 0; //this shouldn't be ncessary but #pragma unmanaged 
doesn't work
        return (imp->*mf)();
    }
    #pragma managed

(see http://www.pcreview.co.uk/forums/thread-1428005.php)
and then use

    bool returnValue = 
!!virtual_bool_workaround(solver,OsiSolverInterface::isProvenOptimal);

in stead of the original lines.
I'm not really into using __asm, so dont ask me how this works, but some 
tests show that indeed now the returnValue is always what it's supposed to 
be.

Hope this helps.
Regards,

Jan-Willem












More information about the Coin-discuss mailing list