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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.