C++/CLI parameter type conversion

  • Thread starter Thread starter vijay.gandhi
  • Start date Start date
V

vijay.gandhi

Hello,

I am trying to write a wrapper function in C++/CLI over an existing
function in C++.

For instance, assume the C++ function is:
ComputeMedian(double* x).

To create a C++/CLI wrapper, I created a function in C++/CLI which
looks like:
ComputeMedianInterface(array<double>^ x).

I want my ComputeMedianInterface() function to call ComputeMedian()
internally. I somehow want to convert the input values array<double>^x
to double* x so that I do not have to modify the code within
ComputeMedian().

The only way I could think of is by creating a copy (using malloc so
that I get the pointer) within ComputeMedianInterface() and then
passing that as an argument to ComputeMedian().

Is there any better way?

Thanks!
Vijay.
 
I am trying to write a wrapper function in C++/CLI over an existing
function in C++.

For instance, assume the C++ function is:
ComputeMedian(double* x).

To create a C++/CLI wrapper, I created a function in C++/CLI which
looks like:
ComputeMedianInterface(array<double>^ x).

I want my ComputeMedianInterface() function to call ComputeMedian()
internally. I somehow want to convert the input values array<double>^x
to double* x so that I do not have to modify the code within
ComputeMedian().

The only way I could think of is by creating a copy (using malloc so
that I get the pointer) within ComputeMedianInterface() and then
passing that as an argument to ComputeMedian().

Is there any better way?

Have a look at the pin_ptr documentation.

Dave
 
I want my ComputeMedianInterface() function to call ComputeMedian()
internally. I somehow want to convert the input values array<double>^x
to double* x so that I do not have to modify the code within
ComputeMedian().

The only way I could think of is by creating a copy (using malloc so
that I get the pointer) within ComputeMedianInterface() and then
passing that as an argument to ComputeMedian().

Is there any better way?

Hi,

You can do that very easily like this:
pin_ptr<double> p = &arr[0];

this will pin the managed array into memory so that it doesn't get moved
around by the GC, and will allow you to access the elements in the managed
array through a native pointer p.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
Thanks to you both. I got it working!
Vijay.

I want my ComputeMedianInterface() function to call ComputeMedian()
internally. I somehow want to convert the input values array<double>^x
to double* x so that I do not have to modify the code within
ComputeMedian().
The only way I could think of is by creating a copy (using malloc so
that I get the pointer) within ComputeMedianInterface() and then
passing that as an argument to ComputeMedian().
Is there any better way?

Hi,

You can do that very easily like this:
pin_ptr<double> p = &arr[0];

this will pin the managed array into memory so that it doesn't get moved
around by the GC, and will allow you to access the elements in the managed
array through a native pointer p.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
Back
Top