reorganized the samples, again. Moved the programs that were more test cases to the unit test directory and repackaged the unit test directory.
This commit is contained in:
2
unittest/com/jacob/test/vbscript/ScriptTest.bat
Normal file
2
unittest/com/jacob/test/vbscript/ScriptTest.bat
Normal file
@@ -0,0 +1,2 @@
|
||||
java ScriptTest "1+2"
|
||||
java ScriptTest "()1+2"
|
||||
50
unittest/com/jacob/test/vbscript/ScriptTest.java
Normal file
50
unittest/com/jacob/test/vbscript/ScriptTest.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package com.jacob.test.vbscript;
|
||||
|
||||
import com.jacob.com.*;
|
||||
import com.jacob.activeX.*;
|
||||
|
||||
/**
|
||||
* In this case the component is created and used in the same thread
|
||||
* and it's an Apartment Threaded component, so we call InitSTA.
|
||||
*/
|
||||
class ScriptTest
|
||||
{
|
||||
public static void main(String args[]) throws Exception
|
||||
{
|
||||
ComThread.InitSTA(true);
|
||||
DispatchEvents de = null;
|
||||
Dispatch sControl = null;
|
||||
|
||||
try {
|
||||
String lang = "VBScript";
|
||||
ActiveXComponent sC = new ActiveXComponent("ScriptControl");
|
||||
sControl = (Dispatch)sC.getObject();
|
||||
Dispatch.put(sControl, "Language", lang);
|
||||
ScriptTestErrEvents te = new ScriptTestErrEvents();
|
||||
de = new DispatchEvents(sControl, te);
|
||||
if (de == null){
|
||||
System.out.println("Received null when trying to create new DispatchEvents");
|
||||
}
|
||||
Variant result = Dispatch.call(sControl, "Eval", args[0]);
|
||||
// call it twice to see the objects reused
|
||||
result = Dispatch.call(sControl, "Eval", args[0]);
|
||||
// call it 3 times to see the objects reused
|
||||
result = Dispatch.call(sControl, "Eval", args[0]);
|
||||
System.out.println("eval("+args[0]+") = "+ result);
|
||||
} catch (ComException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Integer I = null;
|
||||
for(int i=1;i<1000000;i++)
|
||||
{
|
||||
I = new Integer(i);
|
||||
}
|
||||
System.out.println(I);
|
||||
ComThread.Release();
|
||||
ComThread.quitMainSTA();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
98
unittest/com/jacob/test/vbscript/ScriptTest2.java
Normal file
98
unittest/com/jacob/test/vbscript/ScriptTest2.java
Normal file
@@ -0,0 +1,98 @@
|
||||
package com.jacob.test.vbscript;
|
||||
|
||||
import com.jacob.com.*;
|
||||
import com.jacob.activeX.*;
|
||||
|
||||
/**
|
||||
* This example demonstrates how to make calls between
|
||||
* two different STA's.
|
||||
* First, to create an STA, you need to extend the STA class
|
||||
* and override its OnInit() method. This method will be called
|
||||
* in the STA's thread so you can use it to create your COM
|
||||
* components that will run in that STA.
|
||||
* If you then try to call methods on those components from other
|
||||
* threads (STA or MTA) - this will fail.
|
||||
* You cannot create a component in an STA and call its methods
|
||||
* from another thread.
|
||||
* You can use the DispatchProxy to get a proxy to any Dispatch
|
||||
* that lives in another STA. This object has to be created in the
|
||||
* STA that houses the Dispatch (in this case it's created in the
|
||||
* OnInit method). Then, another thread can call the toDispatch()
|
||||
* method of DispatchProxy to get a local proxy. At most ONE (!)
|
||||
* thread can call toDispatch(), and the call can be made only once.
|
||||
* This is because a IStream object is used to pass the proxy, and
|
||||
* it is only written once and closed when you read it.
|
||||
* If you need multiple threads to access a Dispatch pointer, then
|
||||
* create that many DispatchProxy objects.
|
||||
*/
|
||||
class ScriptTest2 extends STA
|
||||
{
|
||||
public static ActiveXComponent sC;
|
||||
public static DispatchEvents de = null;
|
||||
public static Dispatch sControl = null;
|
||||
public static DispatchProxy sCon = null;
|
||||
|
||||
public boolean OnInit()
|
||||
{
|
||||
try
|
||||
{
|
||||
System.out.println("OnInit");
|
||||
System.out.println(Thread.currentThread());
|
||||
String lang = "VBScript";
|
||||
sC = new ActiveXComponent("ScriptControl");
|
||||
sControl = (Dispatch)sC.getObject();
|
||||
|
||||
// sCon can be called from another thread
|
||||
sCon = new DispatchProxy(sControl);
|
||||
|
||||
Dispatch.put(sControl, "Language", lang);
|
||||
ScriptTestErrEvents te = new ScriptTestErrEvents();
|
||||
de = new DispatchEvents(sControl, te);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnQuit()
|
||||
{
|
||||
System.out.println("OnQuit");
|
||||
}
|
||||
|
||||
public static void main(String args[]) throws Exception
|
||||
{
|
||||
try {
|
||||
ComThread.InitSTA();
|
||||
ScriptTest2 script = new ScriptTest2();
|
||||
Thread.sleep(1000);
|
||||
|
||||
// get a thread-local Dispatch from sCon
|
||||
Dispatch sc = sCon.toDispatch();
|
||||
|
||||
// call a method on the thread-local Dispatch obtained
|
||||
// from the DispatchProxy. If you try to make the same
|
||||
// method call on the sControl object - you will get a
|
||||
// ComException.
|
||||
Variant result = Dispatch.call(sc, "Eval", args[0]);
|
||||
System.out.println("eval("+args[0]+") = "+ result);
|
||||
script.quit();
|
||||
System.out.println("called quit");
|
||||
} catch (ComException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Integer I = null;
|
||||
for(int i=1;i<1000000;i++)
|
||||
{
|
||||
I = new Integer(i);
|
||||
}
|
||||
System.out.println(I);
|
||||
ComThread.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
96
unittest/com/jacob/test/vbscript/ScriptTest2ActiveX.java
Normal file
96
unittest/com/jacob/test/vbscript/ScriptTest2ActiveX.java
Normal file
@@ -0,0 +1,96 @@
|
||||
package com.jacob.test.vbscript;
|
||||
|
||||
import com.jacob.com.*;
|
||||
import com.jacob.activeX.*;
|
||||
|
||||
/**
|
||||
* This example demonstrates how to make calls between
|
||||
* two different STA's.
|
||||
* First, to create an STA, you need to extend the STA class
|
||||
* and override its OnInit() method. This method will be called
|
||||
* in the STA's thread so you can use it to create your COM
|
||||
* components that will run in that STA.
|
||||
* If you then try to call methods on those components from other
|
||||
* threads (STA or MTA) - this will fail.
|
||||
* You cannot create a component in an STA and call its methods
|
||||
* from another thread.
|
||||
* You can use the DispatchProxy to get a proxy to any Dispatch
|
||||
* that lives in another STA. This object has to be created in the
|
||||
* STA that houses the Dispatch (in this case it's created in the
|
||||
* OnInit method). Then, another thread can call the toDispatch()
|
||||
* method of DispatchProxy to get a local proxy. At most ONE (!)
|
||||
* thread can call toDispatch(), and the call can be made only once.
|
||||
* This is because a IStream object is used to pass the proxy, and
|
||||
* it is only written once and closed when you read it.
|
||||
* If you need multiple threads to access a Dispatch pointer, then
|
||||
* create that many DispatchProxy objects.
|
||||
*/
|
||||
class ScriptTest2ActiveX extends STA
|
||||
{
|
||||
public static ActiveXComponent sC;
|
||||
public static DispatchEvents de = null;
|
||||
public static DispatchProxy sCon = null;
|
||||
|
||||
public boolean OnInit()
|
||||
{
|
||||
try
|
||||
{
|
||||
System.out.println("OnInit");
|
||||
System.out.println(Thread.currentThread());
|
||||
String lang = "VBScript";
|
||||
sC = new ActiveXComponent("ScriptControl");
|
||||
|
||||
// sCon can be called from another thread
|
||||
sCon = new DispatchProxy(sC);
|
||||
|
||||
sC.setProperty("Language", lang);
|
||||
ScriptTestErrEvents te = new ScriptTestErrEvents();
|
||||
de = new DispatchEvents(sC, te);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnQuit()
|
||||
{
|
||||
System.out.println("OnQuit");
|
||||
}
|
||||
|
||||
public static void main(String args[]) throws Exception
|
||||
{
|
||||
try {
|
||||
ComThread.InitSTA();
|
||||
ScriptTest2ActiveX script = new ScriptTest2ActiveX();
|
||||
Thread.sleep(1000);
|
||||
|
||||
// get a thread-local Dispatch from sCon
|
||||
ActiveXComponent sc = new ActiveXComponent(sCon.toDispatch());
|
||||
|
||||
// call a method on the thread-local Dispatch obtained
|
||||
// from the DispatchProxy. If you try to make the same
|
||||
// method call on the sControl object - you will get a
|
||||
// ComException.
|
||||
Variant result = sc.invoke("Eval", args[0]);
|
||||
System.out.println("eval("+args[0]+") = "+ result);
|
||||
script.quit();
|
||||
System.out.println("called quit");
|
||||
} catch (ComException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Integer I = null;
|
||||
for(int i=1;i<1000000;i++)
|
||||
{
|
||||
I = new Integer(i);
|
||||
}
|
||||
System.out.println(I);
|
||||
ComThread.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
68
unittest/com/jacob/test/vbscript/ScriptTest3.java
Normal file
68
unittest/com/jacob/test/vbscript/ScriptTest3.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package com.jacob.test.vbscript;
|
||||
|
||||
import com.jacob.com.*;
|
||||
import com.jacob.activeX.*;
|
||||
|
||||
/**
|
||||
* Here we create the ScriptControl component in a separate MTA thread
|
||||
* and then call the Eval method from the main thread. The main thread
|
||||
* must also be an MTA thread. If you try to create it as an STA
|
||||
* then you will not be able to make calls into a component running
|
||||
* in another thread.
|
||||
*/
|
||||
class ScriptTest3 extends Thread
|
||||
{
|
||||
public static ActiveXComponent sC;
|
||||
public static DispatchEvents de = null;
|
||||
public static Dispatch sControl = null;
|
||||
public static boolean quit = false;
|
||||
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
ComThread.InitMTA();
|
||||
System.out.println("OnInit");
|
||||
String lang = "VBScript";
|
||||
sC = new ActiveXComponent("ScriptControl");
|
||||
sControl = (Dispatch)sC.getObject();
|
||||
Dispatch.put(sControl, "Language", lang);
|
||||
ScriptTestErrEvents te = new ScriptTestErrEvents();
|
||||
de = new DispatchEvents(sControl, te);
|
||||
System.out.println("sControl="+sControl);
|
||||
while (!quit) sleep(100);
|
||||
ComThread.Release();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
System.out.println("worker thread exits");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) throws Exception
|
||||
{
|
||||
try {
|
||||
ComThread.InitMTA();
|
||||
ScriptTest3 script = new ScriptTest3();
|
||||
script.start();
|
||||
Thread.sleep(1000);
|
||||
|
||||
Variant result = Dispatch.call(sControl, "Eval", args[0]);
|
||||
System.out.println("eval("+args[0]+") = "+ result);
|
||||
System.out.println("setting quit");
|
||||
ScriptTest3.quit = true;
|
||||
} catch (ComException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
System.out.println("main done");
|
||||
ComThread.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
66
unittest/com/jacob/test/vbscript/ScriptTest3ActiveX.java
Normal file
66
unittest/com/jacob/test/vbscript/ScriptTest3ActiveX.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package com.jacob.test.vbscript;
|
||||
|
||||
import com.jacob.com.*;
|
||||
import com.jacob.activeX.*;
|
||||
|
||||
/**
|
||||
* Here we create the ScriptControl component in a separate MTA thread
|
||||
* and then call the Eval method from the main thread. The main thread
|
||||
* must also be an MTA thread. If you try to create it as an STA
|
||||
* then you will not be able to make calls into a component running
|
||||
* in another thread.
|
||||
*/
|
||||
class ScriptTest3ActiveX extends Thread
|
||||
{
|
||||
public static ActiveXComponent sC;
|
||||
public static DispatchEvents de = null;
|
||||
public static boolean quit = false;
|
||||
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
ComThread.InitMTA();
|
||||
System.out.println("OnInit");
|
||||
String lang = "VBScript";
|
||||
sC = new ActiveXComponent("ScriptControl");
|
||||
sC.setProperty("Language", lang);
|
||||
ScriptTestErrEvents te = new ScriptTestErrEvents();
|
||||
de = new DispatchEvents(sC, te);
|
||||
System.out.println("sControl="+sC);
|
||||
while (!quit) sleep(100);
|
||||
ComThread.Release();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
System.out.println("worker thread exits");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) throws Exception
|
||||
{
|
||||
try {
|
||||
ComThread.InitMTA();
|
||||
ScriptTest3ActiveX script = new ScriptTest3ActiveX();
|
||||
script.start();
|
||||
Thread.sleep(1000);
|
||||
|
||||
Variant result = sC.invoke("Eval", args[0]);
|
||||
System.out.println("eval("+args[0]+") = "+ result);
|
||||
System.out.println("setting quit");
|
||||
ScriptTest3ActiveX.quit = true;
|
||||
} catch (ComException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
System.out.println("main done");
|
||||
ComThread.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
49
unittest/com/jacob/test/vbscript/ScriptTestActiveX.java
Normal file
49
unittest/com/jacob/test/vbscript/ScriptTestActiveX.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package com.jacob.test.vbscript;
|
||||
|
||||
import com.jacob.com.*;
|
||||
import com.jacob.activeX.*;
|
||||
|
||||
/**
|
||||
* In this case the component is created and used in the same thread
|
||||
* and it's an Apartment Threaded component, so we call InitSTA.
|
||||
*/
|
||||
class ScriptTestActiveX
|
||||
{
|
||||
public static void main(String args[]) throws Exception
|
||||
{
|
||||
ComThread.InitSTA(true);
|
||||
DispatchEvents de = null;
|
||||
|
||||
try {
|
||||
String lang = "VBScript";
|
||||
ActiveXComponent sC = new ActiveXComponent("ScriptControl");
|
||||
sC.setProperty("Language",lang);
|
||||
ScriptTestErrEvents te = new ScriptTestErrEvents();
|
||||
de = new DispatchEvents(sC, te);
|
||||
if (de == null){
|
||||
System.out.println("null returned when trying to create DispatchEvents");
|
||||
}
|
||||
Variant result;
|
||||
result = sC.invoke("Eval",args[0]);
|
||||
// call it twice to see the objects reused
|
||||
result = sC.invoke("Eval",args[0]);
|
||||
// call it 3 times to see the objects reused
|
||||
result = sC.invoke("Eval",args[0]);
|
||||
System.out.println("eval("+args[0]+") = "+ result);
|
||||
} catch (ComException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Integer I = null;
|
||||
for(int i=1;i<1000000;i++)
|
||||
{
|
||||
I = new Integer(i);
|
||||
}
|
||||
System.out.println(I);
|
||||
ComThread.Release();
|
||||
ComThread.quitMainSTA();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
19
unittest/com/jacob/test/vbscript/ScriptTestErrEvents.java
Normal file
19
unittest/com/jacob/test/vbscript/ScriptTestErrEvents.java
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
package com.jacob.test.vbscript;
|
||||
|
||||
import com.jacob.com.Variant;
|
||||
|
||||
/**
|
||||
* Extracted from ScriptTest so everyone can see this
|
||||
*/
|
||||
public class ScriptTestErrEvents {
|
||||
|
||||
public void Error(Variant[] args)
|
||||
{
|
||||
System.out.println("java callback for error!");
|
||||
}
|
||||
public void Timeout(Variant[] args)
|
||||
{
|
||||
System.out.println("java callback for error!");
|
||||
}
|
||||
}
|
||||
18
unittest/com/jacob/test/vbscript/speed.java
Normal file
18
unittest/com/jacob/test/vbscript/speed.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package com.jacob.test.vbscript;
|
||||
|
||||
import com.jacob.com.*;
|
||||
import com.jacob.activeX.*;
|
||||
|
||||
class speed
|
||||
{
|
||||
public static void main(String args[])
|
||||
{
|
||||
String lang = "VBScript";
|
||||
ActiveXComponent sC = new ActiveXComponent("ScriptControl");
|
||||
Dispatch sControl = sC.getObject();
|
||||
Dispatch.put(sControl, "Language", lang);
|
||||
for(int i=0;i<10000;i++) {
|
||||
Dispatch.call(sControl, "Eval", "1+1");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user