这是VB调用API的代码,运行正常,可以让PC喇叭发出声音:
Private Declare Function beep Lib "kernel32" Alias "Beep" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
Private Sub CommandTest_Click()
For i = 50 To 2000 Step 20
beep i, 10
Next
End Sub
这是我改写的C#的代码:
[DllImport("kernel32.dll", EntryPoint="Beep")]
public static extern long beep(ref long dwFreq, ref long dwDuration);
private void buttonTest_Click(object sender, System.EventArgs e)
{
long j=10;
for (long i=50;i <= 2000 ;i+=20) beep(ref i, ref j);
}
结果是编译没有问题,运行也没有错误,但是不发出声音,大家帮我看看什么地方不对~~~
---------------------------------------------------------------
[DllImport("kernel32.dll")]
private static extern int Beep(int dwFreq ,int dwDuration) ;
private void buttonTest_Click(object sender, System.EventArgs e)
{
int j=10;
for (int i=50;i <= 2000 ;i+=20) Beep(i,j);
}
跟时间长短没有关系,按照breezem(夜空幽灵)这样写就好了~~~
---------------------------------------------------------------
Parameters
dwFreq
[in] Frequency of the sound, in hertz. This parameter must be in the range 37 through 32,767 (0x25 through 0x7FFF).
Windows 95/98/Me: The Beep function ignores this parameter.
dwDuration
[in] Duration of the sound, in milliseconds.
Windows 95/98/Me: The Beep function ignores this parameter.