PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Compact Framework address of byte array

Reply

address of byte array

 
Thread Tools Rate Thread
Old 16-03-2004, 05:55 AM   #1
A. User
Guest
 
Posts: n/a
Default address of byte array


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.



  Reply With Quote
Old 16-03-2004, 05:49 PM   #2
Alex Feinman [MVP]
Guest
 
Posts: n/a
Default Re: address of byte array

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.

"Daniel Petersson" <dape@isbit.com> wrote in message
news:268FBA49-44CE-4A19-B030-9E14E29E3F00@microsoft.com...
> To get the adress of an object for interop you use the IntPtr type

together
> with GCHandle.
>
> // an array of bytes
> byte[] bytes = new byte[]{ 1,2,3,4,5 };
>
> // pin array during interop operation and to get the address
> GCHandle pinnedArray = GCHandle.Alloc( bytes, GCHandleType.Pinned );
>
> // do funky interop call
> Win32.NativeCallThatNeedAdressOfArray( pinnedArray.AddrOfPinnedObject );
>
> // free the handle
> pinnedArray.Free();



  Reply With Quote
Old 16-03-2004, 05:52 PM   #3
Alex Feinman [MVP]
Guest
 
Posts: n/a
Default Re: address of byte array

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

"A. User" <dve@byby.com> wrote in message
news:OaP5OtxCEHA.3804@TK2MSFTNGP09.phx.gbl...
> 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.
>
>
>



  Reply With Quote
Old 17-03-2004, 05:07 AM   #4
Lloyd Dupont
Guest
 
Posts: n/a
Default Re: address of byte array

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);


"Daniel Petersson" <dape@isbit.com> wrote in message
news:268FBA49-44CE-4A19-B030-9E14E29E3F00@microsoft.com...
> To get the adress of an object for interop you use the IntPtr type

together
> with GCHandle.
>
> // an array of bytes
> byte[] bytes = new byte[]{ 1,2,3,4,5 };
>
> // pin array during interop operation and to get the address
> GCHandle pinnedArray = GCHandle.Alloc( bytes, GCHandleType.Pinned );
>
> // do funky interop call
> Win32.NativeCallThatNeedAdressOfArray( pinnedArray.AddrOfPinnedObject );
>
> // free the handle
> pinnedArray.Free();



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off