A Simple way to play a sound on a form?


just trying to play a sound when the user presses a button.

This won't compile. Gives a very long error message which I don't understand

mciSendString(TEXT("open \"Music.mp3\" type mpegvideo alias mp3"), NULL, 0, NULL);
mciSendString(TEXT("play mp3"), NULL, 0, NULL);


Adding Winmm.lib in the Project Linker Settings gets rid of the error:)

Can anyone explain to me what the Linker Does and why I needed to change it?

Thanks in advance.
Last edited on
Can anyone explain to me what the Linker Does and why I needed to change it?
Hard to say what the exact issue was without seeing the error, but the point of the linker is to actually connect that function name, mciSendString, to actual code behind that function. Just #including the declaration of mciSendString isn't enough, because it doesn't tell you how to actually execute the function.

The object code for that function must exist in the Winmm.lib, so by linking to that library, it now knows how to actually call and run that function.
Last edited on
the linker puts all the pieces together. When you get to the linker, you have file.cpp's object, file2.cpp's object, main.cpp's object, somelibrary.lib, ... and whatever junk all in a pile.
the linker grabs all that stuff and mashes it together into the actual executable program file.

a library is basically a compiled program that left out the main() function. It does not run, because it lacks main, but in a simple example, it has say a bunch of functions, those are ready to be called. The program using it calls the functions. The linker bridges that gap by connecting the compiled function in the library to its call in the user.

Windows has a lot of functionality a program uses. Do you as a programmer really want to write all the code needed to manage windows and menus and the multi-media system?

I sure don't.

A Windows-based compiler has precompiled bits of assembled code for interacting with the Windows interface that get linked into your executable by the compiler's toolchain.

There are key bits of pre-assembled code .lib files that are automatically added when compiling even the most rudimentary WinAPI source. Most of the WinAPI code has to be manually added to a project's make file so the linker can use it.

winmm.lib is one of those bits of code.
Thanks so much guys for the good explanations. I understand it now.

What does the capital TEXT mean in my first post. I tried googgling it....

TEXT("") is a replacement macro that changes when compiling WinAPI source as ANSI or Unicode.

ANSI is as dead as Win9x/Me. Every Windows version since Win NT has used Unicode.

Many WinAPI functions are similar, there are two versions. One for ANSI, the other for Unicode. MessageBox() for example is a replacement macro for either MessageBoxA() or MessageBoxW().


MS recommends new WinAPI apps use explicit Unicode encoding.
mciSendString(L"play mp3", NULL, 0, NULL);

You should spend some time learning what the WinAPI data types are:

Notice the type of the first two parameters of mciSendString are:

LPCTSTR and LPTSTR. Replacement macros for pointers to strings.
Last edited on
... which are now also the same as PTSTR and PCTSTR since Windows moved from 16 bits.

For those not of that era, LP sends for Long Pointer and P for Pointer (or short pointer). Long Pointer was for a pointer outside of the referenced segment and Pointer was for within the same segment. Segments were of 64K bytes - but the 8086 could address up to 1M bytes.
Topic archived. No new replies allowed.