0

I am trying to make an audio player that plays .wav files. I wrote a function ReadWaveFile(CString szFilename) for reading the wave data of the file into the WAVEHDR structure. In this function

BOOL CWavePlay::ReadWaveFile(CString szFilename)
{
    hmmio = mmioOpen((LPTSTR)&szFilename,NULL,MMIO_READ);
    ASSERT(hmmio);      //error here: hmmio=0x00000000
    if(hmmio==0)
        return FALSE;
        ....
}

mmioOpen is always returning 0 whenever I pass a filepath to this function for opening the specified file. And what baffles me is when i pass the filepath explicitly in mmioOpen API the code works; i.e., a valid handle is returned. can some body explain why is this happening??

ckram
  • 19
  • 1
  • 4
  • the problem is solved when I have written the code as : hmmio = mmioOpen((LPTSTR)(LPCTSTR)szFilename,NULL,MMIO_READ); – ckram Mar 29 '12 at 07:41

1 Answers1

0

What will happen when you say

MessageBox(NULL,(LPTSTR)&szFilename,"Foo",MB_ICONINFORMATION);

When passing strings to system functions you will need to pick up the pointer to the raw string. For example, if you want to use an std::string object to build your path you will need to say

mmioOpen(filename.c_str(),NULL,MMIO_READ);

Your cast assumes from CString* to LPTSTR assumes that a CString is binary compatible with a LPTSRT which is not the case. When you write LPCTSTR on szFilename you will invoke a cast operator defined on CStrings that converts it to apropriate format. Did you tried just

hmmio = mmioOpen((LPCTSTR)szFilename,NULL,MMIO_READ);

The last cast does not do anything real here so it should be enough.

user877329
  • 6,717
  • 8
  • 46
  • 88