>In coping data between managed and native code, I make use of the Marshal
>class methods, ReadInt32, Copy, StructureToPtr, etc. However, I also want to
>copy Double and Single values, but given that there's is no ReadSingle and
>ReadDouble, what's the best way to do the copy?
A couple of different options with varying performance.
- Use Marshal.Copy to a single[1] or double[1].
- Use Marshal.PtrToStructure. I believe it handles simple types as
well. If not you can use a dummy wrapper type:
struct YourDouble { public double d; }
- For doubles, use Marshal.ReadInt64 followed by
BitConverter.Int64BitsToDouble.
- Use the CopyMemory windows API.
[DllImport("kernel32.dll")]
static extern void RtlMoveMemory(out double dest, IntPtr src, int cb);
- If you use a language with pointer support (C# with unsafe code or
C++) you can cast to a real double* or single* and simply dereference
it.
Mattias

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Peter - 24 Nov 2007 03:40 GMT
Mattias,
Thanks for your reply.
I have gone the 'dummy wrapper' suggestion below.
You indicate that some of your suggestions are better performers than
others, are your suggestions in any particular order?
Thanks
Peter.
>>In coping data between managed and native code, I make use of the Marshal
>>class methods, ReadInt32, Copy, StructureToPtr, etc. However, I also want
[quoted text clipped - 24 lines]
>
> Mattias