Quote Originally Posted by jmsrickland View Post
Code:
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" _
 (ByRef lpszName As Any, _
  ByVal hModule As Long, _
  ByVal dwFlags As Long) As Long

Private Const SND_ASYNC = &H1
Private Const SND_MEMORY = &H4

Dim WavSound() As Byte

Private Sub Command1_Click()
 WavSound = LoadResData(101, "custom")
 MsgBox "WavSound Loaded"
End Sub

Private Sub Command2_Click()
 On Error Resume Next
 PlaySound WavSound(0), 0, SND_MEMORY Or SND_ASYNC
End Sub

Public Sub Command3_Click()
 PlaySound "", ByVal 0&, SND_MEMORY
End Sub

Private Sub Form_Unload(Cancel As Integer)
 Command3_Click
 End
End Sub


This works for some reason, but there are a couple problems I see, things that will need to be changed to be technically correct.

1. Your PlaySound declare statement uses ByVal for hModule, and later when calling it to stop the playing you use ByVal 0&. This is redundant, as ByVal is only needed when calling if you have ByRef in the declaration.

2. You use pass the number zero as 0& instead of 0. This is also redundant. In your declaration, you use ByVal hModule As Long. This means any value you supply to it will automatically be converted to a long (just like with the CLng function) prior to being passed to the DLL file for processing. So manually setting it as a zero-valued long (by saying 0&) instead of a zero-valued integer (by saying 0) is redundant.

3. In your line to stop the playing sound your first argument you pass is "" (an empty string), which is actually the pointer to an empty string because it was declared ByRef in the declaration. But you aren't supposed to pass a pointer to an empty string. In order to stop playing (according to Microsoft's official documentation) you are supposed to pass a pointer to to a NULL string, which is to say a 1-character string who's ascii value is 0x00 (the NULL ascii character). So instead of typing "" for the first argument, you should have put vbNullchar (the VB constant for a string consisting of a a single ascii NULL character), or just to make sure you are passing just 1 byte to the function (since all strings in VB are unicode, 2bytes per character) is to have passed this instead StrConv(vbNullchar,vbFromUnicode). This command will convert the 1 unicode character long string which is a NULL character (having a 2byte unicode value of 0x0000) into a true ascii (1 byte per character) string consisting of 1 ascii character long string which is a NULL character (having a 1byte ascii value of 0x00). It would output a proper single character (1 byte) ascii string of a NULL character and would have passed the pointer to the string, to the DLL file.

You put:
Code:
Public Sub Command3_Click()
 PlaySound "", ByVal 0&, SND_MEMORY
End Sub
You should have put:
Code:
Public Sub Command3_Click()
 PlaySound vbNullchar, 0, SND_MEMORY
End Sub
or even better:
Code:
Public Sub Command3_Click()
 PlaySound StrConv(vbNullchar,vbFromUnicode), 0, SND_MEMORY
End Sub
If I'm mistaken in this, please correct me, so I can see where my understanding is wrong.