-
Help!!!(Again)
Could someone explain why I am getting these errors:
Code:
#include <iostream>
#include <string>
#include <windows.h>
#include <cstdlib>
using namespace std;
int main()
{
cout << "Hello world" << endl;
system ("PAUSE");
PlaySound("music30.wav.wav", NULL, SND_ASYNC | SND_FILENAME);
system ("PAUSE");
return 0;
}
------ Build started: Project: soundtest, Configuration: Debug Win32 ------
Compiling...
sound test.cpp
c:\documents and settings\hp_administrator\my documents\visual studio 2005\projects\soundtest\soundtest\sound test.cpp(11) : error C2664: 'PlaySoundW' : cannot convert parameter 1 from 'const char [16]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Build log was saved at "file://c:\Documents and Settings\HP_Administrator\My Documents\Visual Studio 2005\Projects\soundtest\soundtest\Debug\BuildLog.h tm"
soundtest - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I AM NEW TO C++. Please explain in simple terms. Thank You
-
-
------ Build started: Project: soundtest, Configuration: Debug Win32 ------
Compiling...
sound test.cpp
Linking...
sound test.obj : error LNK2019: unresolved external symbol __imp__PlaySoundW@12 referenced in function _main
C:\Documents and Settings\HP_Administrator\My Documents\Visual Studio 2005\Projects\soundtest\Debug\soundtest.exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://c:\Documents and Settings\HP_Administrator\My Documents\Visual Studio 2005\Projects\soundtest\soundtest\Debug\BuildLog.h tm"
soundtest - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
...Did not work
-
Forgot to post new code
Code:
#include <iostream>
#include <string>
#include <windows.h>
#include <cstdlib>
using namespace std;
int main()
{
cout << "Hello world" << endl;
system ("PAUSE");
PlaySound(L"music30.wav.wav", NULL, SND_ASYNC | SND_FILENAME);
system ("PAUSE");
return 0;
}
-
-
Anyone know what is going on???
-
>> sound test.obj : error LNK2019: unresolved external symbol __imp__PlaySoundW@12 referenced in function _main
This means you need to link to the library that implements PlaySound.
-
Link against Winmm.lib (found in your linker options).
-
Here is a code update
Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <windows.h>
#include <mmsystem.h>
#include <stdlib.h>
#pragma comment(lib, "winmm.lib")
using namespace std;
int main()
{
PlaySound((LPCWSTR) "C:\\music30.wav", NULL, SND_FILENAME | SND_ASYNC);
system("PAUSE");
return 0;
}
I used this same code in Visual Studio 2005 Pro (The program I want to use) and in Dev C++ (Bloodshed). In Bloodshed I hear the correct music, but in visual i only hear a single beep noise. Please help. Thanks
-
Code:
PlaySound((LPCWSTR) "C:\\music30.wav", NULL, SND_FILENAME | SND_ASYNC);
PlaySound(L"music30.wav.wav", NULL, SND_ASYNC | SND_FILENAME);
the first one is even more broken than the second one.
you can't just cast it and hope that it goes ok. it doesn't work like that. you need to use the macros like _T and TEXT.
PlaySound is a macro that expands to PlaySoundA or PlaySoundW, depending on what you have defined for your unicode project settings. if it's defined, it will change it to PlaySoundW and it expects a LPCWSTR (aka L"string"), if not, then it will use PlaySoundA and want the plain string (aka "string" aka LPCSTR).
like this:
Code:
PlaySound(TEXT("C:\\music30.wav"), NULL, SND_FILENAME | SND_ASYNC);
PlaySound(_T("C:\\music30.wav"), NULL, SND_FILENAME | SND_ASYNC);
PlaySoundA("C:\\music30.wav", NULL, SND_FILENAME | SND_ASYNC);
PlaySoundW(L"C:\\music30.wav", NULL, SND_FILENAME | SND_ASYNC);
wrong:
Code:
PlaySound(L"music30.wav", NULL, SND_ASYNC | SND_FILENAME);
PlaySound((LPCWSTR) "C:\\music30.wav", NULL, SND_FILENAME | SND_ASYNC);
PlaySound("C:\\music30.wav", NULL, SND_FILENAME | SND_ASYNC);
PlaySoundA(L"C:\\music30.wav", NULL, SND_FILENAME | SND_ASYNC);
-
It works now. Thank you very much