Top

C++/Win32: Sounds

March 2, 2008

This might be the shortest lesson of all. All I want to do here is play a sound. Why? Well in the cast of a port scanner, the user is probably looking for open ports on a system. The majority of the ports scanned will be closed, and he/she might be looking over 8000 ports for the few that will be open. The user might better occupy his time doing something, anything, besides staring at our application. So a sound alert might be a nice option.

I have to say here that sounds are generally an annoyance. If you want to put sounds in your program, you should have a really good reason to use them, you should give the user the option to turn them off, you should consider giving the user the option to choose his own sounds, and you should definitely consider having them turned off by default. I typically hate sounds. I don’t IM a lot because of the sound. Yeah, I know I can disable sounds, but everywhere I go?

Anyways, we’ll add a discrete sound here when our scanner finds an open port, and we’ll give the user the option to disable sounds. We will enable them by default, just for the sake of this example, but I would probably not do this normally.

So we’ll need a BOOL in our PortScannerWindow class, and a call to PlaySound in goScan if we find an open port and m_hSoundsEnabled is true.

That’s it.

We’ll use the relatively unobtrusive notify sound found in the c:\windows\media folder.

// Play sound
if (m_bSoundsEnabled) PlaySound("notify.wav", NULL, SND_FILENAME);

If you run this as is, you’ll notice that the app takes a performance hit. Actually, it doesn’t. What happens is when you call PlaySound, the function doesn’t return until the sound is finished playing. So you end up lagging waiting for the sound to play. To correct this, pass in the SND_ASYNC flag, which causes PlaySound to return immediately rather than waiting for the sound to finish. The sound will keep playing, you just won’t have to wait for it to finish. Remember, I like to give the user options, so I’ll give him/her the option of syncing the sound. Maybe he/she wants to hear a sound every time a port is found open! Fine with me, just set the flag on the menu.

That’s it. Just remember that the sound file you are loading must be accessible, which either means you must provide the full path to the file, or the filename must reflect a path relative to the executable. When you are running the app from inside Visual Studio, the path must be relative to your project folder.

You must also remember to tell the linker to include the winmm.lib library in your project (project->project properties->linker->input).

Next: Context Menus.


Source Code:

win32tut_part18.zip [3.40MB zipped]

Additional Information:

· TheForger’s Win32 API Tutorial - Files target=_blank>CodeProject - ListViews

Further Reading:

· Nitty Gritty Windows Programming with C++ by Henning Hansen.

· Thinking in C++ by Bruce Eckel.

Comments

3 Responses to “C++/Win32: Sounds”

  1. Wholesale NFL Jerseys on August 30th, 2010 4:53 pm

    To find your favorite team’s NFL jerseys just click here:www.stajump.com

  2. cheap nfl jerseys on September 3rd, 2010 5:08 am

    Couldn’t agree more…Had a post already written about this too, (good job i checked around first)never mind.It is a really good tune though.
    Here’s hoping more labels wise up and do this.

  3. Palolem on September 4th, 2010 9:05 pm

    Wow, nice info

Got something to say?





Bottom