C# using winmm.dll audio recording

Thread Starter

kevinroseit

Joined Nov 7, 2011
1
Hello, I am trying to create a software that records audio information from a microphone. I am trying to do this using code in the winmm.dll file.
I can import this code fine from the DLL.
Rich (BB code):
[DllImport("winmm.dll")]
privatestaticexternbool PlaySound(string filename, int module, int flags);
However I cannot import this code
Rich (BB code):
[DllImport("winmm.dll")]
private static extern MMRESULT waveInOpen(LPHWAVEIN phwi, UINT uDeviceID, LPCWAVEFORMATEX pwfx, DWORD_PTR dwCallback, DWORD_PTR dwCallbackInstance, DWORD fdwOpen);
The reason being waveInOpen is of type MMRESULT. I was wondering if anyone knew how I could import the MMRESULT. If I have to make it, what is actually in the MMRESULT type?

I have looked into other ways of going about programming my project and this seems like the best way, I would choose for what I want.

Also I could use CORE Audio API's. The only problem I have found is they're verry low level programming so i would have to use C/C++. I need to use C# for this project but if anyone could give some insight to how i would implement header files in C# it would be greatly appreciated!
 

debrasteed

Joined Feb 10, 2021
1
Well, thanks for sharing one of my friends developing an application in C# in which I am trying to record sound using the winmm.dll. A few days earlier I was able to record audio, but now the audio file doesn't get generated. Also, no errors are produced. I debugged the program but couldn't find where I am going wrong.

Here is the code:

Code:
bool rec_strt = false;
    static int incre = 1;
    //string save_path = "E:\\mic"+incre+".wav";
    string save_path = Path.Combine(Environment.CurrentDirectory, @"Sent_files\", incre + ".wav");

    [DllImport("winmm.dll", EntryPoint = "mciSendStringA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
    private static extern int record(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
    private void p_audio_Click(object sender, EventArgs e)
    {
        if (rec_strt == false)
        {

            try
            {//getting both  i.e values of oo and op 0 ,which meanse no error
                int oo = record("open new Type waveaudio Alias recsound", "", 0, 0);
                int op = record("record recsound", "", 0, 0);
                string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
                if (op != 0)
                {
                    MessageBox.Show("Please attach the recording device first...", "Error message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    toolTip1.SetToolTip(p_audio, "Click to stop recording");
                    p_audio.BackgroundImage = WindowsFormsApplication1.Properties.Resources.voice_recording;
                    timer1.Enabled = true;
                    timer1.Start();
                  //  toolTip2.Show(timer1.ToString(), p_audio);
                    rec_strt = true;
                }
            }
            catch (Exception er)
            {
                MessageBox.Show("Error occured: " + er.Message, "Error message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        else
        {
            toolTip1.SetToolTip(p_audio, "Click to start recording");
            p_audio.BackgroundImage = WindowsFormsApplication1.Properties.Resources.voice;
            MessageBox.Show("Do you want to send this message to " + l_cnm.Text, "Confirmation message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            timer1.Stop();
            timer1.Enabled = false;
            toolTip2.Hide(p_audio);
            try
            {
                record("save recsound" + Path.Combine(Environment.CurrentDirectory, @"Sent_files\", incre + ".wav"), "", 0, 0);
                record("close recsound", "", 0, 0);
                try
                {
                    NameValueCollection nvc = new NameValueCollection();
                    nvc.Add("sender", my_id);
                    nvc.Add("receiver", fr_id);
                    nvc.Add("file", "Upload");
                    string contype = GetContentType(Path.GetExtension(save_path));
                    HttpUploadFile("http://chat.bobl.us/msgfile.php", save_path, "file", contype, nvc);
                    string fname = save_path.Substring(save_path.LastIndexOf("\\") + 1);
                    i1 = save_path;
                    add_label(true, "aud_" + fr_id + "_" + fname, DateTime.Now);
                    //p_cpic.Image = Image.FromFile(i);
                }
                catch (Exception ex)
                { }
            }
            catch (Exception er)
            {
                MessageBox.Show("Error occured: ", "Error message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            rec_strt = false;
            incre++;
        }
    }
 

ApacheKid

Joined Jan 12, 2015
418
Hello, I am trying to create a software that records audio information from a microphone. I am trying to do this using code in the winmm.dll file.
I can import this code fine from the DLL.
Rich (BB code):
[DllImport("winmm.dll")]
privatestaticexternbool PlaySound(string filename, int module, int flags);
However I cannot import this code
Rich (BB code):
[DllImport("winmm.dll")]
private static extern MMRESULT waveInOpen(LPHWAVEIN phwi, UINT uDeviceID, LPCWAVEFORMATEX pwfx, DWORD_PTR dwCallback, DWORD_PTR dwCallbackInstance, DWORD fdwOpen);
The reason being waveInOpen is of type MMRESULT. I was wondering if anyone knew how I could import the MMRESULT. If I have to make it, what is actually in the MMRESULT type?

I have looked into other ways of going about programming my project and this seems like the best way, I would choose for what I want.

Also I could use CORE Audio API's. The only problem I have found is they're verry low level programming so i would have to use C/C++. I need to use C# for this project but if anyone could give some insight to how i would implement header files in C# it would be greatly appreciated!
I can help with anything to do with C#, if the code is only ever going to run on a .Net platform then avoid stepping down to C or C++ at all cost if you can and stick with .Net. The interop abilities in .Net for C and C++ are extremely good, very well designed and far better than what Java offers.

The signature for those interop calls are visible here, looks like you can remove MMRESULT, the actual type is int.

https://stackoverflow.com/questions/7105806/c-sharp-audio-recording-using-winmm-dll-native-code
 
Last edited:
Top