Click here to Skip to main content
15,901,284 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Templates... they are nasty!! Pin
Stuart Dootson4-Sep-09 1:52
professionalStuart Dootson4-Sep-09 1:52 
QuestionPlaySound(MAKEINTRESOURCE, IDR_WAVE_RANDOM... problem Pin
lctrncs3-Sep-09 7:08
lctrncs3-Sep-09 7:08 
QuestionRe: PlaySound(MAKEINTRESOURCE, IDR_WAVE_RANDOM... problem Pin
David Crow3-Sep-09 7:18
David Crow3-Sep-09 7:18 
AnswerRe: PlaySound(MAKEINTRESOURCE, IDR_WAVE_RANDOM... problem Pin
lctrncs3-Sep-09 12:27
lctrncs3-Sep-09 12:27 
GeneralRe: PlaySound(MAKEINTRESOURCE, IDR_WAVE_RANDOM... problem Pin
David Crow3-Sep-09 18:15
David Crow3-Sep-09 18:15 
GeneralRe: PlaySound(MAKEINTRESOURCE, IDR_WAVE_RANDOM... problem Pin
lctrncs3-Sep-09 19:09
lctrncs3-Sep-09 19:09 
AnswerRe: PlaySound(MAKEINTRESOURCE, IDR_WAVE_RANDOM... problem Pin
iraclyKv3-Sep-09 22:58
iraclyKv3-Sep-09 22:58 
AnswerMore Detailed Information About the Problem Re: PlaySound(MAKEINTRESOURCE, IDR_WAVE_RANDOM... problem Pin
lctrncs4-Sep-09 9:06
lctrncs4-Sep-09 9:06 
Thanks to everyone at CodeProject!

It appears that the original post may not be clear.

Therefore, consider the following code snippet which includes much more information about the problem and the (failed) attemtps to resolve it.

Again, for reasons this beginner has yet to understand, PlaySound works fine with a filename constructed from a CString and a random number (/path/filename1.wav), but fails when concatenated CStrings are used to create a resouce identifier (IDR_WAVE_1).

Please consider the code and coments below.


//Create random resource ID (similar approach used to create random wave file names)

	int m_nRandomSound;
	m_nRandomSound = getrandom( 500, 509  );		//Place the random number into the variable n_nRandomRecord

	CString m_sStrRandomSound;						
	m_sStrRandomSound = "";

	CString m_sStrRandomResourcePrefix;			//Create the resource prefix
	m_sStrRandomResourcePrefix = "IDR_WAVE_";

	m_sStrRandomSound.Format("%d", m_nRandomSound);	//Convert the random integer to a CString


//Concatentate the strings into the resource identifier
	CString m_sStrRandomResourcePrefixWav;
	m_sStrRandomResourcePrefixWav = m_sStrRandomResourcePrefix + m_sStrRandomSound;

	
//Set the resource identifier directly
	CString m_sStrRandomResourcePrefixWav2;
	m_sStrRandomResourcePrefixWav2="IDR_WAVE_503";


//  
//
//Demonstrate function similar method that plays sounds using concatenated CString of path/filename
PlaySound(m_sStrRandomSoundPathWav, GetModuleHandle(NULL),  SND_FILENAME|SND_SYNC );  
          //PLAYS from concatenated CString 
          //(e.g. \path\filename?.wav - where ? is an integer converted to a CString)


PlaySound(MAKEINTRESOURCE(IDR_WAVE_503), GetModuleHandle(NULL),  SND_SYNC|SND_RESOURCE );
          //PLAYS IDR_WAVE_503
          //which is directly entered an not subtituted in any way
//
//
//ATTEMPTS TO EMPLOY CONCATENATED STRING TO PLAY RANDOM WAVE FILE

//PlaySound(MAKEINTRESOURCE(m_sStrRandomResourcePrefixWav), GetModuleHandle(NULL),  SND_SYNC|SND_RESOURCE );
          //error C2440: 'type cast' : cannot convert from 'class CString' to 'unsigned short'
          //included for completeness 


//PlaySound(IDR_WAVE_503, GetModuleHandle(NULL),  SND_SYNC|SND_RESOURCE );	
          //error C2664: 'PlaySoundA' : cannot convert parameter 1 from 'const int' to 'const char *'
          //included for completeness 


PlaySound(m_sStrRandomResourcePrefixWav2, GetModuleHandle(NULL),  SND_SYNC|SND_RESOURCE );
          //Compiles but wav not played - 
          //Visual Studio reports (directly intialized as IDR_WAVE 503) m_sStrRandomResourcePrefixWav as "IDR_WAVE 503"


PlaySound("m_sStrRandomResourcePrefixWav", GetModuleHandle(NULL),  SND_SYNC|SND_RESOURCE );
          //Compiles but wav not played - 
          //Visual Studio reports (CONCATENATED CString IDR_WAVE 503) m_sStrRandomResourcePrefixWav as "IDR_WAVE 503"


//
//
//Attempt to cast the CString to an unsigned short

LPCTSTR pRandomSound = m_sStrRandomResourcePrefixWav.GetBuffer(0);	
LPCTSTR pRandomSound2 = m_sStrRandomResourcePrefixWav2.GetBuffer(0);
//

PlaySound(MAKEINTRESOURCE(pRandomSound2), GetModuleHandle(NULL),  SND_SYNC|SND_RESOURCE );
          //Compiles but wav not played - 
          //Visual Studio reports (directly intialized as IDR_WAVE 503) m_sStrRandomResourcePrefixWav as 0x02bd08bc "IDR_WAVE 503"
//

PlaySound(MAKEINTRESOURCE(pRandomSound), AfxGetResourceHandle(),  SND_SYNC|SND_RESOURCE );
          //Compiles but wav not played - 
          //Visual Studio reports (concatenated as IDR_WAVE 503) m_sStrRandomResourcePrefixWav as 0x02bd092c "IDR_WAVE 503"
//

PlayWav(pRandomSound);      //A different approach employing loading/locking resource
          //behaves similarly
          //Compiles but wav not played - 
          //Visual Studio reports (concatenated as IDR_WAVE 503) m_sStrRandomResourcePrefixWav as 0x02bd092c "IDR_WAVE 503" etc.
//

m_sStrRandomResourcePrefixWav.ReleaseBuffer();
m_sStrRandomResourcePrefixWav2.ReleaseBuffer();




Thank you again.

Your comments and suggestions are greatly appreciated.

"For a successful technology, reality must take precedence over public relations, for nature cannot be fooled." Richard Feynman, Minority Report to the Official Report on the Space Shuttle Challenger Crash

QuestionHow to create detailed log.txt file in C programming. Pin
mohant$.net3-Sep-09 3:00
mohant$.net3-Sep-09 3:00 
AnswerRe: How to create detailed log.txt file in C programming. Pin
Hamid_RT3-Sep-09 3:28
Hamid_RT3-Sep-09 3:28 
AnswerRe: How to create detailed log.txt file in C programming. Pin
Maximilien3-Sep-09 3:44
Maximilien3-Sep-09 3:44 
QuestionRe: How to create detailed log.txt file in C programming. Pin
David Crow3-Sep-09 5:10
David Crow3-Sep-09 5:10 
AnswerRe: How to create detailed log.txt file in C programming. Pin
Chris Losinger3-Sep-09 11:31
professionalChris Losinger3-Sep-09 11:31 
QuestionCursor freeze in rich edit control Pin
theCPkid3-Sep-09 2:47
theCPkid3-Sep-09 2:47 
AnswerRe: Cursor freeze in rich edit control Pin
Code-o-mat3-Sep-09 4:20
Code-o-mat3-Sep-09 4:20 
GeneralRe: Cursor freeze in rich edit control Pin
theCPkid4-Sep-09 21:19
theCPkid4-Sep-09 21:19 
QuestionHow can i chk the file is readonly or not? Pin
Le@rner3-Sep-09 1:40
Le@rner3-Sep-09 1:40 
AnswerRe: How can i chk the file is readonly or not? Pin
Michael Schubert3-Sep-09 1:44
Michael Schubert3-Sep-09 1:44 
GeneralRe: How can i chk the file is readonly or not? Pin
Le@rner3-Sep-09 1:57
Le@rner3-Sep-09 1:57 
GeneralRe: How can i chk the file is readonly or not? Pin
Michael Schubert3-Sep-09 2:07
Michael Schubert3-Sep-09 2:07 
GeneralRe: How can i chk the file is readonly or not? Pin
Richard MacCutchan3-Sep-09 2:08
mveRichard MacCutchan3-Sep-09 2:08 
GeneralRe: How can i chk the file is readonly or not? Pin
Le@rner3-Sep-09 2:42
Le@rner3-Sep-09 2:42 
QuestionAssociating an extension to a MFC application - advanced Pin
BadJerry3-Sep-09 1:17
BadJerry3-Sep-09 1:17 
AnswerRe: Associating an extension to a MFC application - advanced Pin
Richard MacCutchan3-Sep-09 2:15
mveRichard MacCutchan3-Sep-09 2:15 
GeneralRe: Associating an extension to a MFC application - advanced Pin
BadJerry3-Sep-09 2:47
BadJerry3-Sep-09 2:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.