1340233 protect Dispatch constructor
1185167 Connect to running instance -- experimental API
This commit is contained in:
60
unittest/com/jacob/com/ActiveXComponentFactoryTest.java
Normal file
60
unittest/com/jacob/com/ActiveXComponentFactoryTest.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package com.jacob.com;
|
||||
|
||||
import com.jacob.activeX.ActiveXComponent;
|
||||
|
||||
/**
|
||||
* This exercises the two Dispatch factor methods that let you
|
||||
* control whether you create a new running COM object or connect to an existing one
|
||||
*
|
||||
* -Djava.library.path=d:/jacob/release -Dcom.jacob.autogc=false -Dcom.jacob.debug=true
|
||||
*
|
||||
* @author joe
|
||||
*
|
||||
*/
|
||||
public class ActiveXComponentFactoryTest {
|
||||
public static void main(String args[]) throws Exception {
|
||||
ComThread.InitSTA(true);
|
||||
try {
|
||||
System.out.println("This test only works if MS Word is NOT already running");
|
||||
String mApplicationId = "Word.Application";
|
||||
ActiveXComponent mTryConnectingFirst = ActiveXComponent.connectToActiveInstance(mApplicationId);
|
||||
if (mTryConnectingFirst != null ){
|
||||
mTryConnectingFirst.invoke("Quit",new Variant[] {});
|
||||
System.out.println("Was able to connect to MSWord when hadn't started it");
|
||||
} else {
|
||||
System.out.println("Correctly could not connect to running MSWord");
|
||||
}
|
||||
System.out.println(" Word Starting");
|
||||
ActiveXComponent mTryStartingSecond = ActiveXComponent.createNewInstance(mApplicationId);
|
||||
if (mTryStartingSecond == null){
|
||||
System.out.println("was unable to start up MSWord ");
|
||||
} else {
|
||||
System.out.println("Correctly could start MSWord");
|
||||
}
|
||||
ActiveXComponent mTryConnectingThird = ActiveXComponent.connectToActiveInstance(mApplicationId);
|
||||
if (mTryConnectingThird == null ){
|
||||
System.out.println("was unable able to connect to MSWord after previous startup");
|
||||
} else {
|
||||
System.out.println("Correctly could connect to running MSWord");
|
||||
// stop it so we can fail trying to connect to a running
|
||||
mTryConnectingThird.invoke("Quit",new Variant[] {});
|
||||
System.out.println(" Word stopped");
|
||||
}
|
||||
Thread.sleep(2000);
|
||||
ActiveXComponent mTryConnectingFourth = ActiveXComponent.connectToActiveInstance(mApplicationId);
|
||||
if (mTryConnectingFourth != null ){
|
||||
System.out.println("Was able to connect to MSWord that was stopped");
|
||||
mTryConnectingFourth.invoke("Quit",new Variant[] {});
|
||||
} else {
|
||||
System.out.println("Correctly could not connect to running MSWord");
|
||||
}
|
||||
} catch (ComException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
System.out.println("About to sleep for 2 seconds so we can bask in the glory of this success");
|
||||
Thread.sleep(2000);
|
||||
ComThread.Release();
|
||||
ComThread.quitMainSTA();
|
||||
}
|
||||
}
|
||||
}
|
||||
32
unittest/com/jacob/com/DispatchNullProgramId.java
Normal file
32
unittest/com/jacob/com/DispatchNullProgramId.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package com.jacob.com;
|
||||
|
||||
/**
|
||||
* This test verifies that the Dispatch object protects itself when
|
||||
* the constructor is called with a null program id.
|
||||
* Prior to this protection, the VM might crash.m
|
||||
* @author joe
|
||||
*
|
||||
*/
|
||||
public class DispatchNullProgramId {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
String nullParam = null;
|
||||
new Dispatch(nullParam);
|
||||
System.out.println(
|
||||
"the dispatch failed to protect itself from null program ids");
|
||||
} catch (IllegalArgumentException iae){
|
||||
System.out.println(
|
||||
"the dispatch protected itself from null program ids");
|
||||
}
|
||||
try {
|
||||
String nullParam = "";
|
||||
new Dispatch(nullParam);
|
||||
System.out.println(
|
||||
"the dispatch failed to protect itself from empty string program ids");
|
||||
} catch (IllegalArgumentException iae){
|
||||
System.out.println(
|
||||
"the dispatch protected itself from empty string program ids");
|
||||
}
|
||||
}
|
||||
}
|
||||
50
unittest/com/jacob/com/ExcelEventTest.java
Normal file
50
unittest/com/jacob/com/ExcelEventTest.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package com.jacob.com;
|
||||
|
||||
import com.jacob.activeX.ActiveXComponent;
|
||||
import com.jacob.com.ComFailException;
|
||||
import com.jacob.com.DispatchEvents;
|
||||
|
||||
/**
|
||||
* This test was lifted from a forum posting and shows how you can't listen to
|
||||
* Excel events added post 1.9.1 Eclipse Settings...
|
||||
* -Djava.library.path=d:/jacob/release -Dcom.jacob.autogc=false
|
||||
* -Dcom.jacob.debug=true
|
||||
*/
|
||||
public class ExcelEventTest {
|
||||
|
||||
public static void main(String args[]) {
|
||||
|
||||
listenTo("Word.Application",null);
|
||||
|
||||
// Create an Excel Listener
|
||||
listenTo("Excel.Application",
|
||||
"C:\\Program Files\\Microsoft Office\\OFFICE11\\EXCEL.EXE");
|
||||
}
|
||||
|
||||
private static void listenTo(String pid, String typeLibLocation) {
|
||||
|
||||
// Grab The Component.
|
||||
ActiveXComponent axc = new ActiveXComponent(pid);
|
||||
try {
|
||||
// Add a listener (doesn't matter what it is).
|
||||
DispatchEvents de;
|
||||
if (typeLibLocation == null){
|
||||
de = new DispatchEvents(axc, new ExcelEventTest());
|
||||
} else {
|
||||
de = new DispatchEvents(axc, new ExcelEventTest(),
|
||||
pid,typeLibLocation);
|
||||
}
|
||||
if (de == null){
|
||||
System.out.println(
|
||||
"No exception thrown but now dispatch returned for Excel events");
|
||||
}
|
||||
// Yea!
|
||||
System.out.println("Successfully attached to " + pid);
|
||||
} catch (ComFailException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("Failed to attach to " + pid + ": "
|
||||
+ e.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user