469,580 Members | 1,992 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,580 developers. It's quick & easy.

Using PlaySound() from within a DLL

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?
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 17 '05 #1
4 4323
>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
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #2
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

Nov 17 '05 #3
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

"David Lowndes" wrote:
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
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq

Nov 17 '05 #4
>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
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by bballmitch | last post: by
3 posts views Thread by Harry J. Smith | last post: by
2 posts views Thread by Joe Thompson | last post: by
10 posts views Thread by Ot | last post: by
9 posts views Thread by Andy | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.