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

IHTMLWindow2::execScript -How do I get return value

1,637 views
Skip to first unread message

Ry

unread,
Dec 17, 1999, 3:00:00 AM12/17/99
to
Hi

Does anybody know how to get a return value from IHTMLWindow2::execScript?
Here is the function as described in MSDN library:

HRESULT execScript(
BSTR code,
BSTR language,
VARIANT *pvarRet
);
Executes the specified script.

Returns S_OK if successful, or an error value otherwise.
code
String specifying the script code to execute.
language
String specifying the language in which the code is executed. The default is
JScript.
pvarRet
Address of a variable that receives the return value from the script code.

My question is this:
How do you get a return value from script code into pvarRet?
For example, if this is the JavaScript code:
var retVal = 2 + 2;
How could I put the value of retVal into pvarRet?
There does not seem to be anything in JavaScript or DHTML that can write the
value of a JavaScript variable to a VARIANT.

Idea:
By using "IHTMLWindow2::execScript" you could attach all the capability of
DHTML and JavaScript to menus and toolbars and dialog boxes. You could write
most of your program in JavaScript instead of using all that complicated
query interface stuff.

Thanks

Anton Spaans

unread,
Dec 18, 1999, 3:00:00 AM12/18/99
to
Hi Ry,

The return value of your script is the last expression evaluated, like in
C++.

For example:
...
ulVar += 1;
...

is an expression that returns a value 1 greater than ulVar, and incremement
ulVar on the fly... but that you now.

It is the same with Java, and i expect, with JavaScript as well.

Example:
script: "2+2"
expression's return value: 4

script: "var retval = 2+2;"
expression's return value: I don't know, because this script is not an
expression, but a declaration and definition.

script: "retval = 2+2"
expression's return value: 4, being the left-most value, i.e. the value of
"retval"

script: "myfunction(2)"
expression's return value: the value that 'myfunction(2)' returns

script:"myfunction(value) { return 2*value}"
expression's return value: I don't know, because this script is not an
expression, but a declaration and definition.


JavaScript, like any language in the scripting-host, usea only variants as
values. Scripts are type-less. So any value you use/return is a variant. Use
in you C++ code, which is typed, type-coercion (VariantChangeType(), for
example) to get the desired type. In your example:

pWin->execScript(T2BSTR("retval = 2+2"), "JScript", &comResultVariant);
comResultVariant.ChangeType(VT_I4);

I hope this helps you.
Good luck.
Anton.
Ry <r...@blaze.ca> wrote in message news:385b2...@news.vphos.net...

Todd Stafney

unread,
Dec 20, 1999, 3:00:00 AM12/20/99
to
C++
-------
_variant_t vRetVal;
_bstr_t bstrFunc("myFunc");
_bstr_t bstrLang("JavaScript");
pSomeHTMLWindow2->execScript(bstrFunc, bstrLang, &vRetVal);
if (vRetVal.vt == VT_BSTR) OutputDebugString(vRetVal.bstrVal);
-------

JavaScript
-------
function myFunc()
{
return "Hello There";
// or return x;
// or return anything_you_want;
}


HTH,
Todd

Ry

unread,
Dec 21, 1999, 3:00:00 AM12/21/99
to
Thanks Guys for your suggestions.
I still can't get IHTMLWindow2::execScript to return a value. I tried the
example that Todd wrote (see below) and others.

This is the function I am trying to use:


"HRESULT execScript( BSTR code, BSTR language, VARIANT *pvarRet);

I want the script to assign a value to vRetVal.
This is the C++ code I used:

_variant_t vRetVal;
_bstr_t bstrFunc("myFunc()");
_bstr_t bstrLang("JavaScript");
pHTMLWindow2->execScript(bstrFunc, bstrLang, &vRetVal);
if (vRetVal.vt == VT_BSTR)
{
CString s = vRetVal.bstrVal;
AfxMessageBox(s);
}

I used this JavaScript code in the web page:

<script language="JavaScript"><!--
function myFunc()
{
var retVal = "Hello There";
alert(retVal);
return retVal;
}
// --></script>

This is what happens when the function runs:

- An Alert box that says "Hello There" pops up on the web page.
- vRetVal.vt == VT_EMPTY.
- All values of vRetVal become set to zero, although they were at non - zero
(probably random) values one line before execScript.

I know the script does actually run in the CHTMLView window because of the
alert box.
I know execScript does cause an effect to vRetVal, because all values become
set to zero.
Scripts that return an integer give similar results, as do scripts that are
all contained in the BSTR passed to execScript.
Does anybody have an Idea why I can't get a return value ?

Merry Christmas


Todd Stafney wrote in message ...

Anton Spaans

unread,
Dec 21, 1999, 3:00:00 AM12/21/99
to
Hi Ry, this is a nasty problem...

You could try to give your return value a type: VT_BSTR | VT_BYREF and let
the callee fill in the value.

VARIANT vRetVal;
VariantInit(&vtRetVal);
V_VT(&vtRetVal) = VT_BSTR | VT_BYREF
*vRetVal.pbstrVal = ::SysAllocString(....something...);


_bstr_t bstrFunc("myFunc()");
_bstr_t bstrLang("JavaScript");
pHTMLWindow2->execScript(bstrFunc, bstrLang, &vRetVal);

if (vRetVal.vt)
{
CString s = *vRetVal.pbstrVal;
AfxMessageBox(s);
}
::SysFreeString(*vRetVal.pbstrVal);

This is just a guess, it may not work... but maybe it does.
Good luck.

Anton.
Ry <r...@blaze.ca> wrote in message news:385f3...@news.vphos.net...

Israel Disatnik

unread,
Jan 6, 2000, 3:00:00 AM1/6/00
to
Hello Ry,
looks like the retval parameter was included by MS in the COM interface but
cannot be implemented because it does not exist in the original DHTML object
model it is supposed to reflect.
See the definition of execScript in DHTML:

window.execScript(sExpression, sLanguage)

(no return value!)

check out:
http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/execscript
.asp

Srul.
(If you find any workaround I'll be glad to know, I need it too :)

Tim Tabor

unread,
Mar 3, 2000, 3:00:00 AM3/3/00
to
window.execScript() *returns* the value that shows up as
IHTMLWindow2::execScript(...,...,pvarRet).

VARIANT execScript(
[in] BSTR code,
[in, optional, defaultvalue("JScript")] BSTR
language);

HRESULT execScript(
[in] BSTR code,
[in, optional, defaultvalue("JScript")] BSTR
language,
[out, retval] VARIANT* pvarRet);


Israel Disatnik <i...@srul.com> wrote in message
news:852rfp$8mv$1...@news.netvision.net.il...

0 new messages