2009年4月1日水曜日

require 'dl'の続き

Ruby/DLをもっと使ってみた。

sample.rb
#!/usr/local/bin/ruby -Ku
# -*- encoding: utf-8 -*-

require 'dl/import'
require 'dl/struct'

module DSO
extend DL::Importable

typealias('BOOL', 'int')
typealias('WORD', 'unsigned short')
typealias('LPSYSTEMTIME', 'void*')
typealias('LPFILETIME', 'void*')

SYSTEMTIME = struct [
"WORD wYear",
"WORD wMonth",
"WORD wDayOfWeek",
"WORD wDay",
"WORD wHour",
"WORD wMinute",
"WORD wSecond",
"WORD wMilliseconds",
]

FILETIME = struct [
"DWORD dwLowDateTime",
"DWORD dwHighDateTime"
]

dlload 'Kernel32'
extern 'void GetSystemTime(LPSYSTEMTIME)'
extern 'void GetSystemTimeAsFileTime(LPFILETIME)'
extern 'void GetLocalTime(LPFILETIME)'
extern 'BOOL FileTimeToSystemTime(LPFILETIME, LPSYSTEMTIME)'
end

def print_system_time(st)
puts(
"#{st.wYear}/#{st.wMonth}/#{st.wDay}(#{st.wDayOfWeek}) " +
"#{st.wHour}:#{st.wMinute}:#{st.wSecond}.#{st.wMilliseconds}")
end

st = DSO::SYSTEMTIME.malloc
print_system_time(st)

ft = DSO::FILETIME.malloc
DSO.getSystemTimeAsFileTime(ft)
DSO.fileTimeToSystemTime(ft, st)
print_system_time(st)

DSO.getSystemTime(st)
print_system_time(st)
出力例
0/0/0(0) 0:0:0.0
2009/4/1(3) 11:8:44.406
2009/4/1(3) 11:8:44.406

0 件のコメント:

コメントを投稿