![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Retrieving an array from JScript / MSScriptControl I am sure this has been covered, but right now I can't seem to track it down via Google. I'd like to know what the most direct way is to return an array to VBScript from JScript. Let's suppose that the JScript lives in an MSScriptControl. The obvious way of passing an array back from the script control to VBScript yields a comma separated string. This is not such a good thing if your array may contain commas. One could, of course, go through the JScript array, and somehow escape all the commas, but this is not so good if you have multi dimensional arrays. The following is a really hacky way to do it, but it would be real nice to have a direct way to pass the object back. Dim ie, oScript, code, out, aRay Set ie = CreateObject("InternetExplorer.Application") ie.Navigate2 "about:blank" ie.document.parentWindow.execScript("window.aRay=1;") Set oScript = CreateObject("MSScriptControl.ScriptControl") oScript.Language = "JScript" oScript.addObject "ie", ie code = "function retAr(ie) { " & _ "ie.document.parentWindow.aRay=['foo,bar', 'bob']; }" oScript.AddCode code oScript.Eval "retAr(ie)" MsgBox typename(ie.document.parentWindow.aRay) set aRay = ie.document.parentWindow.aRay out = "Size: " & aRay.length & vbCrLf For each idx in aRay: out = out & idx & vbCrLf: Next MsgBox out Any tips appreciated, Csaba Gabor from Vienna |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Retrieving an array from JScript / MSScriptControl "Csaba Gabor" <danswer@xxxxxx> wrote in message news:cfa9eaac-5f51-484a-a82f-6e47f08be66a@xxxxxx Quote: >I am sure this has been covered, but right > now I can't seem to track it down via Google. > I'd like to know what the most direct way is > to return an array to VBScript from JScript. > > Let's suppose that the JScript lives in an > MSScriptControl. The obvious way of passing > an array back from the script control to > VBScript yields a comma separated string. > This is not such a good thing if your array > may contain commas. > > One could, of course, go through the JScript > array, and somehow escape all the commas, > but this is not so good if you have multi > dimensional arrays. > > The following is a really hacky way to do it, > but it would be real nice to have a direct way > to pass the object back. > > Dim ie, oScript, code, out, aRay > Set ie = CreateObject("InternetExplorer.Application") > ie.Navigate2 "about:blank" > ie.document.parentWindow.execScript("window.aRay=1;") > > Set oScript = CreateObject("MSScriptControl.ScriptControl") > oScript.Language = "JScript" > oScript.addObject "ie", ie > code = "function retAr(ie) { " & _ > "ie.document.parentWindow.aRay=['foo,bar', 'bob']; }" > oScript.AddCode code > oScript.Eval "retAr(ie)" > > MsgBox typename(ie.document.parentWindow.aRay) > > set aRay = ie.document.parentWindow.aRay > out = "Size: " & aRay.length & vbCrLf > For each idx in aRay: out = out & idx & vbCrLf: Next > MsgBox out make the IE object visible, like: ie.Visible = true This will remind the person that each time they run it, an IE object is being created, and that this object will be running and use memory until the computer is rebooted or a script is run to terminate the orphanned IE objects. I played with this problem several years ago and never found a satisfactory, general purpose way to pass info between languages. One general purpose object that VBScript and JScript share is the dictionary object, but I don't know if this will help you. If you know of a character that your data will never contain, you could use that as a separator instead of a comma. If JScript strings use 16 bits per character, perhaps some Unicode character could be used as the separator. -Paul Randall |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Retrieving an array from JScript / MSScriptControl Csaba Gabor wrote: Quote: > I'd like to know what the most direct way is > to return an array to VBScript from JScript. > You can find an answer (whether it will satisfy you or not is problematical) in my reply to Sarah Ilyas posting in the news://microsoft.public.scripting.wsh newsgroup of 1/29/09 entitled: "Microsoft Script Control". It involves "exchanging calling cards". That is, you send in to the script control an object, which in my case was the parent form object -- but any container object will do. With that, you can then call (from the script running in the script control) the methods and properties of the parent object. cheers, jw ____________________________________________________________ You got questions? WE GOT ANSWERS!!! ..(but, no guarantee the answers will be applicable to the questions) |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Retrieving an array from JScript / MSScriptControl Thank you (both) for your comments, a few of my own are interspersed. JW, your comments about the thread below got me thinking, with a reply there. http://groups.google.com/group/micro...4834f273df21d/ On Feb 18, 5:25 pm, "Paul Randall" <paul...@xxxxxx> wrote: Quote: > "Csaba Gabor" <dans...@xxxxxx> wrote in message > > news:cfa9eaac-5f51-484a-a82f-6e47f08be66a@xxxxxx > Quote: > >I am sure this has been covered, but right > > now I can't seem to track it down via Google. > > I'd like to know what the most direct way is > > to return an array to VBScript from JScript. Quote: > > Let's suppose that the JScript lives in an > > MSScriptControl. The obvious way of passing > > an array back from the script control to > > VBScript yields a comma separated string. > > This is not such a good thing if your array > > may contain commas. Quote: > > One could, of course, go through the JScript > > array, and somehow escape all the commas, > > but this is not so good if you have multi > > dimensional arrays. Quote: > > The following is a really hacky way to do it, > > but it would be real nice to have a direct way > > to pass the object back. Quote: > > Dim ie, oScript, code, out, aRay > > Set ie = CreateObject("InternetExplorer.Application") > > ie.Navigate2 "about:blank" > > ie.document.parentWindow.execScript("window.aRay=1;") Quote: > > Set oScript = CreateObject("MSScriptControl.ScriptControl") > > oScript.Language = "JScript" > > oScript.addObject "ie", ie > > code = "function retAr(ie) { " & _ > > "ie.document.parentWindow.aRay=['foo,bar', 'bob']; }" > > oScript.AddCode code > > oScript.Eval "retAr(ie)" Quote: > > MsgBox typename(ie.document.parentWindow.aRay) Quote: > > set aRay = ie.document.parentWindow.aRay > > out = "Size: " & aRay.length & vbCrLf > > For each idx in aRay: out = out & idx & vbCrLf: Next > > MsgBox out > First, I think that anyone who tries this script should add a statement to > make the IE object visible, like: > ie.Visible = true > This will remind the person that each time they run it, an IE object is > being created, and that this object will be running and use memory until the > computer is rebooted or a script is run to terminate the orphanned IE > objects. up. For example, rerunning my original code a few times with the following at the top would be a good way of checking the window count: swCnt = 0 For each oWnd in CreateObject("Shell.Application").windows swCnt = swCnt + 1 Next MsgBox "Total ie/explorer windows: " & swCnt Nevertheless, with some environments (such as PHP) the window cleanup is not so guaranteed and orphaned objects may indeed be left lying around. I would therefore recommend an explicit ie.Quit to be on the safe side. Quote: > I played with this problem several years ago and never found a satisfactory, perhaps with discussions about GetRef, MemCopy and those types of functions. But I couldn't find anything when I searched recently. Quote: > general purpose way to pass info between languages. One general purpose > object that VBScript and JScript share is the dictionary object, but I don't > know if this will help you. a dictionary. But of course, it's even worse to get that back to vbscript than an array. I think the hokey technique I illustrated wasn't enough to get the keys back. So the formal Scripting.Dictionary object definitely provides a way of transfering an array and more. Note to self: obtain via var oDct = new ActiveXObject(“Scripting.Dictionary”) Quote: > If you know of a character that your data will never contain, you could use > that as a separator instead of a comma. If JScript strings use 16 bits per > character, perhaps some Unicode character could be used as the separator. > > -Paul Randall |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Having trouble with jscript | General Discussion | |||
| Fast copy method of sub array (=array range) possible? | VB Script | |||
| Stupid Array Tricks: Initializing an Array to a Certain Size | PowerShell | |||
| jscript error | Vista General | |||
| how to assign values to array and how to create array via variable | PowerShell | |||