using System;
using System.Runtime.InteropServices;
using System.Text;
class BeepProgram {
[DllImport("kernel32.dll", SetLastError = true)]
private extern static bool Beep(uint dwFreq, uint dwDuration);
[DllImport("kernel32.dll")]
static extern uint FormatMessage(
uint dwFlags, IntPtr lpSource,
uint dwMessageId, uint dwLanguageId,
StringBuilder lpBuffer, int nSize,
IntPtr Arguments);
private const uint FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
private static void Main() {
bool ret = Beep(0x8000, 500);
if (ret == false) {
// エラーが発生
int errCode = Marshal.GetLastWin32Error();
Console.WriteLine("Win32エラー・コード:" +
String.Format("{0:X8}", errCode));
StringBuilder message = new StringBuilder(255);
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM,
IntPtr.Zero,
(uint)errCode,
0,
message,
message.Capacity,
IntPtr.Zero);
Console.WriteLine("Win32エラー・メッセージ:" +
message.ToString());
// 出力結果:
// Win32エラー・メッセージ:パラメータが間違っています。
}
}
} |
Imports System.Runtime.InteropServices
Imports System.Text
Module BeepProgram
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Function Beep _
(ByVal dwFreq As Integer, ByVal dwDuration As Integer) As Boolean
End Function
<DllImport("Kernel32.dll")> _
Public Function FormatMessage( _
ByVal dwFlags As Integer, ByRef lpSource As IntPtr, _
ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, _
ByVal lpBuffer As StringBuilder, ByVal nSize As Integer, _
ByRef Arguments As IntPtr) As Integer
End Function
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Sub Main()
Dim ret As Boolean = Beep(&H8000, 500)
If ret = False Then
' エラーが発生
Dim errCode As Integer = Marshal.GetLastWin32Error()
Console.WriteLine("Win32エラー・コード:" & _
String.Format("{0:X8}", errCode))
Dim message As New StringBuilder(255)
FormatMessage( _
FORMAT_MESSAGE_FROM_SYSTEM, _
IntPtr.Zero, _
errCode, _
0, _
message, _
message.Capacity, _
IntPtr.Zero)
Console.WriteLine("Win32エラー・メッセージ:" & _
message.ToString())
' 出力結果:
' Win32エラー・メッセージ:パラメータが間違っています。
End If
End Sub
End Module |
|