uint mimeType;
FindMimeFromData(0, null, data, 256, null, 0, out mimeType, 0);
var mimePointer = new IntPtr(mimeType);
This is surely wrong at 64 bits... A IntPtr
is 64 bits (it is a memory address)... How could a uint
(a 32 bits) contain it?
And if we take a look at the pinvoke site the signature should be:
[DllImport("urlmon.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = false)]
static extern int FindMimeFromData(IntPtr pBC,
[MarshalAs(UnmanagedType.LPWStr)] string pwzUrl,
[MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.I1, SizeParamIndex=3)]
byte[] pBuffer,
int cbSize,
[MarshalAs(UnmanagedType.LPWStr)] string pwzMimeProposed,
int dwMimeFlags,
out IntPtr ppwzMimeOut,
int dwReserved);
very important while the documentation of the method is very poor on msdn, calling FindMimeFromData
will cause a memory leak: you have to free the ppwzMimeOut
you receive... The problem is that it isn't clear how: here it is suggested to use CoTaskMemFree
, that is Marshal.FreeCoTaskMem
. I'll say that it is right, tested with:
byte[] bytes = File.ReadAllBytes("someimage.jpg");
while (true)
{
IntPtr ptr1;
int success1 = FindMimeFromData(IntPtr.Zero, null, bytes, bytes.Length, null, 0, out ptr1, 0);
Marshal.FreeCoTaskMem(ptr1);
}
If I remove the Marshal.FreeCoTaskMem
and take a look with the TaskManager, the memory used by the process will go up quite quickly... If I restore the Marshal.FreeCoTaskMem
, the memory will remain stable.