Home All Groups Group Topic Archive Search About

CopyMemory(buffer, newBuffer, nBytes)?

Author
13 Oct 2006 5:49 PM
vb newbie
I need to copy the contents of one byte array to another one.  I was
hoping to do it quickly uisng the CopyMemory api call.  However, I'm
getting:

FatalExecutionEngineError was detected
Message: The runtime has encountered a fatal error. The address of the
error was at 0x7f47f8e4, on thread 0x12c4. The error code is
0xc0000005. This error may be a bug in the CLR or in the unsafe or
non-verifiable portions of user code. Common sources of this bug
include user marshaling errors for COM-interop or PInvoke, which may
corrupt the stack.

How do you set things up in order to do the copy?  I suspect it might
involve the 'managed' feature, but I'm not yet sure exactly how that
works...

Author
13 Oct 2006 5:54 PM
GhostInAK
Hello vb,

Check out the Array class.

-Boo

Show quoteHide quote
> I need to copy the contents of one byte array to another one.  I was
> hoping to do it quickly uisng the CopyMemory api call.  However, I'm
> getting:
>
> FatalExecutionEngineError was detected
> Message: The runtime has encountered a fatal error. The address of the
> error was at 0x7f47f8e4, on thread 0x12c4. The error code is
> 0xc0000005. This error may be a bug in the CLR or in the unsafe or
> non-verifiable portions of user code. Common sources of this bug
> include user marshaling errors for COM-interop or PInvoke, which may
> corrupt the stack.
> How do you set things up in order to do the copy?  I suspect it might
> involve the 'managed' feature, but I'm not yet sure exactly how that
> works...
>
Author
13 Oct 2006 11:53 PM
Herfried K. Wagner [MVP]
"GhostInAK" <p***@paco.net> schrieb:
> Check out the Array class.

Namely 'Array.Copy' and 'Array.CopyTo'.  Note that I'd recommend to use
'Buffer.BlockCopy' as pointed out by Tom for the types it supports.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
13 Oct 2006 7:48 PM
Tom Shelton
vb newbie wrote:
Show quoteHide quote
> I need to copy the contents of one byte array to another one.  I was
> hoping to do it quickly uisng the CopyMemory api call.  However, I'm
> getting:
>
> FatalExecutionEngineError was detected
> Message: The runtime has encountered a fatal error. The address of the
> error was at 0x7f47f8e4, on thread 0x12c4. The error code is
> 0xc0000005. This error may be a bug in the CLR or in the unsafe or
> non-verifiable portions of user code. Common sources of this bug
> include user marshaling errors for COM-interop or PInvoke, which may
> corrupt the stack.
>
> How do you set things up in order to do the copy?  I suspect it might
> involve the 'managed' feature, but I'm not yet sure exactly how that
> works...

Buffer.BlockCopy

Option Strict On
Option Explicit On

Imports System

Module Module1

    Sub Main()
        Dim array1() As Byte = {1, 2, 3, 4, 5}
        Dim array2(4) As Byte

        Buffer.BlockCopy(array1, 0, array2, 0, 5)
        For Each b As Byte In array2
            Console.WriteLine(b)
        Next
    End Sub

End Module

Calling RtlMoveMemory in VB.NET is something that should really be
avoided - as it is generally not safe.  Addresses can be relocated on
the heap unless you pin them, and that can cause performance/memory
issues if not managed carefully.

--
Tom Shelton