Ok, now the EventLog#close method is dying. It’s returning “the handle
is invalid”.
What am I doing wrong?
require "Win32API"
module Win32
CloseEventLog = Win32API.new(“advapi32”,“CloseEventLog”,“P”,“I”)
FormatMessage =
Win32API.new(“kernel32”,“FormatMessage”,“LPLLPLP”,“L”)
GetLastError = Win32API.new(“kernel32”,“GetLastError”,[],“L”)
OpenEventLog = Win32API.new(“advapi32”,“OpenEventLog”,“PP”,“P”)
ERROR_SUCCESS = 0x00
Error formatting constants
FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100
FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200
FORMAT_MESSAGE_FROM_STRING = 0x00000400
FORMAT_MESSAGE_FROM_HMODULE = 0x00000800
FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000
FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000
FORMAT_MESSAGE_MAX_WIDTH_MASK = 0x000000FF
class Win32EventLogError < StandardError; end
class EventLog
def initialize(source,machine=0)
lpSource = [source].pack(“p”)
unless 0 == machine
lpMachine = [machine].pack("p")
end
begin
@hEventLog = OpenEventLog.call(machine,source)
rescue
raise Win32EventLogError, get_error
end
end
def close
if 0 == CloseEventLog.call(@hEventLog)
raise Win32EventLogError, get_error
end
end
def get_error(error_code=GetLastError.call)
msg = " " * 255
FormatMessage.call(
FORMAT_MESSAGE_FROM_SYSTEM +
FORMAT_MESSAGE_ARGUMENT_ARRAY,
'',
error_code,
0,
msg,
255,
''
)
msg.gsub!(/\000/, '')
msg.strip!
msg
end
end # EventLog
end # Win32
if $0 == FILE
include Win32
e = EventLog.new(“Application”)
e.close # Error
end