PC Review


Reply
Thread Tools Rate Thread

address of byte array

 
 
A. User
Guest
Posts: n/a
 
      16th Mar 2004
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
 
 
 
 
Alex Feinman [MVP]
Guest
Posts: n/a
 
      16th Mar 2004
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" <(E-Mail Removed)> wrote in message
news:268FBA49-44CE-4A19-B030-(E-Mail Removed)...
> 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
 
Alex Feinman [MVP]
Guest
Posts: n/a
 
      16th Mar 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
 
Lloyd Dupont
Guest
Posts: n/a
 
      17th Mar 2004
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" <(E-Mail Removed)> wrote in message
news:268FBA49-44CE-4A19-B030-(E-Mail Removed)...
> 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Value of type byte cannot be converted to 1-dimentional array of byte cmdolcet69 Microsoft VB .NET 3 25th Sep 2007 10:10 PM
How do I convert a ASCII Byte Array, to another Byte Array Russell Mangel Microsoft Dot NET Framework 2 2nd Feb 2005 06:01 PM
Re: Byte Array to Printable String to Byte Array Jon Skeet [C# MVP] Microsoft Dot NET 0 4th Aug 2004 01:53 PM
RE: address of byte array =?Utf-8?B?RGFuaWVsIFBldGVyc3Nvbg==?= Microsoft Dot NET Compact Framework 0 16th Mar 2004 08:41 AM
Convert native byte array (pointer) to managed byte[] Dave Microsoft Dot NET 1 13th Aug 2003 05:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:09 AM.