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:

app.doc.getAnnots({nPage:0})[num].subject;

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:

app.doc.getAnnots({nPage:0})(num).subject;

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 = Form2.sc.eval("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){
   tb.alert(x.nPage)
   var ret = new Array()
   for(i=0;i<5;i++){
     ret[i] = new AnnotClass(i);
   }
   return ret
}

tb.alert( getAnnot({nPage:22})[1].subject )
ok..so 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 control..it 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 property..it can also hold a function reference..so
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:
Name:
Email: (not shown)
Message: (Required)
Math Question: 53 + 33 = ? followed by the letter: E 



About Me
More Blogs
Main Site
Posts:
Console tricks
FireFox temp dir
OCX License
Extract substring
VB6 Console Apps
VB6 UDTs
VB6 Debugger View As Hex tooltips
VB6 - C Share registry data
VB6 Addin Missing Menus
VB6 Class Init Params
VB6 isIn function
Python and VB6
Python pros and cons
download web Dir
vc rand in python
VB6 Language Enhancement
Register .NET as COM
VB6 CDECL
UDT Tricks pt2
Remote Data Extraction
Collection Extender
VB6 FindResource
CDO.Message
DirList Single Click
Reset CheckPoint VPN Policy
VB6 BSTR Oddities Explained
SafeArrays in C
BSTR and Variant in C++
Property let optional args
Misc Libs
Enum Named Pipes
Vb6 Collection in C++
VB6 Overloaded Methods
EXPORT FUNCDNAME Warning
VB6 Syncronous Socket
Simple IPC
VB6 Auto Resize Form Elements
Mach3 Automation
Exit For in While
C# self register ocx
VB6 Class Method Pointers
JS Debugger
Duktape Debug Protocol
QtScript 4 VB
Vb6 Named Args
vb6 Addin Part 2
VB6 Addin vrs Toolbars
OpenFile Dialog MultiSelect
Duktape Example
DukTape JS
VB6 Unsigned
.Net version
TitleBar Height
.NET again
VB6 Self Register OCXs
Query Last 12 Mos
Progid from Interface ID
VB6 to C Array Examples
Human Readable Variant Type
ScriptBasic COM Integration
CodeView Addin
ScriptBasic - Part 2
Script Env
MSCOMCTL Win7 Error
printf override
History Combo
Disable IE
API Hooking in VB6
Addin Hook Events
FastBuild Addin
VB6 MemoryWindow
Link C Obj Files into VB6
Vb6 Standard Dlls
CStr for Pascal
Lazarus Review
asprintf for VS
VB6 GlobalMultiUse
Scintilla in VB6
Dynamic Highlight
WinVerifyTrust, CryptMsgGetParam VB6
MS GLEE Graphing
printf for VB6
C# App Config
Tero DES C# Test
VC 2008 Bit Fields
Speed trap
C# Db Class Generator
VB6 vrs .NET (again)
FireFox Whois Extension
git and vb6
Code Additions
Compiled date to string
C# ListView Sorter
VB6 Wish List
C# Process Injection
CAPTCHA Bots
C# PE Offset Calculator
VB6 Async Download
Show Desktop
coding philosophy
Code release
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
The .NET Fiasco
One rub on computers
Universal extractor