Can't get an old WIN32 WINAPI program to compile

399 Views Asked by At

I tried CodeBlocks and MS VC++ 2010 but both fail to compile.

They can't find the definition for PlaySound() in . What is the Problem?

case WM_CREATE:
    PlaySound (TEXT("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
    return 0 ;

Errors:

error C2065: 'SND_FILENAME': nichtdeklarierter Bezeichner
error C2065: 'SND_ASYNC': nichtdeklarierter Bezeichner
error C3861: "PlaySound": Bezeichner wurde nicht gefunden.

windows.h is included in stdafx.h but even if I include it directly in codeblocks it doesn't work.

3

There are 3 best solutions below

3
datenwolf On

The symbols for PlaySound and SND_… are declared in mmsystem.h header and defined in the winmm.lib library. You must include and link against those.

4
Aykhan Hagverdili On

PlaySound function is declared in Mmsystem.h (which is included by Windows.h). Make sure to include that. See more on that function here. Note that if WIN32_LEAN_AND_MEAN is defined, Windows.h does not include Mmsystem.h. Make sure to #undef WIN32_LEAN_AND_MEAN before #include <Windows.h> if you notice a problem.

Then if you have a linker error, read a little more from the book this exercise comes from (Programming Windows 5th ed Chapter 2):

A couple of warnings: If you use Microsoft Visual C++ to create a new project for this program, you need to make an addition to the object libraries the linker uses. Select the Settings option from the Project menu, and pick the Link tab. Select General from the Category list box, and add WINMM.LIB (“Windows multimedia”) to the Object/Library Modules text box. You need to do this because HELLOWIN makes use of a multimedia function call, and the multimedia object library isn’t included in a default project. Otherwise you’ll get an error message from the linker indicating that the PlaySound function is unresolved.

1
MARSHMALLOW On

Try including MMSystem.h and Windows.h because by default, MMSystem.h should be included in Windows.h, but when WIN32_LEAN_AND_MEAN is defined, Windows.h will not include it.

And you should also add winmm.lib to the linker (by typing -lwinmm) because symbols of PlaySound, SND_SYNC, SND_ASYNC and SND_FILENAME are defined there.