creightonsmith Posted July 18, 2003 Posted July 18, 2003 I am using the winmm.dll to do some simple sound recording in vb.net. I am able to get the DLL to work like a charm by sending a string in quotes, but after many different attempts am having no luck when I pass a string variable as described below. Per the MSDN instructions for calling Win32 API functions from .net I have done the following: First I declare a wrapper class Imports System.Runtime.InteropServices Public Class DLLWrapper ' Declare API to work with Windows MCI Command Declare Auto Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal _ lpstrCommand As String, ByVal lpstrReturnString As String, _ ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long End Class Then I create the class and call the function using the described parameters. If I do this I have no problems: clsDLLWrapper.mciSendString("save libedit C:\Test.wav", 0&, 0, 0) When I pass the string variable like this it doesn't work: Dim strVariable As String strVariable = "save libedit C:\Test.wav" clsDLLWrapper.mciSendString(strVariable, 0&, 0, 0) I have tried a few things including changing the Charset of the function to ANSI, AUTO etc. Any help would be much appreciated as I don't want to have to fix my file path for file recording. Thanks! Quote
Leaders dynamic_sysop Posted July 18, 2003 Leaders Posted July 18, 2003 try this , you need more than that code to record / save a wav . Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer '/// use Integer's not Longs Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strVariable As String() = {"open new type waveaudio alias capture", "set capture bitspersample 8" _ & "set capture samplespersec 11025", "record capture"} mciSendString(strVariable(0), vbNullString, 0, 0) '/// create a new wav. mciSendString(strVariable(1), 0, 0, 0) '///set the bitrate. mciSendString(strVariable(2), 0, 0, 0) '///samples per second. mciSendString(strVariable(3), 0, 0, 0) '///lets record. End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click mciSendString("stop capture", 0, 0, 0) '/// stop recording. mciSendString("save capture c:\test.wav", 0, 0, 0) '/// save our new Wav. End Sub Quote
*Gurus* Derek Stone Posted July 18, 2003 *Gurus* Posted July 18, 2003 Private Declare Ansi Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" ( _ ByVal lpstrCommand As String, _ ByVal lpstrReturnString As StringBuilder, _ ByVal uReturnLength As Integer, _ ByVal hwndCallback As Integer) As Integer Dim sReturn As StringBuilder = New StringBuilder(128) Dim sInput As String = "play C:\Test.wav" Dim iReturn As Integer = mciSendString(sInput, sReturn, sReturn.Capacity, 0) Quote Posting Guidelines
creightonsmith Posted July 18, 2003 Author Posted July 18, 2003 Thanks! Thanks, with your help and some more investigation I discovered that there were a couple of issues with my code above. The first is that in .Net the Longs in the DLL Declare should be changed to Integer when calling from vb.net. The other interesting thing, and I don't completely understand it, but apparently to get an accurate return string, you need to pass a fixed length string, which was accomplished using the stringbuilder code submitted above. The other thing that appears to be important is that when passing a string to an asyncronous DLL, it is possible that when the DLL attempts to use the string represeting the file path to save the file that the string variable could already be collected by the garbage collector. The following addition to Derek's code declaration prevents this problem: Dim sInput As New String = "play C:\Test.wav" Considering the limited support for recording in the .Net framework, I hope these findings are helpful to others. Thanks to Derek and dynamic_sysop. Quote
Leaders dynamic_sysop Posted July 18, 2003 Leaders Posted July 18, 2003 StringBuilder is a great thing and you can use many Win32 api's with it ( which will return blank with normal strings ) Quote
Recommended Posts