Problems passing string to Win32 API

creightonsmith

New member
Joined
Jul 18, 2003
Messages
3
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 doesnt 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 dont want to have to fix my file path for file recording. Thanks!
 
try this , you need more than that code to record / save a wav .
Code:
    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 Integers 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
 
Code:
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
Code:
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)
 
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 dont 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 Dereks 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.
 
Back
Top