2

So I am trying to make a simple DLL to play sounds in another program. I am trying to use the playsound function, and I have it set up, but when I run it in the other program, I get the default beep. Here is my code.

export double sound_manage(const char* file_old,double handler) 
{
    /*
    Handler values:
    0 - Play sound
    1 - Loop sound
    2 - Stop sound
    */
    bool good;
    double ret;
    double op;
    if (handler == 0) op = SND_ASYNC;
    if (handler == 1) op = SND_LOOP;
    if (handler == 2) op = SND_PURGE;
    LPCTSTR file;
    file = LPCTSTR(file_old);
    good = PlaySound(file,NULL,op);
    ret = double(good);
    return ret;
}

I have included in the linker the winmib or whatever, but I don't know what's going on.

Any help is greatly appreciated!

null
  • 548
  • 2
  • 6
  • 17

2 Answers2

0

If you get the default beep then it is most likely that the sound file can't be found. PlaySound searches for the sound file in the current directory, then the windows directory, then the system directory and finally the PATH.

jaket
  • 9,140
  • 2
  • 25
  • 44
0

The documentation includes the following:

Three flags in fdwSound (SND_ALIAS, SND_FILENAME, and SND_RESOURCE) determine whether the name is interpreted as an alias for a system event, a file name, or a resource identifier. If none of these flags are specified, PlaySound searches the registry or the WIN.INI file for an association with the specified sound name.

So if you want to play a file on disk, you have to include the SND_FILENAME flag, for example:

if (handler == 0) op = SND_ASYNC | SND_FILENAME;
if (handler == 1) op = SND_LOOP | SND_ASYNC | SND_FILENAME;

Something else, this line is probably wrong:

file = LPCTSTR(file_old);

If you don't compile with Unicode, file_old is compatible with file so you don't need to cast. If you compile with Unicode, a typecast is not sufficient.

wimh
  • 15,072
  • 6
  • 47
  • 98
  • I tried that, but same thing occurs. I'm not sure about the unicode, but I'm using VC++ 2010 and it tells me it's wrong if I don't cast. – null Oct 18 '14 at 22:57
  • see [here](http://stackoverflow.com/a/332086/33499) why typecasting can be dangerous. You should try to avoid it if possible. You should use [MultiByteToWideChar](http://msdn.microsoft.com/en-us/library/dd319072%28v=vs.85%29.aspx) to convert the string. But it is probably easier if you use `PlaySoundA` instead, that should avoid the typecast. – wimh Oct 19 '14 at 11:03
  • I tried multibyte to wide char and it says its wrong... Also, what is PlaySoundA? (I am a C++ newb.) – null Oct 20 '14 at 23:20
  • PlaySoundA is the ansi version of the PlaySound function. If you don't add the A, you will use PlaySoundW automatically because of your compiler settings. See [this](http://msdn.microsoft.com/en-us/library/dd374089.aspx) – wimh Oct 21 '14 at 07:48
  • and MultiByteToWideChar is not easy to use. It is a separate topic, but you first have to call it to determine how big the result will be, then you have to allocate the memory and call it again. At the end you have to free the memory. So I can understand it will not work immediately for you. – wimh Oct 21 '14 at 07:51