Search code examples
referencec++-cliclrpass-by-referencemanaged

C++/CLI from tracking reference to (native) reference - wrapping


I need a C# interface to call some native C++ code via the CLI dialect. The C# interface uses the out attribute specifier in front of the required parameters. That translates to a % tracking reference in C++/CLI.

The method I has the following signature and body (it is calling another native method to do the job):

virtual void __clrcall GetMetrics(unsigned int %width, unsigned int %height, unsigned int %colourDepth, int %left, int %top) sealed
{
    mRenderWindow->getMetrics(width, height, colourDepth, left, top);
}

Now the code won't compile because of a few compile time errors (all being related to not being able to convert parameter 1 from 'unsigned int' to 'unsigned int &').

As a modest C++ programmer, to me CLI is looking like Dutch to a German speaker. What can be done to make this wrapper work properly in CLI?


Solution

  • Like it was also suggested in a deleted answer, I did the obvious and used local variables to pass the relevant values around:

     virtual void __clrcall GetMetrics(unsigned int %width, unsigned int %height, unsigned int %colourDepth, int %left, int %top) sealed
            {
                unsigned int w = width, h = height, c = colourDepth;
                int l = left, t = top;
                mRenderWindow->getMetrics(w, h, c, l, t);
                width = w; height = h; colourDepth = c; left = l; top = t;
            }
    

    It was a bit obvious since the rather intuitive mechanism of tracked references: they're affected by the garbage collector's work and are not really that static/constant as normal &references when they're prone to be put somewhere else in memory. Thus this is the only way reliable enough to overcome the issue. Thanks to the initial answer.