I have the following snippet of code which which makes a call into a
dll to generate an array of bytes. Its arguments are a IntPtr to the
bitmap, a number, and a reference to the array that it will output.
-- Start DLL code snippet
[DllImport(@"Lib\compare.dll", EntryPoint="GetHash")]
private static extern unsafe Int16 GetHash(IntPtr Bitmap1, Int16
FilterNum, void* ImageHash);
/// <summary>
/// Creates an hash using the pointer to the image bitmap
/// </summary>
/// <param name="Bitmap1">is a pointer to bitmap in
memory.</param>
/// <param name="FilterNum">variable is used for internal
algorithm fine tuning</param>
/// <param name="ImageHash">is a pointer to 768-bytes array in
memory. You have to allocate
/// memory before the variable use. And do not forget to free
memory
/// after you are done.</param>
/// <returns></returns>
public unsafe static void GetHash(IntPtr BitMapPtr, int
FilterNum, ref Byte[] ImageHash) {
// Fix the ImageHashes byte array so it can't be moved
fixed(byte* p = ImageHash) {
byte** pp = null;
try {
// Global alloc a byte* sized chunk of unmanaged
memory and initialize with our byte *
pp = (byte**)Marshal.AllocHGlobal(sizeof(byte*));
*pp = p;
// Call Russian function: pp is now a pointer to a
pointer to the byte array.
string s = ImageCompare.GetHash(BitMapPtr, (Byte)
FilterNum, pp).ToString();
} finally {
// Free global memory
if(pp != null)
Marshal.FreeHGlobal((IntPtr)pp);
}
}
}
-- End DLL code snippet
Here is how I am calling it
-- Start calling Code
Bitmap img1 = new Bitmap("c:\\Junk\\Compare 6\\TestImage.jpg");
byte[] hash = new byte[768];
Rectangle rec = new Rectangle (0, 0,img1.Width, img1.Height);
System.Drawing.Imaging.BitmapData bd = img1.LockBits (rec,
System.Drawing.Imaging.ImageLockMode.ReadWrite, img1.PixelFormat);
Compare.GetHash ( bd.Scan0 , 1, ref hash );
-- End calling code
I am getting
An unhandled exception of type
'System.Runtime.InteropServices.SEHException'
on
Compare.Compare.GetHash ( bd.Scan0 , 1, ref hash );
Can anybody tell me if I am calling this correctly. This code was
handed to me and I really don't know the interal workings of the DLL.
Thanks is advance