Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. read stream to unmanaged memory

read stream to unmanaged memory

Scheduled Pinned Locked Moved C#
performancequestioncareer
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    ZejulioZ
    wrote on last edited by
    #1

    Is there any ways to read directly and rapidly from a stream to an unmanaged buffer ( IntPtr or byte* ) without having to do a convertion from the byte[] to the IntPtr through Marshal.Copy method ? More clearly, I want to avoid this Marshal.Copy : byte[] b = new byte[1024]; int size = theStream.Read(b, 0, 1024); IntPtr bufferPtr = Marshal.AllocHGlobal(size); Marshal.Copy(b, 0, bufferPtr, size); Apparently there is a UnmanagedMemoryStream class coming along with winFx doing the job, but I'm looking for something usable with the framework 1.1 Thanks a lot, Julien.

    L 1 Reply Last reply
    0
    • Z ZejulioZ

      Is there any ways to read directly and rapidly from a stream to an unmanaged buffer ( IntPtr or byte* ) without having to do a convertion from the byte[] to the IntPtr through Marshal.Copy method ? More clearly, I want to avoid this Marshal.Copy : byte[] b = new byte[1024]; int size = theStream.Read(b, 0, 1024); IntPtr bufferPtr = Marshal.AllocHGlobal(size); Marshal.Copy(b, 0, bufferPtr, size); Apparently there is a UnmanagedMemoryStream class coming along with winFx doing the job, but I'm looking for something usable with the framework 1.1 Thanks a lot, Julien.

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Here is an example of how I load a memory font from resources: PrivateFontCollection col = new PrivateFontCollection(); Stream fontStream = GetType().Assembly.GetManifestResourceStream("somefont.ttf"); byte[] fontdata = new byte[fontStream.Length]; fontStream.Read(fontdata,0,(int)fontStream.Length); fontStream.Close(); GCHandle gc = GCHandle.Alloc(fontdata, GCHandleType.Pinned); col.AddMemoryFont(gc.AddrOfPinnedObject(),fontdata.Length); gc.Free(); xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

      Z 1 Reply Last reply
      0
      • L leppie

        Here is an example of how I load a memory font from resources: PrivateFontCollection col = new PrivateFontCollection(); Stream fontStream = GetType().Assembly.GetManifestResourceStream("somefont.ttf"); byte[] fontdata = new byte[fontStream.Length]; fontStream.Read(fontdata,0,(int)fontStream.Length); fontStream.Close(); GCHandle gc = GCHandle.Alloc(fontdata, GCHandleType.Pinned); col.AddMemoryFont(gc.AddrOfPinnedObject(),fontdata.Length); gc.Free(); xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

        Z Offline
        Z Offline
        ZejulioZ
        wrote on last edited by
        #3

        That's interesting. Do you know what is actually GCHandle.AddrOfPinnedObject doing behind ? Because I'm not sure C# arrays are contiguous like C/C++ arrays are. And I profit to ask you; have you compared both methods efficiency ( GCHandle / standard Marshal.Copy ) ? Thanks for the hint anyway :D Julien

        L 1 Reply Last reply
        0
        • Z ZejulioZ

          That's interesting. Do you know what is actually GCHandle.AddrOfPinnedObject doing behind ? Because I'm not sure C# arrays are contiguous like C/C++ arrays are. And I profit to ask you; have you compared both methods efficiency ( GCHandle / standard Marshal.Copy ) ? Thanks for the hint anyway :D Julien

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          ZejulioZ wrote: Because I'm not sure C# arrays are contiguous like C/C++ arrays are. If you pinned it, they are :) ZejulioZ wrote: And I profit to ask you; have you compared both methods efficiency ( GCHandle / standard Marshal.Copy ) ? Sorry cant help you there... xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups