CopyMemory(buffer, newBuffer, nBytes)?

  • Thread starter Thread starter vb newbie
  • Start date Start date
V

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...
 
vb said:
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.
 
GhostInAK said:
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.
 
Back
Top