pinvoke double[] to Intptr , passing double as IntPtr ??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sagaert Johan

    pinvoke double[] to Intptr , passing double as IntPtr ??

    Hi

    I am struggling with pinvoke

    I have dll a function that is declared as :
    sf_command (IntPtr sndfile,int command, IntPtr data, int datasize);

    i need to pass a pointer to a double in the data parameter, i now did :

    IntPtr mem = Marshal.AllocHG lobal(sizeof(do uble));

    sf_command (sndfile,comman d, mem, sizeof(double)) ;

    then i did

    byte[] ba=new byte[sizeof (Double)];

    ba[0]=Marshal.ReadBy te(mem,0);
    ba[1]=Marshal.ReadBy te(mem,1);
    ba[2]=Marshal.ReadBy te(mem,2);
    ba[3]=Marshal.ReadBy te(mem,3);
    ba[4] = Marshal.ReadByt e(mem, 4);
    ba[5] = Marshal.ReadByt e(mem, 5);
    ba[6] = Marshal.ReadByt e(mem, 6);
    ba[7] = Marshal.ReadByt e(mem, 7);

    max_val = BitConverter.To Double(ba,0);

    Marshal.FreeHGl obal(mem);


    this works ,but i think there should be a shorter way ??

    second question :
    How do i pass an array doubles to my function ?


    Johan


  • Ben Voigt [C++ MVP]

    #2
    Re: pinvoke double[] to Intptr , passing double as IntPtr ??

    Sagaert Johan wrote:
    Hi
    >
    I am struggling with pinvoke
    >
    I have dll a function that is declared as :
    sf_command (IntPtr sndfile,int command, IntPtr data, int datasize);
    >
    i need to pass a pointer to a double in the data parameter, i now did
    :
    IntPtr mem = Marshal.AllocHG lobal(sizeof(do uble));
    >
    sf_command (sndfile,comman d, mem, sizeof(double)) ;
    >
    then i did
    >
    byte[] ba=new byte[sizeof (Double)];
    >
    ba[0]=Marshal.ReadBy te(mem,0);
    ba[1]=Marshal.ReadBy te(mem,1);
    ba[2]=Marshal.ReadBy te(mem,2);
    ba[3]=Marshal.ReadBy te(mem,3);
    ba[4] = Marshal.ReadByt e(mem, 4);
    ba[5] = Marshal.ReadByt e(mem, 5);
    ba[6] = Marshal.ReadByt e(mem, 6);
    ba[7] = Marshal.ReadByt e(mem, 7);
    >
    max_val = BitConverter.To Double(ba,0);
    >
    Marshal.FreeHGl obal(mem);
    >
    >
    this works ,but i think there should be a shorter way ??
    >
    second question :
    How do i pass an array doubles to my function ?
    Did you try the obvious solution of using either "double[]" or "ref double"
    as the type of the argument in the DllImport?
    >
    >
    Johan

    Comment

    • Pavel Minaev

      #3
      Re: pinvoke double[] to Intptr , passing double as IntPtr ??

      On Nov 20, 11:25 pm, "Sagaert Johan" <REMOVEsagaer.. .@hotmail.com>
      wrote:
      Hi
      >
      I am struggling with pinvoke
      >
      I  have dll a function that is declared as :
      sf_command (IntPtr sndfile,int command, IntPtr data, int datasize);
      >
      i need to pass a pointer to a double in the data parameter, i now did :
      >
      IntPtr mem = Marshal.AllocHG lobal(sizeof(do uble));
      >
      sf_command (sndfile,comman d, mem, sizeof(double)) ;
      >
      then  i did
      >
      byte[] ba=new byte[sizeof (Double)];
      >
      ba[0]=Marshal.ReadBy te(mem,0);
      ba[1]=Marshal.ReadBy te(mem,1);
      ba[2]=Marshal.ReadBy te(mem,2);
      ba[3]=Marshal.ReadBy te(mem,3);
      ba[4] = Marshal.ReadByt e(mem, 4);
      ba[5] = Marshal.ReadByt e(mem, 5);
      ba[6] = Marshal.ReadByt e(mem, 6);
      ba[7] = Marshal.ReadByt e(mem, 7);
      >
      max_val = BitConverter.To Double(ba,0);
      >
      Marshal.FreeHGl obal(mem);
      >
      this works ,but i think there should be a shorter way ??
      Yes. For one thing, you can just declare the parameter as double[] in
      the P/Invoke declaration for your function (or "ref double[]" if the
      function is going to write some data into that array). For another,
      you can use pointers:

      double* p = (double*)mem;
      Console.WriteLi ne(*p);

      of course, this necessitates the use of "/unsafe", but there's nothing
      inherently wrong about it. Pointers exist in C# for a reason, so don't
      be afraid to use them for interop.

      second question :
      How do i pass an array doubles to my function ?
      >
      Johan

      Comment

      • Sagaert Johan

        #4
        Re: pinvoke double[] to Intptr , passing double as IntPtr ??

        Hi
        casting double does not work (IntPtr) double

        Changing my pinvoke declaration from IntPtr to ref double is no option,
        since in other case i need to pass pointer to other data type too.

        I did find this is a cleaner alternative (since a double is in fact a
        structure type )

        IntPtr mem = Marshal.AllocHG lobal(sizeof(do uble));

        int err = sf_command(Hsou ndfile, SFCommand.SFC_C ALC_NORM_SIGNAL _MAX, mem,
        sizeof(double)) ; CheckError(err) ;

        double max_val = (double)Marshal .PtrToStructure (mem, typeof(System.D ouble));
        Marshal.FreeHGl obal(mem); return max_val;



        "Ben Voigt [C++ MVP]" <rbv@nospam.nos pamwrote in message
        news:u4eSVu1SJH A.5080@TK2MSFTN GP03.phx.gbl...
        Sagaert Johan wrote:
        >Hi
        >>
        >I am struggling with pinvoke
        >>
        >I have dll a function that is declared as :
        >sf_command (IntPtr sndfile,int command, IntPtr data, int datasize);
        >>
        >i need to pass a pointer to a double in the data parameter, i now did
        >:
        >IntPtr mem = Marshal.AllocHG lobal(sizeof(do uble));
        >>
        >sf_command (sndfile,comman d, mem, sizeof(double)) ;
        >>
        >then i did
        >>
        >byte[] ba=new byte[sizeof (Double)];
        >>
        >ba[0]=Marshal.ReadBy te(mem,0);
        >ba[1]=Marshal.ReadBy te(mem,1);
        >ba[2]=Marshal.ReadBy te(mem,2);
        >ba[3]=Marshal.ReadBy te(mem,3);
        >ba[4] = Marshal.ReadByt e(mem, 4);
        >ba[5] = Marshal.ReadByt e(mem, 5);
        >ba[6] = Marshal.ReadByt e(mem, 6);
        >ba[7] = Marshal.ReadByt e(mem, 7);
        >>
        >max_val = BitConverter.To Double(ba,0);
        >>
        >Marshal.FreeHG lobal(mem);
        >>
        >>
        >this works ,but i think there should be a shorter way ??
        >>
        >second question :
        >How do i pass an array doubles to my function ?
        >
        Did you try the obvious solution of using either "double[]" or "ref
        double" as the type of the argument in the DllImport?
        >
        >>
        >>
        >Johan
        >
        >

        Comment

        • Ben Voigt [C++ MVP]

          #5
          Re: pinvoke double[] to Intptr , passing double as IntPtr ??



          "Sagaert Johan" <REMOVEsagaert_ j@hotmail.comwr ote in message
          news:Oy2$QpBTJH A.5056@TK2MSFTN GP04.phx.gbl...
          Hi
          casting double does not work (IntPtr) double
          >
          Changing my pinvoke declaration from IntPtr to ref double is no option,
          since in other case i need to pass pointer to other data type too.
          You can have multiple p/invoke declarations for the same function with
          different argument lists, just like any overloaded function.


          Comment

          Working...