Thread: playsound error

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    221

    playsound error

    Code:
    #include <iostream>
    #include <windows.h>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    {
    	int i;
    	//char *prt = "C:\\WINDOWS\\Media\\chord.wav";
    	cout << "test";
    	do
    	{
    		i = getch();
    
    		if ( i == 13)
    		{
    			PlaySound("chord.wav", NULL, SND_ALIAS);
    		}
    	}while(1);
    	return 0;
    }
    what am i diong wrong here?
    here are the linker errors im geting

    Linking...
    Text1.obj : error LNK2001: unresolved external symbol __imp__PlaySoundA@12
    Debug/Text1.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    btw, chord.wav is in my current directory

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I could be wrong... but I think I had a problem with including windows.h and using namespace std. I think I had to use the old style headers and leave out namespace std.

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    Code:
    #include <iostream.h>
    #include <windows.h>
    #include <conio.h>
    
    //using namespace std;
    
    int main()
    {
    	int i;
    	//char *prt = "C:\\WINDOWS\\Media\\chord.wav";
    	//cout << "test";
    	do
    	{
    		i = getch();
    
    		if ( i == 13)
    		{
    			PlaySound("chord.wav", NULL, SND_ALIAS);
    		}
    	}while(1);
    	return 0;
    }
    same errors

    does this only work in windows programs, im using this in a win32 console application

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Your getting a linking error because it can't find the actual implementation of PlaySound. You need to link to the appropriate library winmm.lib either in the project settings or explicitly for Visual Studio by using this line near the top of the file.

    Code:
    #pragma comment(lib,"winmm.lib");

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I guess I was wrong.

    PlaySound() should work in console!

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    THANK YOU VERY MUCH


    omg my project is going to be bomb!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM