[RESOLVED] About memory-files: file pointers and byte-array pointers-VBForums
Results 1 to 10 of 10

Thread: [RESOLVED] About memory-files: file pointers and byte-array pointers

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2012
    Posts
    814

    Resolved [RESOLVED] About memory-files: file pointers and byte-array pointers

    In development, I often need to manipulate memory-files rather than physical-files. For example, I use mmioOpen API to open a wav file, and then use mmioRead API to read the data in this file, the code is as follows:

    Code:
    Private Declare Function mmioOpen Lib "winmm.dll" Alias "mmioOpenA" (ByVal szFileName As String, lpmmioinfo As mmioinfo, ByVal dwOpenFlags As Long) As Long
    Private Declare Function mmioRead Lib "winmm.dll" (ByVal hmmio As Long, ByVal pch As Long, ByVal cch As Long) As Long
    
        '--- Open the wave file
        hmmio = mmioOpen(wavFile, mmioinf, MMIO_READ)
        ...
    
        '--- Read the wave file	
        ret = mmioRead(hmmio, waveHdr.lpData, bufferSize)
        ...
    I want to store the wav files in the database, and load them as a byte-array into memory( as a memory-file). Now how to use the mmioRead function to access this memory-file instead of a physical-file? That is, let mmioRead API directly access the byte-array(memory data), not the physical-file. Or, how to convert file handle into memory pointer (virutal file-pointer)?

  2. #2
    Fanatic Member
    Join Date
    Feb 2015
    Posts
    960

    Re: About memory-files: file pointers and byte-array pointers

    See description https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx:
    To open a memory file using an application-supplied buffer, set pchBuffer to point to the memory buffer, fccIOProc to FOURCC_MEM, cchBuffer to the size of the buffer, and adwInfo to the incremental expansion size of the buffer. The expansion size in adwInfo should be nonzero only if pchBuffer is a pointer obtained by calling the GlobalAlloc and GlobalLock functions; in this case, the GlobalReAlloc function will be called to expand the buffer. In other words, if pchBuffer points to a local or global array or a block of memory in the local heap, adwInfo must be zero. Specify the MMIO_CREATE flag for the dwOpenFlags parameter to initially set the end of the file to be the beginning of the buffer. Otherwise, the entire block of memory is considered readable.
    Code:
        tIOInfo.fccIOProc = FOURCC_MEM
        tIOInfo.pchBuffer = VarPtr(bBuffer(0))
        tIOInfo.cchBuffer = UBound(bBuffer) + 1
    
        hFile = mmioOpen(vbNullString, tIOInfo, MMIO_READ)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2012
    Posts
    814

    Re: About memory-files: file pointers and byte-array pointers

    Quote Originally Posted by The trick View Post
    See description https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx:

    Code:
        tIOInfo.fccIOProc = FOURCC_MEM
        tIOInfo.pchBuffer = VarPtr(bBuffer(0))
        tIOInfo.cchBuffer = UBound(bBuffer) + 1
    
        hFile = mmioOpen(vbNullString, tIOInfo, MMIO_READ)
    Great, it's exactly what I want. Thank you so much, The trick.

    But I don't know what the value of FOURCC_MEM is? I've been searching the internet for a long time and I haven't found the value of this constant. I see it in mmsystem.h as defined:

    #define FOURCC_MEM mmioFOURCC('M', 'E', 'M', ' ')

    Do I need to define:

    FOURCC_MEM = mmioStringToFOURCC ("MEM", 0)
    or
    FOURCC_MEM = mmioStringToFOURCC ("mem", 0)

  4. #4

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2012
    Posts
    814

    Re: About memory-files: file pointers and byte-array pointers

    Thank you very much, The trick.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2012
    Posts
    814

    Re: About memory-files: file pointers and byte-array pointers

    Quote Originally Posted by The trick View Post
    See description https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx:

    Code:
        tIOInfo.fccIOProc = FOURCC_MEM
        tIOInfo.pchBuffer = VarPtr(bBuffer(0))
        tIOInfo.cchBuffer = UBound(bBuffer) + 1
    
        hFile = mmioOpen(vbNullString, tIOInfo, MMIO_READ)
    Hi The trick, I modified the code as you said, but failed to read the data.

    Code:
    ' // Read a wave file and check possibility of conversion to output format
    ' // Dreammanor modified to test memory file
    Public Function ReadWaveFile_B(FileNameOrByteArray) As Boolean
        Dim hIn     As Long
        Dim inf     As MMCKINFO
        Dim sInf    As MMCKINFO
        Dim tIOInfo As mmioinfo
        Dim inpFmt  As WAVEFORMATEX
        Dim ret     As Long
        Dim dat() As Byte
        
        ' // Read file
        If VarType(FileNameOrByteArray) = vbString Then
            hIn = mmioOpen(FileNameOrByteArray, ByVal 0, MMIO_READ)
        Else
            dat = FileNameOrByteArray
                
            tIOInfo.fccIOProc = FOURCC_MEM           '--- mmioStringToFOURCC("MEM", 0)
            tIOInfo.pchBuffer = VarPtr(dat(0))
            tIOInfo.cchBuffer = UBound(dat) + 1
                
            hIn = mmioOpen(vbNullString, tIOInfo, MMIO_READ)
        End If
        
        If (hIn = 0) Then
            MsgBox "Error opening file"
            Exit Function
        End If
    
    ...
    ...
    Attached Files Attached Files

  7. #7
    Fanatic Member
    Join Date
    Feb 2015
    Posts
    960

    Re: About memory-files: file pointers and byte-array pointers

    Hi The trick, I modified the code as you said, but failed to read the data.
    You have an error:
    Code:
    Private Type mmioinfo
     dwFlags As Long
     fccIOProc As Long
     pIOProc As Long
     wErrorRet As Long
     htask As Long
     cchBuffer As Long
     pchBuffer As Long
     pchNext As Long
     pchEndRead As Long
     pchEndWrite As Long
     lBufOffset As Long
     lDiskOffset As Long
     adwInfo(4) As Long
     dwReserved1 As Long
     dwReserved2 As Long
     hmmio As Long
    End Type

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2012
    Posts
    814

    Re: About memory-files: file pointers and byte-array pointers

    Oh, sorry, I used a very old type definition. Now mmioRead can successfully read the memory file. Thank you so much, The trick.

  9. #9
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    604

    Re: About memory-files: file pointers and byte-array pointers

    Quote Originally Posted by dreammanor View Post
    But I don't know what the value of FOURCC_MEM is? I've been searching the internet for a long time and I haven't found the value of this constant. I see it in mmsystem.h as defined:

    #define FOURCC_MEM mmioFOURCC('M', 'E', 'M', ' ')

    Do I need to define:

    FOURCC_MEM = mmioStringToFOURCC ("MEM", 0)
    or
    FOURCC_MEM = mmioStringToFOURCC ("mem", 0)
    No, you need to convert FOURCC to Long. Note that sFOURCC is 4 characters long, "MEM ".
    Code:
    Debug.Print "&H" & Hex(FourCCToLong("MEM "))
    Code:
    Function FourCCToLong(ByVal sFOURCC As String) As Long 
    Dim Bytes() As Byte 
    If (Len(sFOURCC) = 4) Then 
        Bytes() = StrConv(sFOURCC, vbFromUnicode) 
        FourCCToLong = ((Bytes(3) And &H7F) * &H1000000&) Or (&H10000& * Bytes(2)) Or (&H100& * Bytes(1)) Or Bytes(0) 
        If (Bytes(3) And &H80) Then FourCCToLong = FourCCToLong Or &H80000000 
    End If 
    End Function
    Conversion must be done to all defined four character constants like:
    Code:
    #define FOURCC_RIFF     mmioFOURCC('R', 'I', 'F', 'F')
    #define FOURCC_LIST     mmioFOURCC('L', 'I', 'S', 'T')
    Last edited by Tech99; Jan 10th, 2018 at 03:58 PM.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2012
    Posts
    814

    Re: [RESOLVED] About memory-files: file pointers and byte-array pointers

    Hi Tech99, thanks for your reply. The FourCCToLong you wrote is similar to mmioStringToFOURCC ("MEM", 0), and both returned values equal to the value &H204D454D given by The trick.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width