C++/CLI parameter type conversion

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.
 
D

David Lowndes

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
 
B

Bruno van Dooren [MVP VC++]

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"
 
V

vijay.gandhi

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top