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
  1. Home
  2. General Programming
  3. C#
  4. accessing unmanaged memory [modified]

accessing unmanaged memory [modified]

Scheduled Pinned Locked Moved C#
helpcsharpperformancequestion
1 Posts 1 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.
  • A Offline
    A Offline
    a mulay
    wrote on last edited by
    #1

    Hi, I am trying to access unmanaged memory which allocated from a dll function. The function signature that is exported is as follows extern "C" __declspec( dllexport ) BOOL ResizeImage(LPVOID pbySrcBytes, LONG lnSrcSize, LPVOID pbyDestBytes, LPLONG lnTgtSize ); Through the parameter "pbySrcBytes" I pass memory to the unmanaged code using marshalling. In return the unmanaged function returns memory through "pbyDestBytes" on unmanaged heap which is having dynamic size which I get through "lnTgtImgSize". I am not able to copy the memory returned. I am getting an error as "Access Violation" & "the memory is corrupted". I have imported the function as follows. [DllImport("ImgProcessor.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)] public static extern Int32 ResizeImage([Out] byte[] pbySrcImgBytes, Int32 lnSrcImgSize, IntPtr ppbyDestImgBytes, [Out] IntPtr lnTgtImgSize ); I have done the C# part as follows:- byte []byInputArray = File.ReadAllBytes(txtInput.Text); // txtInput.Text contains the file name to read Int32 nTgtImgSize = new Int32(); IntPtr ptrTargetImageBytes = Marshal.AllocHGlobal(4); IntPtr ptrTgtImgSize = Marshal.AllocHGlobal(4); IntPtr ptrSourceImageBytes = Marshal.AllocHGlobal(byInputArray.Length); Marshal.StructureToPtr(byInputArray[byInputArray.Length-1], ptrSourceImageBytes, false); ImageResizerClient.ResizeImage(byInputArray, (int)byInputArray.Length, 400, 300, ptrTargetImageBytes, ptrTgtImgSize); nTgtImgSize = Marshal.ReadInt32(ptrTgtImgSize); byte []byOutputArray = new byte[nTgtImgSize]; Marshal.Copy(ptrTargetImageBytes, byOutputArray, 0, nTgtImgSize); The Marshal.Copy method call is throwing the error. Can anyone help me understand why this is happening? Thank You, Amit. -- modified at 23:59 Monday 11th September, 2006

    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