0
String ^ fileName = textBox5->Text + "DES.wav";

PlaySound(fileName, NULL, SND_FILENAME | SND_SYNC);

The error is saying:

Error 4 error C2664: 'PlaySoundW' : cannot convert parameter 1 from 'System::String ^' to 'LPCWSTR'

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455

1 Answers1

0

No need to mix managed and unmanaged code, use SoundPlayer class:

String ^ fileName = textBox5->Text + "DES.wav";
SoundPlayer^ player = gcnew SoundPlayer(filename);
player->PlaySync();    // or Play for asynchronous execution

http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx

Alex F
  • 42,307
  • 41
  • 144
  • 212
  • I tried this code with all the valid include statements and used the necessary namespaces but i get a runime error: An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll. I even tried triming my file name to remove leading and trailing white spaces Additional information: Illegal characters in path. – user2182790 Apr 20 '13 at 14:55
  • What line throws exception? What is the value of filename variable? Does such file exist? Debug it and provide more information. – Alex F Apr 20 '13 at 15:01
  • I can type the file name in as a string "wordDES.wav" and it plays the sound. But when ever i try to concat the word (which varies at run time) the exception occurs.A first chance exception of type 'System.UriFormatException' occurred in System.dll A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll Additional information: Illegal characters in path. – user2182790 Apr 20 '13 at 15:39
  • What is the value of textbox? What is the value of fileName variable? Use debugger. This code prints `wordDES.wav` in my test: `String ^ fileName = "word" + "DES.wav"; Console::WriteLine(fileName);` – Alex F Apr 20 '13 at 15:53
  • I figured out my error. When i was getting a string from another textbox and setting it in text box 5, i forgot to trim it so it had some leading characters that wasn't being shown and affected the playsound function. Thanks for the help! :) – user2182790 Apr 20 '13 at 21:02