1
votes

I have an unmanaged library which I want to use from a managed class. The interface of the function is:

GetProgress(short* value);

So I wrote in my managed class:

short val = 0;
GetProgress(&val);

I got the following error:

Error C2664: 'GetProgress' : cannot convert parameter 1 from 'cli::interior_ptr' in 'short *' with [ Type=short ]

I read this topic, so I changed my code into:

short val = 0;
pin_ptr<short*> pVal = &val;
GetProgress(pVal);

And in addition to the previous error I get

Error C2440: 'initialisation' : cannot convert from 'short *' to 'cli::pin_ptr' with [ Type=short * ]

How can I fix this?

1

1 Answers

1
votes

That's an interesting one.

The following code produces C2664 because val can only be a managed type:

using namespace System;

void GetProgress(short* value)
{
    // unmanaged goodness
}

ref class XYZ : System::Object
{
    short val;

    void foo()
    {
        GetProgress(&val);
    }
};

but if you declare a local variable first, it all works fine...

using namespace System;

void GetProgress(short* value)
{
    // unmanaged goodness
}

ref class XYZ : System::Object
{
    short val;

    void foo()
    {
        short x;
        GetProgress(&x);
        val = x;
    }
};

Not exactly the answer you were looking for, but I thought I'd include it since it's a simple fix.