[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Marshal a 'Single' or 'Double'

Peter

11/23/2007 12:34:00 AM

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.


2 Answers

(Mattias Sjögren)

11/23/2007 6:17:00 AM

0


>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.n... | http://www.dotneti...
Please reply only to the newsgroup.

Peter

11/24/2007 3:41:00 AM

0

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.


"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:ub9GOiZLIHA.484@TK2MSFTNGP06.phx.gbl...
>
>>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
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.org
> http://www.msjogren.n... | http://www.dotneti...
> Please reply only to the newsgroup.