调用系统API

来源:互联网 发布:淘宝游戏代充 编辑:程序博客网 时间:2024/05/18 07:59

using System.Runtime.InteropServices;

public class useAPI
{
    public useAPI()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    /// <summary>
    /// 打开和关闭CD托盘.
    /// </summary>
    [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
    public static extern int mciSendString(string lpstrCommand, string lpstrReturnstring, int uReturnLength, int hwndCallback);

    /// <summary>
    /// 显示和隐藏鼠标指针.
    /// </summary>
    [DllImport("user32.dll", EntryPoint = "ShowCursor", CharSet = CharSet.Auto)]
    public static extern int ShowCursor(int bShow);

    /// <summary>
    /// 清空回收站.
    /// </summary>
    [DllImport("shell32.dll", EntryPoint = "SHEmptyRecycleBin", CharSet = CharSet.Auto)]
    public static extern long SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, long dwFlags);

    /// <summary>
    /// 打开浏览器
    /// </summary>
    [DllImport("shell32.dll", EntryPoint = "ShellExecute", CharSet = CharSet.Auto)]
    public static extern int ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);

    /// <summary>
    /// 最大化窗口,最小化窗口,正常大小窗口;
    /// </summary>
    [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
    public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
}

    调用例:

    打开CD托盘

    public static string strReturn = "h";
    protected void Button1_Click(object sender, EventArgs e)
    {
        useAPI.mciSendString("set CDAudio door open", strReturn, 127, 0);
    }

     关闭CD托盘
    protected void Button2_Click(object sender, EventArgs e)
    {
        useAPI.mciSendString("set CDAudio door closed", strReturn, 127, 0);
    }

原创粉丝点击