Re: C# prototype of C: void**
- From: frankh@xxxxxxxxxxxx
- Date: 4 Aug 2005 15:08:08 -0700
I declared the method as you suggested and tried to call it like this:
--
public unsafe void Capture (int width, int height, byte[] buffer)
{ fixed (byte* bp = &buffer[0])
errcode = imgSnap(sessionID, ref (IntPtr)bp);
}
--
but I got the error message "A ref or out argument must be an lvalue".
So how do I do the type casting? (I am rather new to c#).
As arrays are reference types, I guess that the "byte[] buffer"
declaration in the parameter list of Capture allows changing of the
array components, correct?
Nicholas Paldino [.NET/C# MVP] wrote:
> I would declare it like this:
>
> // The assmption is that it returns an integer.
> [DllImport("some.dll")]
> private static extern int imgSnap(int sid, ref IntPtr bufAddr);
>
> Pointers are typically passed around in IntPtr variables, and since you
> have a pointer to a pointer, you need to pass it by ref.
>
> However, accessing this in C# will require you to marshal the values in
> memory from unmanaged to managed code, which can get costly for 1/2 million
> bytes.
>
> You might want to consider using unsafe code here and casting the
> pointer to a byte array, and performing whatever operation you have to do in
> there.
>
> Hope this helps.
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
>
> <frankh@xxxxxxxxxxxx> wrote in message
> news:1123177279.875587.220400@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> > I want to access a function implemented in a C-dll with the following
> > prototype:
> > rval = imgSnap(SESSION_ID sid, void** bufAddr)
> > Now:
> > 1. How would the "void** bufAddr" part look like in the corresponding
> > C# prototype?
> > 2. How do I call this C# method (rval is uint, sid is int)?
> >
> > "bufAddr" exists allready, it must not be allocated, and I don't want
> > to copy data. imgSnap shall simply write its half a million bytes from
> > &&bufAddr on.
> >
> > Any idea?
> >
.
- Follow-Ups:
- Re: C# prototype of C: void**
- From: Willy Denoyette [MVP]
- Re: C# prototype of C: void**
- References:
- C# prototype of C: void**
- From: frankh
- Re: C# prototype of C: void**
- From: Nicholas Paldino [.NET/C# MVP]
- C# prototype of C: void**
- Prev by Date: Re: [Offtopic] What would you like to change in C# and .NET?
- Next by Date: Re: Automated unit tester
- Previous by thread: Re: C# prototype of C: void**
- Next by thread: Re: C# prototype of C: void**
- Index(es):
Relevant Pages
|