Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Event handling in WSH/VBS

170 views
Skip to first unread message

Tor Bothner

unread,
Feb 15, 1999, 3:00:00 AM2/15/99
to
I'm trying to write a small litle VBscript that uses WinSock. VBS and
WinSock work fine, but I cannot get any event handlers to work.

My question is: Does event handling work the same way under WSH as under
IE4, by calling a procedure called Objectname_EventName? If so, why does
my little script, shown below not work? If not, is there any support for
event handling under WSH?

The litle VBS below is supposed to open a socket, make a connection to a
remote machine, send some data, and display the results sent back. It
works fine on VB6, but not under WSH.

Any help would be appreciated,
--tor

'****************************************************************
'Event handlers
Sub objWS_DataArrival (ByVal bytesTotal)
objWS.GetData strData
wstring.echo "DataArrival: '" & strData & "'"
End Sub

SUB objWS_Error(number, Description, Scode, Source, HelpFile,
HelpContext, CancelDisplay)
wstring.echo "error Event"
end sub

Sub objWS_connect
wstring.echo "Connect Event"
End Sub

Sub objWS_DataArrival (ByVal bytesTotal)
objWS.GetData strData
wstring.echo "DataArrival: '" & strData & "'"
End Sub

Set objWS = Wscript.CreateObject("MSWinsock.Winsock")
objWS.Connect Wscript.Arguments(0), Wscript.Arguments(1)
Wscript.echo "After Connect, state=" & objWS.state

While (objWS.state = 6)
wscript.echo "waiting, state=" & objWS.state
wend
wscript.echo "End of loop, state=" & objWS.state

objWS.SendData Wscript.Arguments(2)
wscript.echo "Sent, state=" & objWS.state

While (objWS.state = 7)
wscript.echo "Waiting, state=" & objWS.state
Wend

wscript.echo "Exiting, state=" & objWS.state

Bernhard Mandl

unread,
Feb 15, 1999, 3:00:00 AM2/15/99
to
You must use the 2nd "CreateObject" paramater like this:

ftp = WScript.CreateObject("Catalyst.FtpClientCtrl.1","FtpEvent_");

Events can be cought by functions named "FtpEvent_EVENTNAME"


Bernhard

Michael Harris

unread,
Feb 15, 1999, 3:00:00 AM2/15/99
to
I've never personally tried to sink winsock events, but try this:

Set objWS = Wscript.CreateObject("MSWinsock.Winsock","objWS_")

See the WSH docs for WScript.CreateObject.

--
Mike Harris

Tor Bothner wrote in message <36C81F6F...@drobak.geo.dec.com>...

Eric Lippert (Microsoft Scripting Dev)

unread,
Feb 15, 1999, 3:00:00 AM2/15/99
to
Well, to answer your question, "yes and no". Event handling works the same
way in the sense that you can have event sinks trigger functions of the form
"Obj_Ev". But it does not work the same way in the sense that you hook up
the events differently.

In IE, the objects that are going to be sunk are known when the script is
compiled:

<object id=foo clsid=whatever...></object>
<script language=vbscript>
sub foo_onBlah()
...

IE can tell the script engine _before the script runs_ "there will be an
object called foo and here are its events". Note that IE cannot do this
for objects which are created dynamically:

<script language=vbscript>
set foo = CreateObject("whatever") ' foo's events not sunk


Obviously, since running the script creates the object, it is impossible to
tell the script engine about the object before the script runs!

Now consider WSH. The current version of WSH does not have any compile-time
information about the objects -- the only objects available are created at
run time. (I say "the current version" because the next version will
support an IE-like syntax for statically creating objects, for exactly this
reason.)

So the original WSH author added a (in my opinion, somewhat cheesy) function
that handles the event sinking dynamically.

Set Foo = Wscript.CreateObject("Whatever", "Foo_")

Now WScript will sink foo's "blah" event and call "Foo_blah" when the event
fires. It knows what function to call based on the prefix passed as the
second argument.

Eric

Tor Bothner <bothn...@drobak.geo.dec.com> wrote in message
news:36C81F6F...@drobak.geo.dec.com...

0 new messages