none
Marshal single, double datatype RRS feed

  • Question

  • Is there a way to copy one single or double value to IntPtr. Marshal.Copy takes an array of double or single. I wish to transfer data from the managed code (which uses ADO.NET ) to a inproc COM object in the same apartment. I'm passing  the address of the field variables in COM object as IntPtr array to the managed code to transfer the whole row (fetched using DataReader) in one call from the managed assembly.

    Tuesday, September 22, 2009 5:10 PM

Answers

  • You can easily just wrap your single or double value into an array, and use Marshal.Copy's current overload.  For example, for a Single value:

        void CopyToPointer(IntPtr copyTarget, float myFloat)
        {
            Marhsall.Copy(new float[] { myFloat }, 0, copyTarget, 1);
        }


    The result of this will be identical, since you're copying by value in any case.

    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by eryang Monday, September 28, 2009 6:22 AM
    Tuesday, September 22, 2009 5:33 PM
    Moderator

All replies

  • You can easily just wrap your single or double value into an array, and use Marshal.Copy's current overload.  For example, for a Single value:

        void CopyToPointer(IntPtr copyTarget, float myFloat)
        {
            Marhsall.Copy(new float[] { myFloat }, 0, copyTarget, 1);
        }


    The result of this will be identical, since you're copying by value in any case.

    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by eryang Monday, September 28, 2009 6:22 AM
    Tuesday, September 22, 2009 5:33 PM
    Moderator
  • Will this not create a temprory array. The resultset from datareader could have millions of rows. So every row fetch that has single or double (they can have more then 1 field of single or double) column will result in creation of this temprory array. I was wondering if there is anyother way to do it since whole point of using INTPTR is to get optimal performance (by reducing the mananged calls from unmanged). Unless CLR is smart enough and in this case will not create a temprary array.
    Monday, September 28, 2009 10:47 PM