Peter and Elmar,
I would not recommend the "add 4" workaround to the bug in
AddrOfPinnedObject() of arrays. The bug is fixed in v2 of .NETCF and may be
fixed in future service packs of v1. So any application that relies on the
result of AddrOfPinnedObject being off-by-4 will no longer work properly on
future fixed versions of the runtime.
Two potentially better ways to do the same thing that are not
version-dependent:
1) From c#, you can take the address of the array using a "fixed"
statement. For example:
fixed(void *ArrayPointer = MyManagedArray) {
use the pointer...
}
You may also need to pin the object using a GCHandle if you want the
address to remain valid outside the fixed statement.
2) Instead of using a managed array, allocate the memory using a native API
such as LocalAlloc. If the memory is to be pinned for a long time, this
would usually be preferable to using a GCHandle because pinned managed
objects make garbage collection less efficient.
If you must use AddrOfPinnedObject, try to apply the "+ 4" workaround only
conditionally. During your application's initialization, you could verify
that the bug exists on the current platform (perhaps by comparing the
results of AddrOfPinnedObject to a fixed statement of a test array) and
record whether the bug exists in a static field. It is also possible to use
Environment.Version to determine what version of .NETCF you are running on,
however since we do not yet know whether the bug will be fixed in v1 SP3, I
cannot say what version you should look for to know whether the bug is
fixed.
I'm sorry about the trouble this bug has caused but hopefully we can avoid
further trouble after the bug is fixed.
Brian
--------------------
From: "Peter Foot [MVP]" <
[email protected]>
References: <
[email protected]>
Subject: Re: transform byte[] in IntPtr or int
Date: Thu, 5 Aug 2004 22:55:25 +0100
One option is to use the GcHandle and pin your byte[] then using the
AddrOfPinnedObject() which returns an IntPtr. However in NETCF you need to
add 4 to this value to offset past the length of the buffer. You can then
write this intptr into another structure. Remember to free your GcHandle
once you have finished with it.
You can also do the same in unsafe code (C# only) to pin an object and get a
pointer to it. The GCHandle method works in VB also.
Finally the other method is to allocate a block of native memory using
LocalAlloc (or the ready made MarshalEx class in the SDF
www.opennetcf.org/sdf/) and copy your data to this and write this IntPtr
into your structure.
Peter
--
Peter Foot
Windows Embedded MVP
www.inthehand.com
Do have an opinion on the effectiveness of Microsoft Windows Mobile and
Embedded newsgroups? Let us know!
https://www.windowsembeddedeval.com/community/newsgroups
Elmar Jacobs said:
Hi folk,
i must fill an byte array with addresses from other byte arrays. Because
than i can search over an Bleutooth dll after devices and services. The
dll
is of cause unmanaged.
My favorite system is
BitConverter.GetBytes(NumnerOfProtocolls).CopyTo(dataStream, 20);
Therefore my question is how can i transfer the address of an byte[]
variable to int value?
So i can fill it in an array.
I hope someone can help me,
With best regards,
elmar
This posting is provided "AS IS" with no warranties, and confers no rights.