OK....sigh...
I need to link to lwinmm to use playsound and stuff
Looked around and looked like I needed to do it like this:

I'm using Codeblocks

#include <iostream>
#include<windows.h>
#include <mmsystem.h>
#pragma comment (lib, "winmm.lib")

using namespace std;

int main()
{

 PlaySound((LPCWSTR) "c:\\Snap ya fingers.wav", NULL, SND_FILENAME | SND_ASYNC);

    cout << "Hello world!" << endl;
    return 0;
}

But the errors I get are:
C:...\main.cpp|8|warning: ignoring #pragma comment |

C:...\main.cpp|15|error: cannot convert `const WCHAR*' to `const CHAR*' for argument `1' to `BOOL PlaySoundA(const CHAR*, HINSTANCE__*, DWORD)'|
||=== Build finished: 1 errors, 1 warnings ===|

Also, I have done this:
Project -> Build Options -> Linker settings then gone down to Link Libraries and added -winmm as some site said should work.

Does not work - same errors

Also, I have done this:
Project -> Build Options -> Linker settings then gone down to Link Libraries and added -winmm as some site said should work.

Does not work - same errors

OK.. I even pressed add (at Link libraries:) and found libwinmm.a so it chose the full path:
C:\blablabla\libwinmm.a
Same errors-...

The problem is not a linking error, it's a compilation error!

If you take a look at the definition of Playsound in mmsystem.h:

WINMMAPI BOOL WINAPI PlaySoundA( IN LPCSTR pszSound, IN HMODULE hmod, IN DWORD fdwSound);
WINMMAPI BOOL WINAPI PlaySoundW( IN LPCWSTR pszSound, IN HMODULE hmod, IN DWORD fdwSound);
#ifdef UNICODE
#define PlaySound  PlaySoundW
#else
#define PlaySound  PlaySoundA
#endif // !UNICODE

If your project is set up to use Unicode, PlaySound will use PlaySoundW. Otherwise it uses PlaySoundA.
The only differences in these functions are in the parameter lists. PlaySoundW takes a const WCHAR* (aka LPCWSTR) for the file-path parameter (pszSound), whereas PlaySoundA takes a const CHAR* (aka LPCSTR) for the file-path parameter.

From the error messages you've posted, PlaySound is using PlaySoundA, NOT PlaySoundW, which indicates that your project is NOT set up to use unicode.

The reason that you are getting an error message is because in your call to PlaySound you're converting the file path to a const WCHAR* (or LPCWSTR), which is only used by PlaySoundW. But the compiler is using PlaySoundA, which is expecting a const CHAR* (or LPCSTR) for the path, not a const WCHAR*. So the compiler is complaining that the parameters you're passing in your call to PlaySound do not match the parameters required for PlaySoundA.

Fortunately there are two simple ways of fixing this problem.
Either:
1. Set up your project to use Unicode, which will cause the compiler to use PlaySoundW when calls to PlaySound are made in the code, which will mean that your existing code will work.
OR:
2. Leave the project as it is and remove the cast to LPCWSTR in your call to PlaySound.
e.g.

PlaySound("c:\\Snap ya fingers.wav", NULL, SND_FILENAME | SND_ASYNC);

If you don't need to be using unicode in your project then option 2 is probably your best bet!

Cheers for now,
Jas.

It doesn't play any music!
I don't get any error messages but it won't play any music...?

#include <iostream>
#include <windows.h>
#include <mmsystem.h>


using namespace std;

int main()
{

PlaySound("c:\\dada.wav", NULL, SND_FILENAME | SND_ASYNC);

    cout << "Hello world!" << endl;
    return 0;
}

Tried with both .wav and .mp3

Edited 5 Years Ago by sha11e: n/a

Ups, I had to make the program wait (i used cin.get() ) because the program would just run and end (but the window is still up using Codeblocks)

This question has already been answered. Start a new discussion instead.