Re: C# prototype of C: void**

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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?
> >

.



Relevant Pages

  • Re: const qualifier and VC6.0
    ... > emphasis is on types, while in C the emphasis is on expressions. ... When declaring pointer and reference variables, ... int &p; ... For example, the following declaration ...
    (comp.lang.cpp)
  • Re: how to call a variable without reference in c
    ... fuction with out sending as a argument, pointer and can't declare as ... int a=10; ... Not in his declaration it didn't. ...
    (comp.lang.c)
  • Re: EXCEPTION : Importing DLL from eVC with double type parameter
    ... __declspecint P1(const TCHAR* pzPos, double* pdLat, double* ... extern static int P1(string pzPos, ref double pdLat, ref double pdLon) ... If setting string as REF so that declaration looks like this:extern static ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: would C be easier to read if...
    ... That indicates that it's a pointer to a function. ... int *a a is pointer to int ... In my case a type declaration that reads linearly from left to right would ... Let's say we have a regular int identifier named "foo". ...
    (comp.lang.c)
  • Re: how to call a variable without reference in c
    ... fuction with out sending as a argument, pointer and can't declare as ... main returns an int. ... The 'a' in fun() has no declaration and your compiler ought to have told you so. ...
    (comp.lang.c)