Re: transform byte[] in IntPtr or int

From: Brian Smith [MSFT] (briansm_at_nospam_microsoft.com)
Date: 08/10/04


Date: Tue, 10 Aug 2004 22:41:21 GMT

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]" <feedback@nospam-inthehand.com>
>References: <OtafKHyeEHA.3612@TK2MSFTNGP12.phx.gbl>
>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" <elmar.jacobs@target-instruments.com> wrote in message
>news:OtafKHyeEHA.3612@TK2MSFTNGP12.phx.gbl...
>> 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.



Relevant Pages

  • PROBLEM: 2.6.0-test10 BUG/panic in mpage_end_io_read
    ... backup folders from half of a failed raid 1 array. ... but the third one causes a BUG or panic every time. ... hdds that are going bad spew out a lot ... 8800-88ff: libata ...
    (Linux-Kernel)
  • Re: undefined vs. undefined (was: new Array() vs [])
    ... After thinking it over again tonight, I see no bug in Mozilla. ... Quote 1 ... Elided array elements are not defined. ... var p = br + br; ...
    (comp.lang.javascript)
  • Re: c / c++ : is it end of era ?
    ... You have to scan the whole string first ... has taken the trouble to learn the language. ... This is not a bug, ... array, however, is converted into a pointer to the array's first element ...
    (comp.lang.c)
  • Re: undefined vs. undefined (was: new Array() vs [])
    ... So where everyone else sees a bug, including the people who wrote the ... Elided array elements are not defined. ... So the applicable productions would be the production: ... Evaluate Elision; if not present, ...
    (comp.lang.javascript)