[Mono-list] P/Invoke question

James Fitzsimons james.fitzsimons@gmail.com
Wed, 16 Mar 2005 23:29:57 +0000

Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

Hi all,

I have been doing a bunch of stuff with mashalling and P/Invoke to
create c# wrappers for the opencv C library and have had good success so
far, until now.

I am trying to call the following C function:

int cvFindChessBoardCornerGuesses( const CvArr* image, CvArr* thresh,
                                   CvMemStorage* storage,=20
				   CvSize board_size,
                                   CvPoint2D32f* corners,=20
				   int* corner_count=3DNULL );

Where the last two parameters are out values and the storage parameter
is allowed to be NULL.

I have the following definitions in my C# app:

		private unsafe static extern int cvFindChessBoardCornerGuesses(
System.IntPtr img, System.IntPtr thresh, 			System.IntPtr storage,
CvSize etalonSize,
                        [Out] CvPoint2D32f* corners,=20
			[In, Out] int* cornerCount );

public unsafe void FindChessBoardCornerGuesses(CvImage image, CvImage
			thresh, CvSize etalonSize,
			out float[] corners, ref int cornerCount)
	System.IntPtr crns =3D new System.IntPtr();
	System.IntPtr crnCnt =3D new System.IntPtr();
	CvPoint2D32f* crs =3D null;
	cvFindChessBoardCornerGuesses( image.ImageHandle, thresh.ImageHandle,
System.IntPtr.Zero, etalonSize, crs, &cornerCount);
	corners =3D new float[cornerCount * 2];
	Marshal.Copy(crns, corners, 0, cornerCount * 2);

When I run however I get a null reference exception:
james@athos src $ MONO_LOG_LEVEL=3D"debug" MONO_LOG_MASK=3D"dll" mono
Mono-INFO: DllImport attempting to load: 'cv'.
Mono-INFO: DllImport loading location: 'libcv.so'.
Mono-INFO: Probing 'cvFindChessBoardCornerGuessesA'.
Mono-INFO: Probing 'cvFindChessBoardCornerGuesses'.
Mono-INFO: Found as 'cvFindChessBoardCornerGuesses'.

Unhandled Exception: System.NullReferenceException: Object reference not
set to an instance of an object
in (unmanaged) 0x2a981fb3e0
in <0x0000f> (wrapper managed-to-native)
in <0x0008b> Slam.CvCamera:FindChessBoardCornerGuesses
in <0x001af> Slam.MainClass:Main (string[])

Can anyone offer any advice. Am I doing the right thing with my wrapping

Thanks for any assistance you can provide!
It's 5.50 a.m.... Do you know where your stack pointer is ?

Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part

Version: GnuPG v1.2.6 (GNU/Linux)