473,320 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 4486
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: bballmitch | last post by:
Why won't the following code work? char soundfile2 = "C:/Windows/Media/pinky and the brain.wav"; PlaySound(soundfile2,NULL,SND_FILENAME|SND_SYNC|SND_ASYNC); getchar(); i put libwinmm.a in...
5
by: MLH | last post by:
I have a line of code that works when called from a procedure in Access 2.0 form... PlaySound("C:\cr\help\Help0018.wav", 0) I imported what I thought was needed into A97. However, running it...
3
by: Harry J. Smith | last post by:
I added some sounds to my application, but the example in the msdn Library did not work. It had: public static extern bool PlaySound( string szSound, IntPtr hMod, PlaySoundFlags flags ); The...
2
by: Joe Thompson | last post by:
Hi, I am trying to use PlaySound in a VC++.net Windows app (VS 2003). I can use it to play a file but now I want to play it from a resource. I have two questions: How do I add a wav file to...
10
by: Ot | last post by:
I found information on PlaySound. I implemented it in my program and it works just dandy. The only little problem is that I have to package the ..wav files and send them along since PlaySound...
9
by: Andy | last post by:
Hi, I have an application that has several forms. Each form acts as a monitor of a gateway system. If the gateway appears to be dead, the application should play a .wav file (not resource)...
3
by: Jared | last post by:
I'm using the first code sample below to play WAV files stored as embedded resources. For some reason I *occasionally* get scratching and crackling. I'm using a couple WAVs that ship with...
0
by: poppy | last post by:
I tried to play a wav file with function Playsound() but it played only the first seconds of the song.I would like to play whole the song.What might de be wrong?Here is my code in case someone can...
0
by: Mike | last post by:
I am a novice writing a simple program in Visual C++ Express and am having an issue. I basically have several picture boxes of the same size on top of each other with only one visible at a time. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.