Re: using PlaySound within a dll
- From: "MikeD" <nobody@xxxxxxxxxxx>
- Date: Thu, 4 Aug 2005 18:55:27 -0400
"stacy" <stacyirish@xxxxxxxxxxxxx> wrote in message
news:1123175525.965880.59030@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>I have compiled and run and still no sound. Maybe someone can recreate
> what I am trying to do to find my mistake...
>
> I have an exe with a form and a button. The code behind the button is:
>
> Private Sub Command1_Click()
> Dim X As New dllSound.Class1
> X.MySound
> X.MyMessage
> End Sub
>
> I have a dll with a module, class and resource file.
>
> The module code is:
>
> Declare Function sndPlaySound Lib "WINMM.DLL" Alias "sndPlaySoundA" ( _
> ByVal lpszSoundName As String, _
> ByVal uFlags As Long) As Long
>
> Declare Function PlaySound Lib "WINMM.DLL" Alias "PlaySoundA" ( _
> ByVal lpszName As String, _
> ByVal hModule As Long, _
> ByVal dwFlags As Long) As Long
>
> Declare Function PlaySoundLong Lib "WINMM.DLL" Alias "PlaySoundA" ( _
> ByVal lpszName As Long, _
> ByVal hModule As Long, _
> ByVal dwFlags As Long) As Long
>
> Public Const SND_ASYNC = &H1
> Public Const SND_NODEFAULT = &H2
> Public Const SND_MEMORY = &H4
> Public Const SND_SYNC = &H0
> Public Const SND_RESOURCE = &H40004
>
> The class code is:
>
> Public Sub MySound()
>
> Dim X As Long
> Dim DataArray() As Byte
> DataArray = LoadResData(101, "WAVE")
> 'X = PlaySound(DataArray(0), App.hInstance, SND_MEMORY Or
> SND_NODEFAULT Or SND_ASYNC)
> X = sndPlaySound(DataArray(0), SND_ASYNC Or SND_NODEFAULT Or
> SND_MEMORY)
> Erase DataArray
>
> MsgBox "App.hInstance within dll " & App.hInstance, vbOKOnly
> MsgBox "X return value " & X, vbOKOnly
>
> End Sub
PlaySound has the capability to play a .wav file directly from the
resource....sndPlaySound does not. When using PlaySound, you should not be
loading the resource and assigning it to a byte array. You would call
PlaySound just like this (given your example code using a resource ID of
101):
X = PlaySound(101, App.hInstance, SND_NODEFAULT Or SND_ASYNC Or
SND_RESOURCE)
That's it. That's all you need to do.
The SND_ALIAS, SND_FILENAME, and SND_MEMORY flags should not be specified.
IOW, those 3 flags and SND_RESOURCE are pretty much mutually exclusive of
one another. If you specify more than 1, you'll confuse Windows as to what
source (a .wav file, a named sound event in the Registry, from memory, or a
resource) to use. I don't know exactly what consequences there'd be. I
would assume Windows prioritizes each. In any case, it's not a good idea to
combine any of these 4 flags.
Second, your declaration for PlaySound is incorrect if you want to use it to
play a sound resource. It should be declared like this:
Private Declare Function PlaySound Lib "WINMM.DLL" Alias "PlaySoundA" (ByVal
lpszName As Any, ByVal hModule As Long, ByVal dwFlags As Long) As Long
This is because you need to be able to pass a Long for lpszName.
Alternatively, you could use your PlaySoundLong type-safe declaration
instead.
Now, there's the issue of the resource itself. The resource type must be
"WAVE" (although I don't think the case actually matters). From your code,
it looks like this is probably correct, but I just wanted to make sure that
was clear.
So, basically the only thing I really see wrong with your attempt at using
PlaySound is how you're calling it and the declaration of it. As I said, if
you don't want to change the declaration, then use your PlaySoundLong alias.
Compile your DLL. It's only the DLL that needs compiled. You just need to
make sure than any app you're running in the IDE is using the compiled DLL
and not the ActiveX DLL project (for example, as part of a project group).
I have a number of ActiveX DLLs which contain their own sound resources and
that one, simple call to PlaySound works fine (and just to reitterate, as
long as your using the compiled DLL).
--
Mike
Microsoft MVP Visual Basic
.
- Follow-Ups:
- Re: using PlaySound within a dll
- From: Veign
- Re: using PlaySound within a dll
- References:
- using PlaySound within a dll
- From: stacy
- Re: using PlaySound within a dll
- From: Ken Halter
- Re: using PlaySound within a dll
- From: stacy
- Re: using PlaySound within a dll
- From: Bob Butler
- Re: using PlaySound within a dll
- From: stacy
- Re: using PlaySound within a dll
- From: MikeD
- Re: using PlaySound within a dll
- From: stacy
- using PlaySound within a dll
- Prev by Date: Re: using PlaySound within a dll
- Next by Date: Re: using PlaySound within a dll
- Previous by thread: Re: using PlaySound within a dll
- Next by thread: Re: using PlaySound within a dll
- Index(es):