473,548 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ 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="Get Hash")]
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(s izeof(byte*));
*pp = p;
// Call Russian function: pp is now a pointer to a
pointer to the byte array.
string s = ImageCompare.Ge tHash(BitMapPtr , (Byte)
FilterNum, pp).ToString();
} finally {
// Free global memory
if(pp != null)
Marshal.FreeHGl obal((IntPtr)pp );
}
}
}

-- End DLL code snippet

Here is how I am calling it

-- Start calling Code

Bitmap img1 = new Bitmap("c:\\Jun k\\Compare 6\\TestImage.jp g");
byte[] hash = new byte[768];
Rectangle rec = new Rectangle (0, 0,img1.Width, img1.Height);
System.Drawing. Imaging.BitmapD ata bd = img1.LockBits (rec,
System.Drawing. Imaging.ImageLo ckMode.ReadWrit e, img1.PixelForma t);

Compare.GetHash ( bd.Scan0 , 1, ref hash );

-- End calling code

I am getting
An unhandled exception of type
'System.Runtime .InteropService s.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 1734

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

Similar topics

2
1902
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? Is 'unsafe' code the same as unmanaged code ?
0
1121
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 ***pOutputData); I can call this function from a console application however when I attempt to use the same code from within a web service I get the following...
0
994
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 ***pOutputData); I can call this function from a console application however when I attempt to use the same code from within a web service I get the following...
7
3588
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 the bmp1 object? (the pixel location based on the X & Y values and it's color based on the red,blue and green values the I already have)
3
3518
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 already have? BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int...
0
2296
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 ***pOutputData); I can call this function from a console application however when I attempt to use the same code from within a web service I get the following...
3
2163
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! This nothrow method requires more work than the exception method, since the value returned has to be checked after each and every memory...
0
1246
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. The trouble is that that each of this pieces of code are bound by a different unsafe section, therefore I'm getting an error that the second unsafe...
12
7175
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? class Trial { public: Trial() {
0
7438
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7803
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6036
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3495
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.