Replacement of MoveMemory in VB.NET

Posted on 2006-04-03
Last Modified: 2008-02-01
Hello Folks,

I am converting VB.6 application to VB.NET 2005.During conversion I couldnt find the resplacement of API MoveMemory which is used in VB.6 application.

Is there anybody who has faced similar kind of problem or know how to solve it.

Thanking you
Question by:ChiragDanech
    LVL 48

    Expert Comment

    LVL 96

    Expert Comment

    by:Bob Learned
    You may not need MoveMemory.  What are you trying to accomplish in .NET 2005?   There might be an easier way without API calls.


    Author Comment

    Trying to copy Structure type to 2 dimensional array.

    MoveMemory(VarPtrArray(2 dimensional Array), VarPtr(structureType), 4)

    Its urgent . Can anybody reply me asap.
    LVL 96

    Expert Comment

    by:Bob Learned
    Ok, let's take a little wider view of your requirement.   What is the overall requirement?  How does this one little question fit into the grander scheme?  That is what I meant by it might be easier without API calls.  Maybe we could suggest a native .NET framework approach, if we knew what you were trying to convert.  It might be simpler than complex memory operations.

    You can use the Marshal class to copy memory, but with the garbage collector, it gets a little messy, since you have to start think about handles and pinning memory.


    Author Comment

    I appriciate  your comments on it. But Its difficult to explain the whole thing. I have already converted 75 % of the VB application into VB.NET. I just stuck here on MoveMemory API. And as i have aleready mentioned my cleare intension what I need to do.
    LVL 96

    Accepted Solution

    Here is an example of using the Marshal.Copy method from System.Runtime.InteropServices:

      Private Shared Function ByteArrayToInteger(ByVal byteArray As Byte()) As Integer

        Dim value As Integer

        ' Create GCHandle instance and pin variable required.
        Dim handle As GCHandle = GCHandle.Alloc(value, GCHandleType.Pinned)

        ' Get address of variable in pointer variable.
        Dim address As IntPtr = handle.AddrOfPinnedObject()

        ' Use copy method to copy array data to variable’s
        ' address with length specified(4)
        Marshal.Copy(byteArray, 0, address, 4)

        'First read value of variable from its address
        'in memory in order to use it
        value = Marshal.ReadInt32(address)

        ' Free the memory pointer.

        Return value

      End Function   'ByteArrayToInteger


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now