all groups > dotnet interop > november 2007 >
You're in the

dotnet interop

group:

Marshal a 'Single' or 'Double'


Re: Marshal a 'Single' or 'Double' Mattias Sjögren
11/23/2007 7:17:19 AM
dotnet interop:

[quoted text, click to view]

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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Marshal a 'Single' or 'Double' Peter
11/23/2007 1:33:45 PM
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?

Thanks
Peter.

Re: Marshal a 'Single' or 'Double' Peter
11/24/2007 4:40:30 PM
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.


[quoted text, click to view]

AddThis Social Bookmark Button