Page 1 of 2

Comate Plus GetStringProperty and MSScript

Posted: Tue Feb 04, 2014 2:49 am
by ricardo
Hi

Im running some vbs with Comate Plus.

Its possible to get some strings from the vbs code? (Not an error description, i mean text that i get from my vbs code, like the result of a function or the value of a property)

Thanks in advance!

Re: Comate Plus GetStringProperty and MSScript

Posted: Tue Feb 04, 2014 10:20 am
by srod
Something like the following ?

Code: Select all

IncludePath "..\"
XIncludeFile "COMatePLUS.pbi"

code$ = "Function StringFunction(ByVal a, ByVal b)" + #CRLF$
code$ + "StringFunction = a + " + Chr(34) + " " + Chr(34) + " + b" + #CRLF$
code$ + "End Function"

Define.COMateObject scriptObject, errorObject
scriptObject = COMate_CreateObject("MSScriptControl.ScriptControl.1") 
If scriptObject
  scriptObject\SetProperty("Language = 'VBScript'")
  If scriptObject\Invoke("AddCode('" + code$ + "')") = #S_OK
    result$ = scriptObject\GetStringProperty("Eval('StringFunction(" + Chr(34) + "Hello" + Chr(34) + ", " + Chr(34) + "Ricardo!" + Chr(34) + ")')")
    MessageRequester("Script demo", Chr(34) + code$ + Chr(34) + #LF$ + #LF$ + "Produced a result of : " + result$)
  Else
    errorObject = scriptObject\GetObjectProperty("Error")
    If errorObject  
      MessageRequester("Script demo - error reported!", Chr(34) + errorObject\GetStringProperty("Text") + Chr(34) + #LF$ + " produced the following error : " + #LF$ + #LF$ + errorObject\GetStringProperty("Description"))
      errorObject\Release()
    EndIf
  EndIf
  scriptObject\Release()
Else
  MessageRequester("COMate - Scripting demo", "Couldn't create the scripting object!")
EndIf

Re: Comate Plus GetStringProperty and MSScript

Posted: Tue Feb 04, 2014 9:16 pm
by ricardo
Yes, exactly as that!! :) You are my hero!! Thanks :)

Re: Comate Plus GetStringProperty and MSScript

Posted: Wed Feb 05, 2014 1:41 am
by ricardo
Is there any way to get some kind of raise external event?
I want to make a function that is received in PB without being any OCX event, just a vbs function or something like that.

I mean, some event that calls PB, like a callback.

Re: Comate Plus GetStringProperty and MSScript

Posted: Wed Feb 05, 2014 10:02 am
by srod
I have wondered that myself, though have not investigated it.

As far as I am aware, VBS can, for example, only access COM dll's through CreateObject() etc. VBS cannot directly access regular dll's except through a COM wrapper.

With this in mind, I would guess that you are not going to be able to easily achieve what you are after. Think about it, allowing a VBS script running in IE, for example, to call an external function could allow a malicious script to cause all sorts of damage.

If it is possible, then I am guessing that it will not be easy. :)

Re: Comate Plus GetStringProperty and MSScript

Posted: Wed Feb 05, 2014 5:28 pm
by ricardo
Hi Srod, thanks for you kind answer, as always.

Javascript have the same limitation, to mantain separated the PC and the browser, however Freak give us a way to use raise event and that allow us to receive some JS eventS direct on PB. I was wondering if we can find a similar situation in VBS.

Re: Comate Plus GetStringProperty and MSScript

Posted: Wed Feb 05, 2014 7:03 pm
by srod
My dodgy memory tells me that the javascript was running in a web gadget, which is completely different to running in the MS Script Control.

Anyhow, the script control exposes 2 events; one for timeouts and the other for errors, neither of which will be of any use here.

The only option as I see it is to use the AddObject() method of the script control, but the objects added have to be COM/iDispatch objects. This really does seem to be the only way. I reckon it is doable, we just have to create our own iDispatch object.

I may give it a go some time if I feel the urge! ;)

Re: Comate Plus GetStringProperty and MSScript

Posted: Wed Feb 05, 2014 7:52 pm
by ricardo
Many years ago i made some MSAgent examples and i used some COM to get the events back to PB.

I mean, the COM was not used to manage the MSAgent, only was used to retreive the events.

One option (the best, but the harder) is the one you point.

Another is to find (or develope) some COM object to receive events.

If you decide to develope your idea i know that you will have a place in the history of PB!! :)

Re: Comate Plus GetStringProperty and MSScript

Posted: Wed Feb 05, 2014 8:02 pm
by srod
The script object will not dispatch any events other than the two I mentioned and any COM object you create to receive events raised in script will really have to be attached using the aforementioned AddObject() method. It all comes down to the creation of an iDispatch interface... which can be a pain in the a*se! :)

Re: Comate Plus GetStringProperty and MSScript

Posted: Wed Feb 05, 2014 8:57 pm
by srod
Ok Ricardo, I have it working. It was quite easy actually. :)

Take me a while to tidy the code up a bit and create a more robust demo for COMate etc. but it all seems to be working well.

You do realise that the MS Script control is 32-bit only right? You will not get this working in x64.

Re: Comate Plus GetStringProperty and MSScript

Posted: Wed Feb 05, 2014 10:46 pm
by srod
Ricardo,

I have added a demo to the COMatePLUS package : 'Demo_Script_VBS - call PB function.pb'.

It uses a little include file : 'VBS_Call PB function_INCLUDE.pbi' which contains a helper function which takes care of creating the COM object needed to receive function calls from a VB script etc.

This of course is 32-bit only because there is no MS Script Control for x64.

The include allows a VB script to call a given PB function. You can have the script pass as many parameters as you wish and your PB function receives them in the form of a string array. Obviously you can change that by altering the COM interface.

An example VB Script calling a PB function is :

Code: Select all

Function SumIntegers(ByVal a, ByVal b)
  SumIntegers = a + b
  InvokePB.OurEvents "SumIntegers() finished execution...", 10
End Function
I have set it up so that you can change the 'OurEvents' method name to anything you want. If you execute that code (as our demo does) in the MS Script Control then a function you would have declared previously in your PB program will be called and the 2 parameters : "SumIntegers() finished execution..." and 10 will be passed in a string array.

See the demo.

I hope that helps.

Re: Comate Plus GetStringProperty and MSScript

Posted: Wed Feb 12, 2014 1:35 am
by ricardo
Yes, that REALLY helps!!!!

Its awesome!!

Does anybody knows if VBS works fine in W7 and W8 32 bits?

Re: Comate Plus GetStringProperty and MSScript

Posted: Wed Feb 12, 2014 11:41 am
by srod
Runs fine here with Win 7 x64. Of course I compiled to 32-bit to use the MSScriptControl class.

Re: Comate Plus GetStringProperty and MSScript

Posted: Wed Feb 12, 2014 5:36 pm
by ricardo
Then you need to compile to 32, but runs on 64 too, right?

Re: Comate Plus GetStringProperty and MSScript

Posted: Wed Feb 12, 2014 5:44 pm
by srod
It runs as a Wow64 process, but it is of course still a 32-bit program.

There is no script control for x64.