Help on winmm.dll exception: "A StringBuilder buffer has been overflowed by unmanaged code"

 Sorry if the format is messed up; I normally don't post here. This message html editor is really messed up, sorry.

I click my eject button on my program, and I get an exception after it opens the CD Rom drive.

Exception:

Warning: A StringBuilder buffer has been overflowed by unmanaged code.  The process may become unstable.  Insufficient capacity allocated to the StringBuilder before marshaling it.

Why is this


Thanks,

Phil

 

private void button1_Click(object sender, EventArgs e)

{

OpenCD();

}

[DllImport("winmm.dll")]

static extern Int32 mciSendString(String command,

StringBuilder buffer, Int32 bufferSize, IntPtr hwndCallback);

public void OpenCD()

{

IntPtr ptr = IntPtr.Zero;

StringBuilder returnstring = new StringBuilder();

mciSendString("set CDAudio door open", returnstring, 127, IntPtr.Zero);

}



Answer this question

Help on winmm.dll exception: "A StringBuilder buffer has been overflowed by unmanaged code"

  • wowpeter

    dxfoo -

    If you're still around, please remember to mark questions as answered!  It helps pull search results back into VS2005.

    Thanks!
    Karen



  • pbiffar

    That did it, thanks.
  • sqldba123

    Your buffer is empty as it is just a newly created StringBuilder. You are telling the API call your buffer is 127 bytes.

    The buffer will overflow.

    Add this line before calling mciSendString()

    returnstring.Length = 127;

    You might change the call to mciSendString to be

    mciSendString("set CDAudio door open", returnstring, returnstring.Length, IntPtr.Zero);



  • Help on winmm.dll exception: "A StringBuilder buffer has been overflowed by unmanaged code"