Click here to Skip to main content
11,416,194 members (65,753 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC
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
<pre>
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??
Posted 28-Mar-12 20:17pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this - hmmio = mmioOpen((LPTSTR)szFilename,NULL,MMIO_READ);
You could also use the PlaySound API with the SND_FILENAME flag.
  Permalink  
Comments
stib_markc at 29-Mar-12 1:50am
   
That is what i actually did, please see my code...
PlaySound works only with small files. As I said I am trying to make an audio player, not trying to add (small)sounds to my program.
«_Superman_» at 29-Mar-12 2:07am
   
If you look carefully you will see that an & is missing in my post.
Are you sure PlaySound only works with small files. I'm not aware of this limitation. Please check with multiple files.
stib_markc at 29-Mar-12 2:58am
   
error C2440: 'type cast' : cannot convert from 'class CString' to 'char *' the code doent build... and yes the PlaySound function doesn't play the file fully(if it is larger than normal windows sound files). I checked it with multiple files.
«_Superman_» at 29-Mar-12 3:30am
   
Try this - hmmio = mmioOpen((LPTSTR)(LPCTSTR)szFilename,NULL,MMIO_READ);
stib_markc at 29-Mar-12 3:39am
   
Thank you <<_Superman_>> the problem is solved.... but why this type of casting is needed, I mean what is difference?
«_Superman_» at 29-Mar-12 3:47am
   
The CString class has an overloaded LPCTSTR operator which returns a const TCHAR*.
So the inner cast is an explicit call to this overloaded operator which we then cast to a LPTSTR which is nothing but a TCHAR*.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

the solution is:
<pre>hmmio = mmioOpen((LPTSTR)(LPCTSTR)szFilename,NULL,MMIO_READ);
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,295
1 OriginalGriff 7,592
2 Maciej Los 3,718
3 Abhinav S 3,456
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web03 | 2.8.150427.4 | Last Updated 29 Mar 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100