all groups > dotnet interop > november 2006 >
You're in the

dotnet interop

group:

dll access error c# pass array of null pointers to c function



dll access error c# pass array of null pointers to c function linknum23 NO[at]SPAM gmail.com
11/29/2006 8:44:31 AM
dotnet interop: Ive been trying to use the following function from the imaq.dll
library:
imgSequenceSetup(SESSION_ID sid, uInt32 numberOfBuffers, void*
bufferList[ ], uInt32 skipCount[ ], uInt32 startNow, uInt32 async);



Everything I've tried I've gotten the error "Attempted to read or write
protected memory. This is often an indication that other memory is
corrupt."

The problem seems to be that I do not pass the array of pointers
properly.
The documentation tells me that i can pass either an array of null
pointers to be allocated, of pass an array of pointers already
allocated.

Some different things ive tried:
1.
public void StartSequenceAquire(){
imgBufList = new IntPtr[10];

imgSequenceSetup(sessionId, 10, imgBufList, 0, 1, 1); << it fails
here
}


[DllImport("imaq.public extern static int imgSequenceSetup(int
SessionID, int NumberOfBuffers,
[In,Out,MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] int[]
BufferList, int SkipCount, int StartNow, int Async);

2.
public void StartSequenceAquire(){
int numImages = 10;
IntPtr imgBufPtr = Marshal.AllocHGlobal(40800*numImages);
IntPtr imgBufAddListPtr = Marshal.AllocHGlobal(40 * numImages);
for (int ii = 0; ii < numImages; ii++)
{
Marshal.WriteInt32(imgBufAddListPtr,ii*4,imgBufPtr.ToInt32() +
4080 * ii);
}
imgSequenceSetup(sessionId, 10, imgBufAddListPtr, 0, 1, 1); <<- it
fails here, the method and its documentation are shown below
}

[DllImport("imaq.dll")]
public extern static int imgSequenceSetup(int SessionID, int
NumberOfBuffers, IntPtr BufferList, int SkipCount, int StartNow, int
Async);

3. Ive also tried several variations of the previous methods using ref
and repacing int with IntPtr and vice versa

documentation for the method is as follows:
pay special attention to the bufferlist parameter

/// <summary>
/// Prepares a session for acquiring a sequence into the buffer list.
/// </summary>
/// <param name="sid">valid SESSION_ID</param>
/// <param name="NumberOfBuffers">number of buffers in the buffer
list.</param>
/// <param name="bufferList">array of buffer pointers. For each element
in the buffer list that is initialized to NULL, bufferList[ ]
allocates a buffer and returns this buffer address in the array
element. This function acquires into the buffer for each element that
is not NULL.
/// </param>
/// <param name="SkipCount">array containing the number of images to
skip before each acquisition.
/// Note: skipCount is not supported for line scan acquisitions.
/// Refer to imgSessionLineTrigSource2 for information about
triggering line scan skip triggers.
/// Note: skipCount is not supported on the NI 1427, NI 1429, or NI
1430.
/// </param>
/// <param name="StartNow">non-zero value specifies that the continuous
acquisition should start immediately.
/// If the value is zero, you must manually start the acquisition with
imgSessionStartAcquisition.
/// </param>
/// <param name="Async">if async is zero and startNow is non-zero, this
function does not return until the acquisition completes.
/// Otherwise, the function returns immediately.
/// </param>
/// <returns>This function returns 0 on success. On failure, this
function returns an error code.
/// For information about the error code, call imgShowError.
/// </returns>

Anyone have any ideas? I dont have much experience with interop
Re: dll access error c# pass array of null pointers to c function Michael C
11/30/2006 12:00:00 AM
[quoted text, click to view]

Try it with a single intptr not in an array and see if that works. If it
does then it's not marshalling the array correctly. Try some of the
MarshalAs options. You might need to specify the [Out] attribute.
[quoted text, click to view]

Re: dll access error c# pass array of null pointers to c function linknum23 NO[at]SPAM gmail.com
11/30/2006 7:34:42 AM
Yeah, I tried using just an intptr same error. Ive also tried many
cambinations of [In,Out] and ref and even out.
Re: dll access error c# pass array of null pointers to c function mehr13 NO[at]SPAM hotmail.com
11/30/2006 9:26:48 AM
bufferList is an array of pointers, I suggest passing it as such.
IntPtr[] bufferList = new Intptr[10];

[DllImport("imaq.dll")]
public extern static int imgSequenceSetup(int SessionID, int
NumberOfBuffers, [In, MarshalAs(UnmanagedType.LPArray)] IntPtr[]
BufferList, int[] SkipCount, int StartNow, int
Async);

MH


[quoted text, click to view]
Re: dll access error c# pass array of null pointers to c function linknum23 NO[at]SPAM gmail.com
11/30/2006 11:19:19 AM
Yeah ive tried using an array of pointers too. NOTHING WORKS!
Re: dll access error c# pass array of null pointers to c function Michael C
12/1/2006 12:00:00 AM
[quoted text, click to view]

You will need to use ref or out, you need to pass a pointer to a pointer.
Did you try it like below? If this doesn't work with numberOfBuffers = 1
then something else is most likely wrong. You were presuming passing the
array was the problem but it could be many other things. The error does say
that it usually indicates that memory is already corrupt, perhaps a previous
call is incorrect.

imgSequenceSetup(SESSION_ID sid, uInt32 numberOfBuffers, [In, Out] ref
IntPtr bufferList, [In, Out] ref uInt32 skipCount, uInt32 startNow, uInt32
async);
[quoted text, click to view]

AddThis Social Bookmark Button