473,480 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

unsafe c# call with bitmap pointer throwing exception

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

Aug 10 '06 #1
0 1726

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1893
by: Elidel | last post by:
Is the use of a COM object in a c# program considered 'unsafe' code ? Does the method that calls the COM object need the 'unsafe' keyword ? Is code that calls a COM object considered unmanaged?...
0
1116
by: Robert Rae | last post by:
I'm calling the C++ function below from C#: GENCLIENT_API int CleanseEx(char ncSystem, const char *strServer, const char **pNames, const char **pInputData, const char **pRules, char...
0
990
by: Robert Rae | last post by:
I'm calling the C++ function below from C#: GENCLIENT_API int CleanseEx(char ncSystem, const char *strServer, const char **pNames, const char **pInputData, const char **pRules, char...
7
3577
by: TomHL | last post by:
hello, - I have a Bitmap Object named: bmp1 - I already have the numerical values of:red,blue and green colors + I have the X and Y cordinates. How do I set the pixel value via safe code on...
3
3515
by: TomHL | last post by:
this code set the color that I want only on the first pixel. I have an X & Y values of the pixel that I want to change. How do I change the pointer's position according to my X & Y values that I...
0
2292
by: Robert Rae | last post by:
I'm calling the C++ function below from C#: GENCLIENT_API int CleanseEx(char ncSystem, const char *strServer, const char **pNames, const char **pInputData, const char **pRules, char...
3
2156
by: Michael | last post by:
Hi, Could you tell me how to under the following statements? Does it mean each and every memory allocation will check all the pointer no matter a null pointer or not? Right? Thanks in advance! ...
0
1235
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I have two piece of unsafe code. In the first one I'm getting the byte* pointer of a Bitmap data, and the second one in inside a loop that uses that byte* pointer to set the data in the Bitmap. ...
12
7168
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6760
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7022
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.