Thread: Help!!!(Again)

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    75

    Question 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

  2. #2

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    75
    ------ 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

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    75
    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;
    }

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    75
    Thanks Again

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    75
    Anyone know what is going on???

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Link against Winmm.lib (found in your linker options).

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    75

    Question

    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

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    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);
    Last edited by robwhit; 11-03-2007 at 06:06 PM.

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    75
    It works now. Thank you very much

Popular pages Recent additions subscribe to a feed