read binary file to buffer using globalalloc/copymemory

Posted on 2005-05-10
Last Modified: 2008-02-01


Here is part of my code:

Open file For Binary Access Write As fileTemp
Put fileTemp, , m_version
m_FileNumber = fileTemp

If m_FileNumber <> 0 Then
        memory_handle = GlobalAlloc(GMEM_MOVEABLE, LOF(m_FileNumber))

        If memory_handle Then

            memory_pointer = GlobalLock(memory_handle)

            CopyMemory ByVal memory_pointer, m_FileNumber, _

            GlobalUnlock memory_handle

        End If

    End If


Is it the good way to do it?

And how can i watch the value that memory_pointer is pointing to?
Question by:TungVan
    LVL 38

    Accepted Solution

    Assuming you want an array of bytes:

    Dim hFile as integer
    dim b() as Byte
    hFile = FreeFile
    Open file For Binary As hFile
    redim b(lof(hfile)-1)
    Get #hFile,, b
    Close #hFile

    If you don't want an array of bytes, then what is it you are trying to accomplish?
    LVL 27

    Assisted Solution

    The result of
    CopyMemory ByVal memory_pointer, m_FileNumber, LOF(m_FileNumber)
    is copying 4 bytes (if m_FileNumber is Long or 2 bytes if m_FileNumber is Integer) of probably useful data (value of m_FileNumber) plus
    LOF(m_FileNumber) - 4 bytes of garbage

    Author Comment


    I try to serialize/deserialize instance of object class to the memory in vb6...

    One of its use is for clipboard:

    I want to serialize the object to the memory, and pass the memory pointer to the clipboard for copying

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    779 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

    15 Experts available now in Live!

    Get 1:1 Help Now