Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using PlaySound in MFC? Possible??? 1

Status
Not open for further replies.

Rmck87

Programmer
Jul 14, 2003
182
US
Hello, I would like to play a wav file whenever I press a button in a dialog-based MFC Application. So, I have used this code in my include block:

#pragma comment(lib, "Winmm.lib")
#include <Mmsystem.h>

...

and this code to play the file:

if (!PlaySound("C:\\...filename...\\song.wav, NULL, SND_FILENAME)
{
exit(1); //error
}



However, the program will not compile, because it does not recognize PlaySound as a proper identifier. It claims that it is an undeclared identifier. Any help would be much appreciated.

Thanks in advance!

One Ring to Rule Them All, One Ring to Find Them, One Ring to Bring Them All, and in the Darkness Bind Them.
 
Given that your attempted snippet contains several syntax errors to being with, how about

1. posting actual code, inside [code][/code] tags to preserve readability.

2. posting actual error messages. "an error" really doesn't tell us anything.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Ok, here's the code hopefully a little easier to read:

My includes:

Code:
#pragma comment(lib, "Winmm")
#include <Mmsystem.h>
#include "stdafx.h"
#include "ThreadWatcher.h"
#include "ThreadWatcherDlg.h"
#include <cstdlib>


My sound calling block:

Code:
if (!PlaySound("C:\\Documents and Settings\\My Documents\\ThreadWatcher\\ThreadWatcher\\containfail.WAV", NULL, SND_FILENAME))
			{
			exit(1); //error
			}


containfail.wav is the filename of the wav file I would like to play.
ThreadWatcher is the name of the application I am writing.



And here are the errors I am receiving:

.\ThreadWatcherDlg.cpp(257) : error C2065: 'SND_FILENAME' : undeclared identifier

.\ThreadWatcherDlg.cpp(257) : error C3861: 'PlaySound': identifier not found

It doesn't like SND_FILENAME, or PlaySound


I just assumed they were related errors, so I only mentioned PlaySound as the error.


Thanks again...

- Ryan

One Ring to Rule Them All, One Ring to Find Them, One Ring to Bring Them All, and in the Darkness Bind Them.
 
What includes windows.h ?

Also (AFAIK), stdafx.h is the magic file which marks the end of the pre-compiled headers. As I understand it, if you're using pre-compiled headers, then the list of includes before "stdafx.h" has to be consistent across ALL source files. Any variation gets ignored.

I'm assuming that your stdafx.h includes windows.h, in which case the answer would be to move
[tt]#pragma comment(lib, "Winmm")
#include <Mmsystem.h>[/tt]
down a couple of lines, so they're after stdafx.h





--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
you were right. Thank you. "stdafx.h" has "windows.h" included in it, btw.

Thanks for the help!

One Ring to Rule Them All, One Ring to Find Them, One Ring to Bring Them All, and in the Darkness Bind Them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top