Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
linknum23@gmail.com - 29 Nov 2006 16:44 GMT
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
Michael C - 30 Nov 2006 03:01 GMT
> Ive been trying to use the following function from the imaq.dll
> library:
[quoted text clipped - 19 lines]
> here
> }

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.

> [DllImport("imaq.public extern static int imgSequenceSetup(int
> SessionID, int NumberOfBuffers,
[quoted text clipped - 61 lines]
>
> Anyone have any ideas? I dont have much experience with interop
linknum23@gmail.com - 30 Nov 2006 15:34 GMT
Yeah, I tried using just an intptr same error. Ive also tried many
cambinations of [In,Out] and ref and even out.
Michael C - 30 Nov 2006 23:28 GMT
> Yeah, I tried using just an intptr same error. Ive also tried many
> cambinations of [In,Out] and ref and even out.

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);
mehr13@hotmail.com - 30 Nov 2006 17:26 GMT
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

> Ive been trying to use the following function from the imaq.dll
> library:
[quoted text clipped - 85 lines]
>
> Anyone have any ideas? I dont have much experience with interop
linknum23@gmail.com - 30 Nov 2006 19:19 GMT
Yeah ive tried using an array of pointers too. NOTHING WORKS!

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.