Tuesday, September 30, 2014

Read dynamic data from unmanaged memory

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.


No comments:

Post a Comment