老外的网搜来的!!!!
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace SmartDeviceProject6
{
public partial class Form1 : Form
{
[DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
internal static extern int DeviceIoControlCE(int hDevice, int dwIoControlCode, byte[] lpInBuffer, int nInBufferSize, byte[] lpOutBuffer, int nOutBufferSize, ref int lpBytesReturned, IntPtr lpOverlapped);
[DllImport("coredll", SetLastError = true)]
private static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
private static IntPtr _pwmFile = CreateFile("PWM1:", 0x40000000, 0, IntPtr.Zero, 3, 0, IntPtr.Zero);
public Form1()
{
InitializeComponent();
}
public static void Beep(uint Frequency, int DurationMS)
{
uint frequency = Frequency;
byte[] buffer = new byte[4];
int accessType = 2;
buffer[0] = (byte)(frequency & 0xff);
frequency = frequency >> 8;
buffer[1] = (byte)(frequency & 0xff);
frequency = frequency >> 8;
buffer[2] = (byte)(frequency & 0xff);
frequency = frequency >> 8;
buffer[3] = (byte)(frequency & 0xff);
DeviceIoControlCE((int)_pwmFile, accessType, buffer, 4, buffer, 0, ref accessType, IntPtr.Zero);
// var t = new System.Threading.Timer(TurnOff, null, DurationMS, System.Threading.Timeout.Infinite);
}
private static void TurnOff(Object obj)
{
byte[] buffer = new byte[4];
int accessType = 1;
DeviceIoControlCE((int)_pwmFile, accessType, buffer, 4, buffer, 0, ref accessType, IntPtr.Zero);
}
private void button1_Click(object sender, EventArgs e)
{
Beep(200, 20000);
}
}
}