Recent

Author Topic: Application location and sndPlaySound  (Read 2197 times)

pasquale

  • Sr. Member
  • ****
  • Posts: 267
    • Esposito Software
Application location and sndPlaySound
« on: December 27, 2020, 11:30:18 am »
Hello, eveybody.

In my Windows app, I'm trying to figure out how to play a WAV file safely in the presence of special characters.

I have tried the following code (after putting MMSystem in Uses), which works fine, provided that the application location does not include any stressed letters (e.g. àèéìòù) or any other special characters:

Code: Pascal  [Select][+][-]
  1. sndPlaySound(pchar(UTF8ToSys(Application.Location + 'welcome.wav')), snd_Async or snd_NoDefault);

The presence of spaces doesn't seem to interfere with the exact app path, whereas special characters are not recognized correctly. The problem is, my language (Italian) is full of stressed letters and I just can't ignore the problem. In fact, the chances of the application location containing special characters are not slim at all.

Do you know any solution to my problem?

Thanks in advance.
Apple loves breaking backward compatibility to make money. If you want to be sure that your apps will never stop working, use Windows and trash macOS!

pasquale

  • Sr. Member
  • ****
  • Posts: 267
    • Esposito Software
Re: Application location and sndPlaySound
« Reply #1 on: December 27, 2020, 02:21:17 pm »
If sndPlaySound does not allow you to override the issue with special characters, any other solution would be welcome.
Apple loves breaking backward compatibility to make money. If you want to be sure that your apps will never stop working, use Windows and trash macOS!

jamie

  • Hero Member
  • *****
  • Posts: 6133
Re: Application location and sndPlaySound
« Reply #2 on: December 27, 2020, 03:22:34 pm »
Try that with WIDESTRING or unicodeString.
The only true wisdom is knowing you know nothing

pasquale

  • Sr. Member
  • ****
  • Posts: 267
    • Esposito Software
Re: Application location and sndPlaySound
« Reply #3 on: December 27, 2020, 03:29:14 pm »
Try that with WIDESTRING or unicodeString.

I don't have much familiarity with Lazarus. Can you tell me where I can find some examples?

Thanks.
Apple loves breaking backward compatibility to make money. If you want to be sure that your apps will never stop working, use Windows and trash macOS!

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Application location and sndPlaySound
« Reply #4 on: December 27, 2020, 03:38:11 pm »
This is working (tested):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   fn: WideString;
  4. begin
  5.   fn := Application.Location + 'äöü.wav';
  6.   sndPlaySoundW(PWideChar(fn), snd_Async or snd_NoDefault);
  7. end;
Note the "W" appended to "sndPlaySound" because it tells Windows that the filename is a pointer to a wide string rather than to an ansistring.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4468
  • I like bugs.
Re: Application location and sndPlaySound
« Reply #5 on: December 27, 2020, 03:49:56 pm »
Yes, wp nailed it.
Here is more information about Unicode in Lazarus programs / applications :
 https://wiki.freepascal.org/Unicode_Support_in_Lazarus
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

pasquale

  • Sr. Member
  • ****
  • Posts: 267
    • Esposito Software
Re: Application location and sndPlaySound
« Reply #6 on: December 27, 2020, 03:50:46 pm »
This is working (tested):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   fn: WideString;
  4. begin
  5.   fn := Application.Location + 'äöü.wav';
  6.   sndPlaySoundW(PWideChar(fn), snd_Async or snd_NoDefault);
  7. end;
Note the "W" appended to "sndPlaySound" because it tells Windows that the filename is a pointer to a wide string rather than to an ansistring.

Thanks a lot. Your code works great. Problem solved.
Apple loves breaking backward compatibility to make money. If you want to be sure that your apps will never stop working, use Windows and trash macOS!

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Application location and sndPlaySound
« Reply #7 on: December 27, 2020, 04:03:46 pm »
B.t.w.: UTF8ToSys is a no-op in a default Lazarus setup.
If you want to translate Utf8 to Windows codepage (ANSI) use Utf8ToWinCP instead.

That being said, the solution with the W version of teh windows API is better (it will allow for any unicode character, whereas Utf8ToWinCP only works if the expected result is inside your current codepage, so on my locale, it will fail with e.g. cyrillic cgaracters).

Bart

pasquale

  • Sr. Member
  • ****
  • Posts: 267
    • Esposito Software
Re: Application location and sndPlaySound
« Reply #8 on: December 27, 2020, 04:46:43 pm »
B.t.w.: UTF8ToSys is a no-op in a default Lazarus setup.
If you want to translate Utf8 to Windows codepage (ANSI) use Utf8ToWinCP instead.

That being said, the solution with the W version of teh windows API is better (it will allow for any unicode character, whereas Utf8ToWinCP only works if the expected result is inside your current codepage, so on my locale, it will fail with e.g. cyrillic cgaracters).

Bart

Thanks.
Apple loves breaking backward compatibility to make money. If you want to be sure that your apps will never stop working, use Windows and trash macOS!

 

TinyPortal © 2005-2018