リスト5:現在の日時を表示するVisual Basicのプログラム


Option Explicit

'↓入出力用の定数とグローバル変数
Public Const STD_INPUT_HANDLE = -10&
Public Const STD_OUTPUT_HANDLE = -11&
Dim stdin As Long, stdout As Long

'↓API関数
Declare Function GetStdHandle _
  Lib "kernel32" (ByVal nStdHandle As Long) As Long
Declare Function WriteFile _
  Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As String, _
  ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, _
  ByVal lpOverlapped As Long) As Long

'↓文字列出力プロシージャ
Sub PutStr(s As String)
    Dim sz As Long, n As Long
    Dim rv
    
    sz = LenB(StrConv(s, vbFromUnicode))
    If sz > 0 Then
        rv = WriteFile(stdout, s, sz, n, 0)
    End If
End Sub

'↓処理の本体ここから
Sub Main()
  Dim dtDate As Date
  Dim dtTime As Date

  '↓グローバル変数に標準入出力のハンドルを設定
  stdin = GetStdHandle(STD_INPUT_HANDLE)
  stdout = GetStdHandle(STD_OUTPUT_HANDLE)

  dtDate = Date
  dtTime = Time
  PutStr ("dtDate, dtTime" + vbCrLf + vbCrLf)
End Sub
'↑処理の本体ここまで


本文へ戻る