PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Compact Framework
address of byte array
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Compact Framework
address of byte array
![]() |
address of byte array |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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. |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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(); |
|
|
|
#3 |
|
Guest
Posts: n/a
|
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. > > > |
|
|
|
#4 |
|
Guest
Posts: n/a
|
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(); |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

