title: Record audio in c# using winmm.dll
body: <p>I am developing an application in C# in which I am trying to record sound using the winmm.dll. Few days earlier I was able to record audio, but now the audio file doesn't get generated. Also no errors is produced. I debugged the program but couldn't find where I am going wrong. </p>
  
  <p>Here is my code:</p>
  
  <pre><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++;
          }
      }
  </code></pre>