[Win32utils-devel] win32-api callback causes ruby to application error (crash).

nao16t at gmail.com nao16t at gmail.com
Fri Jan 4 09:41:39 EST 2008

# I originally intended to post this question here.
# But it took some time for subscription authorization process.
# So I have posted the same question to the forum when I was waiting
# for the subscription confirmation notice.
# It seems to me the forum is not so active, let me drop here too.


The following WinSNMP trap receive program 'win_snmp.rb' causes ruby
interpreter to application error when the callback is invoked just
after sending a trap by 'trapv2.rb'.

This ruby program is written based on the WinSNMP C/C++ sample program
at http://www.winsnmp.com/samples/c/cm_traps.c which works OK.
So 'win_snmp.rb' should be OK, I think.

Would someone please give me some advice ?

My environment:

Windows XP Pro SP2 with MS SNMP Agent and Trap Service.
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
win32-api (1.0.5)
snmp (1.0.1)

-----------start win_snmp.rb-------------------
require 'win32/api'
include Win32

SnmpStartup = API.new('SnmpStartup', 'PPPPP', 'L', 'Wsnmp32')
SnmpGetLastError = API.new('SnmpGetLastError', 'L', 'L', 'Wsnmp32')
SnmpCreateSession = API.new('SnmpCreateSession', 'LLKP', 'L', 'Wsnmp32')
SnmpRegister = API.new('SnmpRegister', 'LLLLPL', 'L', 'Wsnmp32')

nMajorVersion = 0.chr * 4
MinorVersion = 0.chr * 4
nTranslateMode = 0.chr * 4
nRetransmitMode = 0.chr * 4
nLevel = 0.chr * 4
result = SnmpStartup.call(nMajorVersion, MinorVersion, nLevel,
nTranslateMode, nRetransmitMode)
puts "SnmpStartup: #{result}"
printf("nMajorVersion: %s MinorVersion: %s nLevel: %s nTranslateMode: %s
nRetransmitMode: %s\n",
        nMajorVersion.unpack("L*"), MinorVersion.unpack("L*"), nLevel.unpack
        nTranslateMode.unpack("L*"), nRetransmitMode.unpack("L*"))
puts "SnmpGetLastError: #{SnmpGetLastError.call(nil)}"

SNMPAPI_CALLBACK = API::Callback.new('LLLLPP', 'L'){ |hSession, hWnd, wMsg,
wParam, lParam, lpClientData|
  puts "Trap Recieved!"

session = SnmpCreateSession.call(nil, 0, SNMPAPI_CALLBACK, nil)
puts "SnmpCreateSession: #{session}"
puts "SnmpGetLastError: #{SnmpGetLastError.call(session)}"

result = SnmpRegister.call(session, nil, nil, nil, nil, SNMPAPI_ON)
puts "SnmpRegister: #{result}"
puts "SnmpGetLastError: #{SnmpGetLastError.call(session)}"


-----------end win_snmp.rb-------------------

Console output of the win_snmp.rb:
SnmpStartup: 1
nMajorVersion: 2 MinorVersion: 0 nLevel: 2 nTranslateMode: 1
nRetransmitMode: 1
SnmpGetLastError: 1
SnmpCreateSession: 1
SnmpGetLastError: 1
SnmpRegister: 1
SnmpGetLastError: 1

The following code is to send a SNMP trap.
-----------start trapv2.rb-------------------
require 'rubygems'
require 'snmp'

SNMP::Manager.open(:TrapPort => 162, :Version => :SNMPv2c) do |snmp|
  1.times { |i|
    p i
    snmp.trap_v2(12345, "", ["", ""])
-----------end trapv2.rb-------------------


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/win32utils-devel/attachments/20080104/372abf41/attachment.html 

More information about the win32utils-devel mailing list