
ActiveX Events in JScript Not Working (VBScript Works Fine)
First, you should not make an ActiveX control free-threaded. All the ActiveX
control containers are STA. If the control is MTA,
IOleObject::DoVerb(OLEIVERB_UIACTIVATE) will come on arbitrary MTA thread,
and you are supposed to create a window on this thread, and not only will
this thread be different than the one the container's window is created on,
this thread may just as well terminate right after DoVerb exits, and the
window is gone. Even if the thread lives long enough, it won't have a
message pump.
In short, ActiveX controls must be STA.
Second, you spin a new thread and intend to do COM on it, but you never
initialize it with CoInitialize(Ex).
Third, your worker thread is likely to be in different apartment from your
main thread (especially considering that ActiveX controls are STA and only
one thread can belong to a particular STA).You need to marshal interface
pointers from main thread to worker thread. It will be easier to just post a
message from the worker thread to the window created on the main thread, and
fire events from there.
--
With best wishes,
Igor Tandetnik
Quote:
> Warning: Lots of code contained within.
> I am having trouble getting events for an ActiveX control (written using
> ATL) to completely work from within JScript (JavaScript) in IE. I've been
> able to get then to work when the same thread used to call a method fires
> the event as in the following code excerpt for the method call:
> STDMETHODIMP CTestCtrl::Test(long *plValue)
> {
> *plValue = ++m_lValue;
> Fire_Changed(m_lValue);
> return S_OK;
> }
> But as soon as I move the event firing to it's own thread, it stops
working:
> DWORD WINAPI ThreadProc(LPVOID lpParameter)
> {
> CTestCtrl* pThis = (CTestCtrl*) lpParameter;
> Sleep(5000);
> pThis->Fire_Changed(m_lValue);
> return 0;
> }
> STDMETHODIMP CTestCtrl::Test(long *plValue)
> {
> *plValue = ++m_lValue;
> DWORD dwThreadID;
> m_hthread = CreateThread(NULL, 0, ThreadProc, this, 0,
&dwThreadID);
> return S_OK;
> }
> Both methods work fine from within VBScript. The delayed event fires and
is
> received by the VBScript code. Here's the HMTL for VBScript that works
> fine:
> <HTML>
> <HEAD>
> <TITLE>ATL 3.0 test page for object TestCtrl</TITLE>
> </HEAD>
> <BODY>
> <FORM><INPUT TYPE="button" NAME="Button1" Value="Do It"></FORM>
> <SCRIPT LANGUAGE="VBScript">
> Sub Button1_OnClick
> lValue = TestCtrl.Test()
> MsgBox "Returned from Test(): lValue = " & lValue
> End Sub
> Sub TestCtrl_Changed(ByVal lValue)
> MsgBox "Changed() event: lValue = " & lValue
> End Sub
> </SCRIPT>
> <OBJECT ID="TestCtrl"
> CLASSID="CLSID:5BEF906E-3855-11D5-8722-009027B06D16"></OBJECT>
> </BODY>
> </HTML>
> It's from JavaScript that the second method of firing on a separate event
> fails--no event is received in JavaScript. Here's the HTML for JavaScript
> that I am using:
> <HTML>
> <HEAD>
> <TITLE>ATL 3.0 test page for object TestCtrl</TITLE>
> </HEAD>
> <BODY>
> <FORM><INPUT TYPE="button" NAME="Button1" Value="Do It"
> OnClick="PushButton()"></FORM>
> <SCRIPT LANGUAGE="JScript">
> function PushButton()
> {
> var lValue = TestCtrl.Test();
> alert("Returned from Test(): lValue = " + lValue);
> }
> </SCRIPT>
> <SCRIPT LANGUAGE="JScript" FOR="TestCtrl" EVENT="Changed(lValue)">
> alert("Changed() event: lValue = " + lValue);
> </SCRIPT>
> <OBJECT ID="TestCtrl"
> CLASSID="CLSID:5BEF906E-3855-11D5-8722-009027B06D16"></OBJECT>
> </BODY>
> </HTML>
> The ATL object I've created is a full control and marked as "Free". I
don't
> know if my threading model is messing up JScript. Any ideas on this
> puzzling problem? Is this a bug or is it unsupported ? I'd appreciate
any
> ideas.
> --Beck