Using PlaySound() from within a DLL

R

rcattral

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?
 
D

David Lowndes

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.

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
 
R

rcattral

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
 
G

Guest

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
 
D

David Lowndes

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top