C#中调用WIN32 API实现对光驱的操作

Q:C#中调用WIN32 API实现对光驱的操作

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#操作光驱";

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="打开光驱";

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="关闭光驱";

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()); ;

}

}