Playsound problem

This is a discussion on Playsound problem within the Windows Programming forums, part of the Platform Specific Boards category; Hi, im veri new to windows programming and im stucked with a problem with playsound. Code: switch (message) { HINSTANCE ...

  1. #1
    rEtaRD r HUMANS TOO !!! rEtard's Avatar
    Join Date
    Feb 2005
    Posts
    80

    Playsound problem

    Hi, im veri new to windows programming and im stucked with a problem with playsound.

    Code:
    switch (message)
    {	
    	HINSTANCE hinstance;
         	case WM_LBUTTONDOWN:
    	PlayWav(hinstance);
    	break;
    .....
    }
    
    void PlayWav(HINSTANCE hinstance)
    {
    	PlaySound((LPCSTR)IDR_SELECT, hinstance, SND_ASYNC | SND_RESOURCE);
    }
    this is a function i made for testing sounds, it compiled alrite but when i run it, i got an error:
    sound.obj : error LNK2001: unresolved external symbol __imp__PlaySoundA@12
    Debug/Sound.exe : fatal error LNK1120: 1 unresolved externals
    What is the problem ? I also tried a very basic code
    Code:
    PlaySound("wave.wav",  NULL, SND_ASYNC | SND_FILENAME);
    Gave me the same runtime error. PLEASE HELP !
    /* Have a nice day */

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You need to link with Winmm.lib (-lwinmm for MinGW/Dev-Cpp).

    See your compiler documentations for the specifics of how to link with additional libraries.

    If you haven't already done so you may have to #include Mmsystem.h, too.

    For future reference, the lib information (and header) is included in the msdn page for the api, for example see the bottom of the page for PlaySound.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    rEtaRD r HUMANS TOO !!! rEtard's Avatar
    Join Date
    Feb 2005
    Posts
    80
    Ken, Can you teach me how to include that lib for visual C++ ? i can't find any documents on libs but im sure i came across it before.
    Last edited by rEtard; 04-10-2005 at 02:29 PM.
    /* Have a nice day */

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    73
    at the beginning, put this in:

    Code:
    #include <Mmsystem.h>
    
    pragma once(lib, Winmm.lib)
    There is some other way to do it for MSVC++ but this works and its easy enough, right?

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Here is an example of linking with multiple libraries. We check that we are on MSVC so that the #pragmas do not cause problems on other compilers. This is typically placed just below your #includes.
    Code:
    #if defined(_MSC_VER)
    #pragma comment(lib, "winmm.lib")
    #pragma comment(lib, "shlwapi.lib")
    #endif

  6. #6
    rEtaRD r HUMANS TOO !!! rEtard's Avatar
    Join Date
    Feb 2005
    Posts
    80
    Guys, i did all u u asked me to about adding the new header file and the new lib but i still got the same runtime error
    sound.obj : error LNK2001: unresolved external symbol __imp__PlaySoundA@12
    Debug/Sound.exe : fatal error LNK1120: 1 unresolved externals
    Code:
    		HINSTANCE hinstance;
         	case WM_LBUTTONDOWN:
    			PlayWav(hinstance);
    			break;
    Code:
    // Function.h
    #include <Windows.h>
    #include <Mmsystem.h>
    #pragma once(lib, Winmm.lib)
    
    void PlayWav(HINSTANCE hinstance)
    {
    	PlaySound((LPCSTR)IDR_SELECT, hinstance, SND_ASYNC | SND_RESOURCE);
    }
    /* Have a nice day */

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    might wanna try

    #pragma comment(lib, "winmm.lib")

    instead of

    #pragma once(lib, Winmm.lib)

  8. #8
    rEtaRD r HUMANS TOO !!! rEtard's Avatar
    Join Date
    Feb 2005
    Posts
    80
    anonytmouse
    X PaYnE X
    THANX FOR YOUR HELP !!! IT FINALLY WORK, but could you explain what the predecessor #pragma means ? I got this from MSDN.
    The #pragma directives offer a way for each compiler to offer machine- and operating system-specific features while retaining overall compatibility with the C and C++ languages. Pragmas are machine- or operating system-specific by definition, and are usually different for every compiler.
    I don't really get what they mean. Anyway, how do i know when i need to include a new .lib like in this case ?
    /* Have a nice day */

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    usually when you get an error like the one you got

    sound.obj : error LNK2001: unresolved external symbol __imp__PlaySoundA@12
    Debug/Sound.exe : fatal error LNK1120: 1 unresolved externals

    it means that the function you are trying to call isnt defined, and since this isnt a function you wrote, it probably needs a library.

    if you have a win32 api reference (or you can use MSDN), you can look up PlaySound and it will tell you what header files/library(s) you need to include.

  10. #10
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I am having the same problem.. using MinGW/Dev-Cpp

    You need to link with Winmm.lib (-lwinmm for MinGW/Dev-Cpp)

    Code:
    #include <windows.h>
    #include <Mmsystem.h>
    
    #pragma comment(lib, "-lwinmm")
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         static TCHAR szAppName[] = TEXT ("HelloWin") ;
         HWND         hwnd ;
         MSG          msg ;
         WNDCLASS     wndclass ;
    
         wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
         wndclass.lpszMenuName  = NULL ;
         wndclass.lpszClassName = szAppName ;
    
         if (!RegisterClass (&wndclass))
         {
              MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
         
         hwnd = CreateWindow (szAppName,                  // window class name
                              TEXT ("The Hello Program"), // window caption
                              WS_OVERLAPPEDWINDOW,        // window style
                              CW_USEDEFAULT,              // initial x position
                              CW_USEDEFAULT,              // initial y position
                              CW_USEDEFAULT,              // initial x size
                              CW_USEDEFAULT,              // initial y size
                              NULL,                       // parent window handle
                              NULL,                       // window menu handle
                              hInstance,                  // program instance handle
                              NULL) ;                     // creation parameters
         
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
         
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
         return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
         HDC         hdc ;
         PAINTSTRUCT ps ;
         RECT        rect ;
         
         switch (message)
         {
         case WM_CREATE:
              PlaySound("hellowin.wav", NULL,  SND_ASYNC) ;
              return 0 ;        
    
         case WM_PAINT:
              hdc = BeginPaint (hwnd, &ps) ;
              
              GetClientRect (hwnd, &rect) ;
              
              DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
                        DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
              
              EndPaint (hwnd, &ps) ;
              return 0 ;
              
         case WM_DESTROY:
              PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }

    I have also tried sndPlaySound("Hellowwin.wav", SND_ASYNC);


    [Linker error] undefined reference to `PlaySoundA@8'
    ld returned 1 exit status
    F:\Dev-Cpp\Makefile.win [Build Error] [hi.exe] Error 1
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  11. #11
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    I haven't used MinGW, but isn't it a more console based compiler run by a DOS-like command? If this is the case I believe when you run MinGW then you would use the -lwinmm as a parameter (such as: c:\>mingw -lwinmm code.c or whatever the command would be).

    Hopefully this will help, correct me if I'm wrong.

  12. #12
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Quote Originally Posted by The Brain
    I have also tried sndPlaySound("Hellowwin.wav", SND_ASYNC);

    [Linker error] undefined reference to `PlaySoundA@8'
    ld returned 1 exit status
    F:\Dev-Cpp\Makefile.win [Build Error] [hi.exe] Error 1
    You know, I have the exact same problem, but I don't use MinGW. I use Dev-C++.
    Any help there cyreon? . vbmenu_register("postmenu_472085", true);

  13. #13
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    I believe you would have a similar fix for your problem as I described above. You can definitely check in your compiler's documentation and it should explain more about including additional library files in your projects.

    I have done all my C programming in MSVC++ so I have yet to use any other compiler. Sorry I can't be of more help (at least with a specific fix that you could use).

  14. #14
    Registered User
    Join Date
    Feb 2006
    Posts
    1
    Hey guys thank you big time.....
    If it werent for God who directed me to this page that showed the solution as

    # pragma comment(lib,"winmm.lib")

    i wouldnt have got my assignment problem solved......
    thank you guys once again and remember....

    16"For God so loved the world that he gave his one and only Son, that whoever believes in him shall not perish but have eternal life. 17For God did not send his Son into the world to condemn the world, but to save the world through him".
    From the book of John chapter 3 verses 16 to 17

    Thanx to you guys.........

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem working PlaySound()...
    By yaya in forum C++ Programming
    Replies: 1
    Last Post: 12-30-2007, 03:17 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21