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

nao16t at gmail.com nao16t at gmail.com
Sun Jan 6 08:16:49 EST 2008


Dan-san,

Thank you for finding out my careless mistake.
Now the callback turned to be invoked.

But still Ruby interpreter crashes inside the callback.

Basically does the win32 callback run under the ruby main thread ?
Isn't the win32 callback called by creating a new ruby thread ?

If I call ruby "sleep()" at the bottom of code, the win32 callback is
blocked until the ruby sleep returns.

If I call win32 "Sleep()" at the bottom of code, ruby interpreter
crushes inside the callback.

How should the ruby mainthread wait for the asynchronous win32
callback ?

I tried several work-arounds, but so far no luck.
I commented out the lines what I did in the following code.

Best regards,

-nao

require 'win32/api'
include Win32

SNMPAPI_FAILURE = 0
SNMPAPI_SUCCESS = 1

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')
SnmpRecvMsg  = API.new('SnmpRecvMsg', 'LPPPP', 'L', 'Wsnmp32')
SnmpGetPduData = API.new('SnmpGetPduData', 'LPPPPP', 'L', 'Wsnmp32')
SnmpFreeVbl = API.new('SnmpFreeVbl','L', 'L', 'Wsnmp32')
SnmpFreePdu  = API.new('SnmpFreePdu', 'L', 'L', 'Wsnmp32')

Sleep = API.new('Sleep', 'L', 'V')

#th = Thread.new {
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
("L*"),
        nTranslateMode.unpack("L*"), nRetransmitMode.unpack("L*"))
puts "SnmpGetLastError: #{SnmpGetLastError.call(nil)}"

SNMPAPI_CALLBACK = API::Callback.new('LLLLLP', 'L'){ |hSession, hWnd, wMsg,
wParam, lParam, lpClientData|

#  Thread.new(hSession, hWnd, wMsg, wParam, lParam, lpClientData)
{|hSession, hWnd, wMsg, wParam, lParam, lpClientData|
  hPdu = 0.chr * 4
  lType = 0.chr * 4
  lReqId = 0.chr * 4
  lErr = 0.chr * 4
  lIdx = 0.chr * 4
  hVbl = 0.chr * 4

  puts "Trap Recieved, start callback!"

  state = SnmpRecvMsg.call(hSession, nil, nil, nil, hPdu)
  puts "SnmpRecvMsg: #{state}"

  state = SnmpGetPduData.call(hPdu.pack("L*"), lType, lReqId, lErr, lIdx,
hVbl)
  puts "SnmpGetPduData: #{state}"

  state = SnmpFreeVbl.call(hVbl.pack("L*"))
  puts "SnmpFreeVbl: #{state}"

  state = SnmpFreePdu.call(hPdu.pack("L*"))
  puts "SnmpFreePdu: #{state}"

  puts "Trap Recieved, end callback!"
#  }

  return(SNMPAPI_SUCCESS)
}

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

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

#th.join
#loop do
#  Thread.pass
#end

#
Sleep.call(60000)
#sleep(60)
#60.times {
#  sleep(1)
#}

2008/1/6, Daniel Berger <djberg96 at gmail.com>:
>
> Hi Nao,
>
> nao16t at gmail.com wrote:
> > # 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.
>
> That's fine. I monitor both.
>
> <snip>
>
> > SNMPAPI_CALLBACK = API::Callback.new('LLLLPP', 'L'){ |hSession, hWnd,
> > wMsg, wParam, lParam, lpClientData|
> >   puts "Trap Recieved!"
> > }
>
> I haven't tested it, but shouldn't the 5th parameter be 'L'. The docs
> say lParam is a 32 bit identifier.
>
> Regards,
>
> Dan
> _______________________________________________
> win32utils-devel mailing list
> win32utils-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/win32utils-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/win32utils-devel/attachments/20080106/946e3fbc/attachment.html 


More information about the win32utils-devel mailing list