Has anybody been using the PlaySound() function to play WAV files from
within a DLL? Here is what I normally do.
Since upgrading to 7.1 NET after using VC 6.0 for quite some time, I
found that I had to add the following lines in order to use the
multimedia extensions.
In stdafx.h
#include <MMSystem.h>
#pragma comment(lib, "winmm.lib")
After adding these, my old code compiles and still works. I basically
import a WAV file as a resource and then play it with the following
line:
bool played = PlaySound(MAKEINTRESOURCE(IDR_WAVE1), NULL, SND_ASYNC |
SND_RESOURCE);
In standard applications this works fine, however when I put this code
into a function that is being exported from within a DLL it fails. As
a first possible solution I tried to add the Manage_state macro at
the top of the function, as follows:
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
bool played = PlaySound(MAKEINTRESOURCE(IDR_WAVE1), NULL, SND_ASYNC |
SND_RESOURCE);
..
}
No luck...
Suggestions anyone?
David Lowndes - 10 Jun 2004 22:56 GMT
>After adding these, my old code compiles and still works. I basically
>import a WAV file as a resource and then play it with the following
[quoted text clipped - 5 lines]
>In standard applications this works fine, however when I put this code
>into a function that is being exported from within a DLL it fails.
Where is your WAV resource - also in the DLL?
The second parameter is the module handle. It's scantily documented
as:
"hmod
Handle to the executable file that contains the resource to be loaded.
This parameter must be NULL unless SND_RESOURCE is specified in
fdwSound.
"
... which I assume really means that it needs to be the DLL's module
handle in your situation.
Dave

Signature
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nelson Hsieh - 03 Sep 2004 19:15 GMT
Hi David
Is this only for Windows CE? I try to use the playsound function on Windows
XPP with VB6 and got an error of invaild use of NULL
I use function LoadResource to load the WAV sound that I store on a .RC file.
thanks
nelly
> >After adding these, my old code compiles and still works. I basically
> >import a WAV file as a resource and then play it with the following
[quoted text clipped - 21 lines]
>
> Dave
David Lowndes - 03 Sep 2004 22:33 GMT
>Is this only for Windows CE?
No.
>I try to use the playsound function on Windows
>XPP with VB6 and got an error of invaild use of NULL
I'm no VB expert, but wouldn't that be vbNull (or just 0) in VB?
Dave

Signature
MVP VC++ FAQ: http://www.mvps.org/vcfaq
rcattral - 12 Jul 2004 00:05 GMT
Dave,
Thank you for your help. This is a perfect example of what happens
when I use a function for years and then just assume that I know how
it works. When I popped AfxGetInstanceHandle() into the second
parameter instead of the NULL, it worked fine.
In some cases it might be necessary to use the ::GetModuleHandle(...),
but in my current environment it isn't necessary.
Thanks again,
Rob