Advanced MSScript Control

Author: Dave
Date: 12.19.10 - 4:43am

So i am playing with the MSScript control trying to hack in support for some stuff in PDFStreamDumper and I am hitting some weird bugs.

Some I have figured out, some I have almost figured out. and some are kicking my ass :(

Here is the Javascript I need to support:


First you have to have an app.doc object. Ok that was easy.
Public app as object
public doc as object

Private Sub Class_Initialize()
    Set app = Me
    Set doc = Me
Then you create a getAnnots(arg) method. now {nPage:0} is a hash array. How do you access that in VB6? The TypeName(arg) turns out to be JScriptTypeInfo which didnt help at all. Turns out in my getAnnots(arg) function, you can access the nPage variable value by just asking for arg.nPage. Ok so that turned out to be easy too. (not intutive but easy)

So JScriptTypeInfo is the JS object you can make late bound calls to.

So now whatever we return from getAnnots() has to be able to be accessed in Javascript array notation, and each element has to be an object.

One would think you could just:
dim x()
set x(0) = me
getAnnots = x()
But this doesnt work. Not as a variant array, not as an object array. If you return a collection, it kind of works, but then your parent javascript would have to be written like:


Note the () method call instead of [] array object index. Doesnt jive if trying to keep existing JS working.

I even went to the hackerific test of:

getAnnots ="getJSArray(1,2,3)")

Where getJSArray is a function in the script control that explicitly returns a JS array. but..(and give me a big WTF here) my JS array is somehow automagically converted to a comma delimited string when it makes it back to VB6..

So i dont know how to return any kind of array from vb6 code run through the MSScript control to a Javascript function that called it. grrrr...

I may have to do the majority of the code in JS, and only call out to the VB6 methods to grab data. So build the structure in JS, data from VB. this works below: (where tb. is a VB6 form object with an alert method)
function AnnotClass(x){
   this.subject = "my Annot " + x

function getAnnot(x){
   var ret = new Array()
     ret[i] = new AnnotClass(i);
   return ret

tb.alert( getAnnot({nPage:22})[1].subject ) the above is now working. JS to build the function and array structure, and calling back into VB from the JS class to get the data.

Which brings us to our next bug. If you use the this keyword in a script in a script automatically refers to the first object you added with AddObject and you can not override it. grrr #2

Update: Actually there may be a way to trick it though..I dont know if I dare use this on complex scripts..but for simple scripts..I noticed that JS classes can use the this keyword and it does work. The difference is not only are they in a function..but the function is being called as class instantation with the new keyword.
text1 = "vbclass.myAlertTypeName( this )"
sc.AddCode "function main(){" & Text1 & "}"
sc.eval "var x = new main()"
The above code shows the this pointer is now a JScriptTypeInfo object instead of the VB class it would normally do. However this appears to now exclusivly refer to properties of the new JS object. So this.unescape is empty...aghh but that reveals a step in the right direction. Since this.unescape is just a can also hold a function
text1 = "this.unescape = unescape; vbclass.myAlert( this )"
Now gives us the format we need.

Dont know if i can trust that if I have to add scripts with their own functions and nested functions..might be worth a try for compatiablity sake, but seems like it could cause some really hard bugs to track down.

JS engines are not the most reliable creatures

Comments: (0)

Leave Comment:
Email: (not shown)
Message: (Required)
Math Question: 41 + 80 = ? followed by the letter: C 

About Me
More Blogs
Main Site
Posts: (All)
2020 ( 2 )
2019 ( 5 )
2018 ( 6 )
2017 ( 6 )
2016 ( 22 )
2015 ( 16 )
2014 ( 25 )
2013 ( 4 )
2012 ( 10 )
2011 ( 7 )
2010 (11)
     Dll Not Found in IDE
     Advanced MSScript Control
     random tip
     Clipart / Vector Art
     VB6 Callback from C#
     Binary data from VB6 to C#
     CSharp and MsScriptControl
     HexDumper functions
     Js Beautify From VB6 or C#
     vb6 FormPos
     Inline Asm w VB6
2009 (3)
     The .NET Fiasco
     One rub on computers
     Universal extractor