On 2003-10-05, Shayne H <> wrote:
> I cannot get the following function to work. A System.NullReferenceError
> exception occurs when CopyMemory is executed. However, during debug I found
> that none of the parameters passed are Null.
> Is there a .NET way of doing this without using the CopyMemory API function?
>
> Function ByteArrayToString(ByVal byteArray() As Byte, ByVal start As
> Integer, ByVal length As Integer)
> Dim temp As New String(Chr(0), length+1)
> CopyMemory(temp, byteArray(start), length)
> Return temp.ToString()
> End Function
>
> --
> Thanks for any help,
> Shayne H
>
>
>
>
As was pointed out - don't use copymem for this - use the appropriate
System.Text.Encoding class for the job. In fact, using CopyMemory in
..NET is a little dangerous since objects can be relocated on the heap.
If you have to use it, you will want to pin the objects before the call
- basically you tell the runtime not to relocate them until I say it
is ok

(look at the System.Runtime.InteropServices.GCHandle
structure, for more information)
But, to answer your question about the nullreference exception - well
with out seeing your declaration of CopyMemory - I would bet that it is
wrong. If you could post it, it would be easier to tell
Tom Shelton