2

I want to use the new JScript features in IE9 (native json, ...) from a VB6 Host. From what I've read (see http://blogs.msdn.com/b/jscript/archive/2009/04/17/versioning-language-features-in-jscript.aspx), I have to invoke the IActiveScriptProperty::SetProperty and set SCRIPTPROP_INVOKEVERSIONING to 2 (SCRIPTLANGUAGEVERSION_5_8). So, I have added the interface to my odl file:

...
[
   odl,
   uuid(4954E0D0-FBC7-11D1-8410-006008C3FBFC),
]
interface IActiveScriptProperty : stdole.IUnknown
{
   HRESULT GetProperty(
      [in]  LONG    dwProperty,
      [in]  VARIANT *pvarIndex,
      [out] VARIANT *pvarValue
   );

   HRESULT SetProperty(
      [in] LONG    dwProperty,
      [in] VARIANT *pvarIndex,
      [in] VARIANT *pvarValue
   );
}
...

In the VB6 host, I create the engine with:

Dim hRes as Long
Dim IUnk as IUnknown
Dim clsidJS as UUID
Dim uuidActScr as UUID
Dim IProperty as IActiveScriptProperty
Dim IScript As IActiveScript
Dim IParse As IActiveScriptParse

' Create the engine
CLSIDFromString "{16d51579-a30b-4c8b-a276-0ff4dc41e755}", clsidJS    ' JScript9 (Chakra)
CLSIDFromString IID_IActiveScript, uuidActScr
hRes = CoCreateInstance(clsidJS, Nothing, CLSCTX_INPROC_SERVER, uuidActScr, IUnk)

' Set version
Const SCRIPTPROP_INVOKEVERSIONING As Long = &H4000
Dim Version as Variant
Version = 2
Set IProperty = iUnk
IProperty.SetProperty SCRIPTPROP_INVOKEVERSIONING, 0, Version '<--- Here I get error 5 "Invalid procedure call or argument"

In the last comment of the previous article, Byron says: "The undocumented 'feature' of the SetProperty with SCRIPTPTOP_INVOKEVERSIONING is that the value must be a VT_I4 or VT_I2 - any other integer type will be rejected as invalid."

So I modify the above code to (VariantType property comes from http://www.xbeat.net/vbspeed/i_OpenODL.htm#VBVM6Lib):

...
Version = 2
VariantType(Version) = VT_I4 ' Force VT_I4 variant type
Set IProperty = iUnk
IProperty.SetProperty SCRIPTPROP_INVOKEVERSIONING, 0, Version '<--- Here I get the same error 5 "Invalid procedure call or argument"

NOTE: If I don't try to set SCRIPTPROP_INVOKEVERSIONING property, the engine works ok and if I run: ScriptEngineMajorVersion() + "." + ScriptEngineMinorVersion() + "." + ScriptEngineBuildVersion() I get "9.0.16457', but I don't have access to the new features as native json.

Any ideas?

Thanks!

1 Answers1

0

You have to change declaration ot SetProperty to

HRESULT SetProperty(
      [in] LONG    dwProperty,
      [in] void    *pvarIndex,
      [in] VARIANT *pvarValue
   );

to be able to set index-less properties. Just pass 0 (NULL) as you did in your sample code. Present declaration treats SCRIPTPROP_INVOKEVERSIONING as an array and you are setting first index to some value.

Mind that VT_I2 = Integer in VB6 and VT_I4 = Long, so no need to hack these. Just use 2 or 2& or Private Const SCRIPTLANGUAGEVERSION_5_8 As Long = 2 and the const will be correctly typed.

Also note that on this line hRes = CoCreateInstance(clsidJS, Nothing, CLSCTX_INPROC_SERVER, uuidActScr, IUnk) you are already getting IActiveScript interface. No need later to cast Set IProperty = iUnk.

It all depends how you declare CoCreateInstance -- using void * for last param will allow you to directly pass IProperty variable and get it initialized with the IActiveScript interface of clsidJS.

wqw
  • 11,771
  • 1
  • 33
  • 41
  • Thanks! I changed the declaration of `SetProperty`. Now, I don't get error, but `JSON` object remains `not defined`. Any ideas? – user2055079 Feb 12 '13 at 12:35
  • What do you do next with `clsidJS` (Chakra) object? How do you plan to use this particular object with IE9? I think that IE has to spawn its scripting engine on its own. Probably engine settings are controlled with doctypes or metas in the loaded html page. Sorry, but you approach seems totally wrong headed. – wqw Feb 12 '13 at 13:14
  • Thanks for reply wqw. I don't want to use the object with IE9. I want to create a `JScript host` with `VB6`. From the article referenced above: **"Apart from Internet Explorer, there are various other hosts such as Windows Script Host, CScript etc. which host the JScript engine, To avoid similar compatibility issues and enable hosts to choose a particular JScript language feature set, the JScript engine now exposes an IActiveScriptProperty::SCRIPTPROP_INVOKEVERSIONING property."** – user2055079 Feb 12 '13 at 15:26
  • 1
    You do realize this property is per instance i.e. if you are using some kind of custom-made host you can setup your scripting engine before passing JScript source code to it (all this wrapped in your custom-made host environment). This way each host can decide what means to use to configure its scripting engines -- metas, config files, registry. – wqw Feb 12 '13 at 16:04
  • Yes, but the problem appears when I want to set up my scripting engine instance (before passing JScript source code to it) by calling to the "IActiveScriptProperty::SCRIPTPROP_INVOKEVERSIONING" property. Thanks and sorry. Surely my poor English complicates your response... – user2055079 Feb 12 '13 at 16:43