C++/CLI parameter type conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vijay.gandhi@gmail.com

    #1

    C++/CLI parameter type conversion

    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(d ouble* x).

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

    I want my ComputeMedianIn terface() 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 ComputeMedianIn terface() and then
    passing that as an argument to ComputeMedian() .

    Is there any better way?

    Thanks!
    Vijay.

  • David Lowndes

    #2
    Re: C++/CLI parameter type conversion

    >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:
    >ComputeMedianI nterface(array< double>^ x).
    >
    >I want my ComputeMedianIn terface() 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 ComputeMedianIn terface() and then
    >passing that as an argument to ComputeMedian() .
    >
    >Is there any better way?
    Have a look at the pin_ptr documentation.

    Dave

    Comment

    • Bruno van Dooren [MVP VC++]

      #3
      Re: C++/CLI parameter type conversion

      I want my ComputeMedianIn terface() 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 ComputeMedianIn terface() 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<doublep = &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
      bruno_nos_pam_v an_dooren@hotma il.com
      Remove only "_nos_pam"


      Comment

      • vijay.gandhi@gmail.com

        #4
        Re: C++/CLI parameter type conversion

        Thanks to you both. I got it working!
        Vijay.

        On Feb 26, 2:11 am, "Bruno van Dooren [MVP VC++]"
        <bruno_nos_pam_ van_doo...@hotm ail.comwrote:
        I want my ComputeMedianIn terface() 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 ComputeMedianIn terface() 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<doublep = &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
        bruno_nos_pam_v an_doo...@hotma il.com
        Remove only "_nos_pam"

        Comment

        Working...