address of byte array

  • Thread starter Thread starter A. User
  • Start date Start date
A

A. User

I am a C# beginner so I don't know the C# syntax that good.
I have a CF app that manipulates a bunch of data in a byte array.
I need to pass the address of the start of the array as a DWORD
(C# uint) to my unmanaged code function with pinvoke. The unmanaged
code I know works and my app works with the data correctly.
I have made other pinvoke calls and I understand it to some extent.
I can't seem to get the syntax correct for the address of the byte array.
Is there a such thing as a "cast" like in C where I can coheres the start of
the array to a uint?
I tried all kind of permutation of "ref" "&" and get nothing but syntax
errors.
 
Unfortunately there is a bug in GCHandle implementation on CF. IF you pin an
array you will get a pointer to a control structure instead of array data.
In particular for simple 1-dim arrays it will be address of array[-1]
holding numelements. To get a pointer to a real address it will need to be
incremented by 4.
 
Arrays are referenced objects and as such are marshalled by pointer. So if
you have a function that takes e.g. LPBYTE, the corresponding C# declaration
would have byte[] and not ref byte[]. Note that since pointer types are
currently 4 byte, you can have
[DllImport]
extern static void MyFunc(byte[] parm);

calling into

void WINAPI MyFunc(DWORD parm)
{
LPBYTE arr = (LPBYTE)parm;
...
}

In this case the address of the first array element will be passed
downstream
 
to answer your question:

unsafe void call(byte[] values)
{
fixed(byte* pvalue = &values[0])
callNative((uint) pvalue);
}
or something like that ?

anyway I do prefer Alex Feyman's trick of a proper interop definition

[DllImport(mylib)]
static extern void myfunc(byte[] array);
 
Back
Top