×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Using PlaySound in MFC? Possible???

Using PlaySound in MFC? Possible???

Using PlaySound in MFC? Possible???

(OP)
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.

RE: Using PlaySound in MFC? Possible???

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.

RE: Using PlaySound in MFC? Possible???

(OP)
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.

RE: Using PlaySound in MFC? Possible???

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
#pragma comment(lib, "Winmm")
#include <Mmsystem.h>

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.

RE: Using PlaySound in MFC? Possible???

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close