Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2007

Tip: Looking for answers? Try searching our database.

Marshal a 'Single' or 'Double'

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter - 23 Nov 2007 00:33 GMT
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.
Mattias Sjögren - 23 Nov 2007 06:17 GMT
>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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.