Q: C # Calls Win32 API to perform operations on the optical drive
A:
Using system;
Using system. text;
Using system. Windows. forms;
Using system. drawing;
Using system. runtime. interopservices;
Class cdromcontroler: Form
{
[Dllimport ("winmm. dll", entrypoint = "mcisendstringa", charset = charset. ANSI)]
Public static extern int mcisendstring (string lpstrcommand, stringbuilder lpstrreturnstring, int ureturnlength, intptr hwndcallback );
Private button openbutton;
Private button closebutton;
Public cdromcontroler ()
{
This. autoscalebasesize = new size (5, 13 );
This. clientsize = new size (400,200 );
This. Text = "C # operating Optical Drive ";
This. openbutton = new button ();
Openbutton. Location = new point (50,100 );
Openbutton. size = new size (100,20 );
Openbutton. tabindex = 1;
Openbutton. Anchor = anchorstyles. Bottom | anchorstyles. Left;
Openbutton. Text = "enable Optical Drive ";
Openbutton. Click + = new eventhandler (this. openbutton_click );
This. Controls. Add (this. openbutton );
This. closebutton = new button ();
Closebutton. Location = new point (250,100 );
Closebutton. size = new size (100,20 );
Closebutton. tabindex = 2;
Closebutton. Anchor = anchorstyles. Bottom | anchorstyles. Right;
Closebutton. Text = "Disable the optical drive ";
Closebutton. Click + = new eventhandler (this. closebutton_click );
This. Controls. Add (this. closebutton );
}
Private void openbutton_click (Object sender, system. eventargs E)
{
Int ret = mcisendstring ("set cdaudio door open", null, 0, intptr. Zero );
}
Private void closebutton_click (Object sender, system. eventargs E)
{
Int ret = mcisendstring ("set cdaudio door closed", null, 0, intptr. Zero );
}
Public static void main ()
{
Application. Run (New cdromcontroler ());;
}
}