locked
Read dynamic data from unmanaged memory RRS feed

  • Question

  • I have images comming back from an unmanaged DLL. The size of the image is dynamic = it can change from a call to the other

    For this I have a structure reprenting the image + its size that looks like :

    [StructLayout(LayoutKind.Sequential)]
    private unsafe struct StructImage
    {
        public uint imageDate;
        public uint imageSize;
        public byte* imageData;
    }

    I get from the application a pointer to my structure. I read the info like this

    IntPtr imagePtr = ...
    StructImage imageInfo = (StructImage)Marshal.PtrToStructure(imagePtr, typeof(StructImage));
    

    Now I would get the size of the image and I would like to read the data like this

    byte[] buffer = new byte[imageInfo.imageSize];
    IntPtr srcPtr = new IntPtr(imageInfo.imageData);
    Marshal.Copy(srcPtr, buffer, 0, buffer.Length);
    

    At this point I get an "AccessViolationException"

    I understand that the Marshal.PtrToStructure declares that I will be reading unmanaged memory according to the size of my structure = when it comes to the imageData field only 4 bytes are declared "readable" for the byte*

    I red hundreds of forums/blogs talking about marshalling between managed and unmanaged memory but I still cannot figure out how I can extend the "reserved" unmanaged memory to the length of my data

    Can someone help me out ther ?

    Thanks a lot

    Christophe

    Tuesday, September 30, 2014 1:20 PM

Answers

  • Hi Christophe,

    As far as I know, if you want to use Marshal.PtrToStructure to marshal unmanaged memory block to managed object, the unmanage memory block and the managed object must be type-compatible. For example, if you use Marshal.AllocHGlobal method to allocate unmanaged memory for the managed structure or class with StructLayout attribute, then you'll successfully use Marshal.PtrToStructure to convert the unmanaged memory back to the structure. Check the sample code in this MSDN document:

    Marshal.PtrToStructure Method (IntPtr, Type)

    But in your case, seems that you don't know the exact structure of the imange in the unmanaged dll, so I don't think you can convert the unmanaged image memory to your own custom structure.

    If the images are icons, you can P/Invoke the ExtractIconEx function:

    [DllImport("Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        private static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons);

    Also check this blog for how to extract images in an unmanaged dll from managed code:

    How to load unmanaged (native) resources from managed C#


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Caillen Tuesday, October 7, 2014 6:47 AM
    Wednesday, October 1, 2014 6:38 AM