• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 898
  • Last Modified:

read binary file to buffer using globalalloc/copymemory


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?
2 Solutions
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?
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
TungVanAuthor Commented:

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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now