Here are the examples of the java api com.jacob.com.Dispatch taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
43 Examples
19
Source : MSTTSSpeech.java
with GNU General Public License v3.0
from unclezs
with GNU General Public License v3.0
from unclezs
/**
* windows 文本转语音工具
*
* @author https://blog.csdn.net/weixin_33873846/article/details/92559209
* @date 2020-5-17
*/
@Slf4j
public clreplaced MSTTSSpeech {
/**
* 声音:1到100
*/
private int volume = 100;
/**
* 频率:-10到10
*/
private int rate = 0;
/**
* 语音库序号
*/
private int voice = 0;
/**
* 输出设备序号
*/
private int audio = 0;
private ActiveXComponent ax;
/**
* 声音对象
*/
private Dispatch spVoice;
/**
* 音频格式对象
*/
private Dispatch spAudioFormat = null;
/**
* 音频输出对象
*/
private Dispatch dispatch = null;
/**
* 音频的输出格式,默认为:SAFT22kHz16BitMono
*/
private int formatType = 22;
public MSTTSSpeech() {
try {
ComThread.InitSTA();
ax = new ActiveXComponent("Sapi.SpVoice");
spVoice = ax.getObject();
} catch (Throwable e) {
log.error("初始化朗读声源失败:{}", e.getMessage());
}
}
/**
* 改变语音库
*
* @param voice 语音库序号
*/
public void changeVoice(int voice) {
if (this.voice != voice) {
this.voice = voice;
}
try {
Dispatch voiceItems = Dispatch.call(spVoice, "GetVoices").toDispatch();
int count = Integer.valueOf(Dispatch.call(voiceItems, "Count").toString());
if (count > 0) {
Dispatch voiceItem = Dispatch.call(voiceItems, "Item", new Variant(this.voice)).toDispatch();
Dispatch.put(spVoice, "Voice", voiceItem);
}
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
}
}
/**
* 改变音频输出设备
*
* @param audio 音频设备序号
*/
public void changeAudioOutput(int audio) {
if (this.audio != audio) {
this.audio = audio;
}
try {
Dispatch audioOutputs = Dispatch.call(spVoice, "GetAudioOutputs").toDispatch();
int count = Integer.valueOf(Dispatch.call(audioOutputs, "Count").toString());
if (count > 0) {
Dispatch audioOutput = Dispatch.call(audioOutputs, "Item", new Variant(this.audio)).toDispatch();
Dispatch.put(spVoice, "AudioOutput", audioOutput);
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
/**
* 播放语音
*
* @param text 要转换成语音的文本
*/
public void speak(String text) {
this.speak(text, 0);
}
/**
* 停止播放语音
*/
public void stop() {
Dispatch.call(spVoice, "Pause");
}
/**
* 播放语音
*
* @param text 要转换成语音的文本
* @param type 类型0:播放,1:停止
*/
private void speak(String text, int type) {
switch(type) {
case 0:
try {
// 调整音量和读的速度
Dispatch.put(spVoice, "Volume", new Variant(this.volume));
Dispatch.put(spVoice, "Rate", new Variant(this.rate));
// 设置音频格式类型
if (spAudioFormat == null) {
ax = new ActiveXComponent("Sapi.SpAudioFormat");
spAudioFormat = ax.getObject();
ax = new ActiveXComponent("Sapi.SpMMAudioOut");
dispatch = ax.getObject();
}
Dispatch.put(spAudioFormat, "Type", new Variant(this.formatType));
Dispatch.putRef(dispatch, "Format", spAudioFormat);
Dispatch.put(spVoice, "AllowAudioOutputFormatChangesOnNextSet", new Variant(false));
Dispatch.putRef(spVoice, "AudioOutputStream", dispatch);
// 开始朗读
Dispatch.call(spVoice, "Speak", new Variant(text));
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
break;
case 1:
try {
Dispatch.call(spVoice, "Speak", new Variant(text), new Variant(2));
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
break;
default:
break;
}
}
/**
* 获取系统中所有的语音库名称数组
*
* @return String[]
*/
public String[] getVoices() {
String[] voices = null;
try {
Dispatch voiceItems = Dispatch.call(spVoice, "GetVoices").toDispatch();
int count = Integer.valueOf(Dispatch.call(voiceItems, "Count").toString());
if (count > 0) {
voices = new String[count];
for (int i = 0; i < count; i++) {
Dispatch voiceItem = Dispatch.call(voiceItems, "Item", new Variant(i)).toDispatch();
String voice = Dispatch.call(voiceItem, "GetDescription").toString();
voices[i] = voice;
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return voices;
}
/**
* 获取音频输出设备名称数组
*
* @return String[]
*/
public String[] getAudioOutputs() {
String[] result = null;
try {
Dispatch audioOutputs = Dispatch.call(spVoice, "GetAudioOutputs").toDispatch();
int count = Integer.valueOf(Dispatch.call(audioOutputs, "Count").toString());
if (count > 0) {
result = new String[count];
for (int i = 0; i < count; i++) {
Dispatch voiceItem = Dispatch.call(audioOutputs, "Item", new Variant(i)).toDispatch();
String voice = Dispatch.call(voiceItem, "GetDescription").toString();
result[i] = voice;
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return result;
}
/**
* 将文字转换成音频信号,然后输出到.WAV文件
*
* @param text 文本字符串
* @param filePath 输出文件路径
*/
public void saveToWav(String text, String filePath) {
FileUtil.touch(filePath);
// 创建输出文件流对象
ax = new ActiveXComponent("Sapi.SpFileStream");
// 音频文件输出流对象,在读取或保存音频文件时使用
Dispatch spFileStream = ax.getObject();
// 创建音频流格式对象
if (spAudioFormat == null) {
ax = new ActiveXComponent("Sapi.SpAudioFormat");
spAudioFormat = ax.getObject();
}
// 设置音频流格式类型
Dispatch.put(spAudioFormat, "Type", new Variant(this.formatType));
// 设置文件输出流的格式
Dispatch.putRef(spFileStream, "Format", spAudioFormat);
// 调用输出文件流对象的打开方法,创建一个.wav文件
Dispatch.call(spFileStream, "Open", new Variant(filePath), new Variant(3), new Variant(true));
// 设置声音对象的音频输出流为输出文件流对象
Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
// 调整音量和读的速度
Dispatch.put(spVoice, "Volume", new Variant(this.volume));
Dispatch.put(spVoice, "Rate", new Variant(this.rate));
// 开始朗读
Dispatch.call(spVoice, "Speak", new Variant(text));
// 关闭输出文件流对象,释放资源
Dispatch.call(spFileStream, "Close");
Dispatch.putRef(spVoice, "AudioOutputStream", null);
}
/**
* @return the volume
*/
public int getVolume() {
return volume;
}
/**
* @param volume the volume to set
*/
public void setVolume(int volume) {
this.volume = volume;
}
/**
* @return the rate
*/
public int getRate() {
return rate;
}
/**
* @param rate the rate to set
*/
public void setRate(int rate) {
this.rate = rate;
}
/**
* @return the voice
*/
public int getVoice() {
return voice;
}
/**
* @param voice the voice to set
*/
public void setVoice(int voice) {
this.voice = voice;
}
/**
* @return the audio
*/
public int getAudio() {
return audio;
}
/**
* @param audio the audio to set
*/
public void setAudio(int audio) {
this.audio = audio;
}
/**
* @return the ax
*/
public ActiveXComponent getAx() {
return ax;
}
/**
* @param ax the ax to set
*/
public void setAx(ActiveXComponent ax) {
this.ax = ax;
}
/**
* @return the formatType
*/
public int getFormatType() {
return formatType;
}
/**
* 设置音频输出格式类型<br>
* SAFTDefault = -1<br>
* SAFTNoreplacedignedFormat = 0<br>
* SAFTText = 1<br>
* SAFTNonStandardFormat = 2<br>
* SAFTExtendedAudioFormat = 3<br>
* // Standard PCM wave formats<br>
* SAFT8kHz8BitMono = 4<br>
* SAFT8kHz8BitStereo = 5<br>
* SAFT8kHz16BitMono = 6<br>
* SAFT8kHz16BitStereo = 7<br>
* SAFT11kHz8BitMono = 8<br>
* SAFT11kHz8BitStereo = 9<br>
* SAFT11kHz16BitMono = 10<br>
* SAFT11kHz16BitStereo = 11<br>
* SAFT12kHz8BitMono = 12<br>
* SAFT12kHz8BitStereo = 13<br>
* SAFT12kHz16BitMono = 14<br>
* SAFT12kHz16BitStereo = 15<br>
* SAFT16kHz8BitMono = 16<br>
* SAFT16kHz8BitStereo = 17<br>
* SAFT16kHz16BitMono = 18<br>
* SAFT16kHz16BitStereo = 19<br>
* SAFT22kHz8BitMono = 20<br>
* SAFT22kHz8BitStereo = 21<br>
* SAFT22kHz16BitMono = 22<br>
* SAFT22kHz16BitStereo = 23<br>
* SAFT24kHz8BitMono = 24<br>
* SAFT24kHz8BitStereo = 25<br>
* SAFT24kHz16BitMono = 26<br>
* SAFT24kHz16BitStereo = 27<br>
* SAFT32kHz8BitMono = 28<br>
* SAFT32kHz8BitStereo = 29<br>
* SAFT32kHz16BitMono = 30<br>
* SAFT32kHz16BitStereo = 31<br>
* SAFT44kHz8BitMono = 32<br>
* SAFT44kHz8BitStereo = 33<br>
* SAFT44kHz16BitMono = 34<br>
* SAFT44kHz16BitStereo = 35<br>
* SAFT48kHz8BitMono = 36<br>
* SAFT48kHz8BitStereo = 37<br>
* SAFT48kHz16BitMono = 38<br>
* SAFT48kHz16BitStereo = 39<br>
* <br>
* // TrueSpeech format<br>
* SAFTTrueSpeech_8kHz1BitMono = 40<br>
* // A-Law formats<br>
* SAFTCCITT_ALaw_8kHzMono = 41<br>
* SAFTCCITT_ALaw_8kHzStereo = 42<br>
* SAFTCCITT_ALaw_11kHzMono = 43<br>
* SAFTCCITT_ALaw_11kHzStereo = 4<br>
* SAFTCCITT_ALaw_22kHzMono = 44<br>
* SAFTCCITT_ALaw_22kHzStereo = 45<br>
* SAFTCCITT_ALaw_44kHzMono = 46<br>
* SAFTCCITT_ALaw_44kHzStereo = 47<br>
* <br>
* // u-Law formats<br>
* SAFTCCITT_uLaw_8kHzMono = 48<br>
* SAFTCCITT_uLaw_8kHzStereo = 49<br>
* SAFTCCITT_uLaw_11kHzMono = 50<br>
* SAFTCCITT_uLaw_11kHzStereo = 51<br>
* SAFTCCITT_uLaw_22kHzMono = 52<br>
* SAFTCCITT_uLaw_22kHzStereo = 53<br>
* SAFTCCITT_uLaw_44kHzMono = 54<br>
* SAFTCCITT_uLaw_44kHzStereo = 55<br>
* SAFTADPCM_8kHzMono = 56<br>
* SAFTADPCM_8kHzStereo = 57<br>
* SAFTADPCM_11kHzMono = 58<br>
* SAFTADPCM_11kHzStereo = 59<br>
* SAFTADPCM_22kHzMono = 60<br>
* SAFTADPCM_22kHzStereo = 61<br>
* SAFTADPCM_44kHzMono = 62<br>
* SAFTADPCM_44kHzStereo = 63<br>
* <br>
* // GSM 6.10 formats<br>
* SAFTGSM610_8kHzMono = 64<br>
* SAFTGSM610_11kHzMono = 65<br>
* SAFTGSM610_22kHzMono = 66<br>
* SAFTGSM610_44kHzMono = 67<br>
* // Other formats<br>
* SAFTNUM_FORMATS = 68<br>
*
* @param formatType 音频输出格式类型
*/
public void setFormatType(int formatType) {
this.formatType = formatType;
}
public static void main(String[] args) {
MSTTSSpeech speech = new MSTTSSpeech();
String text = FileUtil.readUtf8String("D:\\java\\NovelHarvester\\1.txt");
speech.setFormatType(6);
// speech.setRate(-1);
speech.saveToWav(text, "D:\\java\\NovelHarvester\\test.wav");
}
}
19
Source : MSTTSSpeech.java
with GNU General Public License v3.0
from unclezs
with GNU General Public License v3.0
from unclezs
/**
* 获取音频输出设备名称数组
*
* @return String[]
*/
public String[] getAudioOutputs() {
String[] result = null;
try {
Dispatch audioOutputs = Dispatch.call(spVoice, "GetAudioOutputs").toDispatch();
int count = Integer.valueOf(Dispatch.call(audioOutputs, "Count").toString());
if (count > 0) {
result = new String[count];
for (int i = 0; i < count; i++) {
Dispatch voiceItem = Dispatch.call(audioOutputs, "Item", new Variant(i)).toDispatch();
String voice = Dispatch.call(voiceItem, "GetDescription").toString();
result[i] = voice;
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return result;
}
19
Source : MSTTSSpeech.java
with GNU General Public License v3.0
from unclezs
with GNU General Public License v3.0
from unclezs
/**
* 将文字转换成音频信号,然后输出到.WAV文件
*
* @param text 文本字符串
* @param filePath 输出文件路径
*/
public void saveToWav(String text, String filePath) {
FileUtil.touch(filePath);
// 创建输出文件流对象
ax = new ActiveXComponent("Sapi.SpFileStream");
// 音频文件输出流对象,在读取或保存音频文件时使用
Dispatch spFileStream = ax.getObject();
// 创建音频流格式对象
if (spAudioFormat == null) {
ax = new ActiveXComponent("Sapi.SpAudioFormat");
spAudioFormat = ax.getObject();
}
// 设置音频流格式类型
Dispatch.put(spAudioFormat, "Type", new Variant(this.formatType));
// 设置文件输出流的格式
Dispatch.putRef(spFileStream, "Format", spAudioFormat);
// 调用输出文件流对象的打开方法,创建一个.wav文件
Dispatch.call(spFileStream, "Open", new Variant(filePath), new Variant(3), new Variant(true));
// 设置声音对象的音频输出流为输出文件流对象
Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
// 调整音量和读的速度
Dispatch.put(spVoice, "Volume", new Variant(this.volume));
Dispatch.put(spVoice, "Rate", new Variant(this.rate));
// 开始朗读
Dispatch.call(spVoice, "Speak", new Variant(text));
// 关闭输出文件流对象,释放资源
Dispatch.call(spFileStream, "Close");
Dispatch.putRef(spVoice, "AudioOutputStream", null);
}
19
Source : MSTTSSpeech.java
with GNU General Public License v3.0
from unclezs
with GNU General Public License v3.0
from unclezs
/**
* 改变音频输出设备
*
* @param audio 音频设备序号
*/
public void changeAudioOutput(int audio) {
if (this.audio != audio) {
this.audio = audio;
}
try {
Dispatch audioOutputs = Dispatch.call(spVoice, "GetAudioOutputs").toDispatch();
int count = Integer.valueOf(Dispatch.call(audioOutputs, "Count").toString());
if (count > 0) {
Dispatch audioOutput = Dispatch.call(audioOutputs, "Item", new Variant(this.audio)).toDispatch();
Dispatch.put(spVoice, "AudioOutput", audioOutput);
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
19
Source : MSTTSSpeech.java
with GNU General Public License v3.0
from unclezs
with GNU General Public License v3.0
from unclezs
/**
* 改变语音库
*
* @param voice 语音库序号
*/
public void changeVoice(int voice) {
if (this.voice != voice) {
this.voice = voice;
}
try {
Dispatch voiceItems = Dispatch.call(spVoice, "GetVoices").toDispatch();
int count = Integer.valueOf(Dispatch.call(voiceItems, "Count").toString());
if (count > 0) {
Dispatch voiceItem = Dispatch.call(voiceItems, "Item", new Variant(this.voice)).toDispatch();
Dispatch.put(spVoice, "Voice", voiceItem);
}
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
}
}
19
Source : MSTTSSpeech.java
with GNU General Public License v3.0
from unclezs
with GNU General Public License v3.0
from unclezs
/**
* 获取系统中所有的语音库名称数组
*
* @return String[]
*/
public String[] getVoices() {
String[] voices = null;
try {
Dispatch voiceItems = Dispatch.call(spVoice, "GetVoices").toDispatch();
int count = Integer.valueOf(Dispatch.call(voiceItems, "Count").toString());
if (count > 0) {
voices = new String[count];
for (int i = 0; i < count; i++) {
Dispatch voiceItem = Dispatch.call(voiceItems, "Item", new Variant(i)).toDispatch();
String voice = Dispatch.call(voiceItem, "GetDescription").toString();
voices[i] = voice;
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return voices;
}
19
Source : ImportOutlookContactsDialog.java
with GNU General Public License v2.0
from jfritz-org
with GNU General Public License v2.0
from jfritz-org
public String getContactPic(Dispatch item) {
// $NON-NLS-1$
return "";
}
19
Source : ScriptTest3.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* 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.
* <p>
* May need to run with some command line options (including from inside
* Eclipse). Look in the docs area at the Jacob usage doreplacedent for command line
* options.
*/
public clreplaced ScriptTest3 extends BaseTestCase {
public static ActiveXComponent sC;
public static DispatchEvents de = null;
public static Dispatch sControl = null;
public static boolean quit = false;
public void testScript() {
try {
ComThread.InitMTA();
ScriptTest3Inner script = new ScriptTest3Inner();
script.start();
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
// should we get this?
}
Variant result = Dispatch.call(sControl, "Eval", getSampleVPScriptForEval());
System.out.println("eval(" + getSampleVPScriptForEval() + ") = " + result);
System.out.println("setting quit");
ScriptTest3.quit = true;
} catch (ComException e) {
e.printStackTrace();
fail("Caught excpetion running script with MTA");
} finally {
System.out.println("main done");
ComThread.Release();
}
}
clreplaced ScriptTest3Inner extends Thread {
public void run() {
try {
ComThread.InitMTA();
System.out.println("OnInit");
String lang = "VBScript";
sC = new ActiveXComponent("ScriptControl");
sControl = 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");
}
}
}
}
19
Source : ScriptTest2.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
public void testScript2() {
try {
ComThread.InitSTA();
ScriptTestSTA script = new ScriptTestSTA();
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
// should we get this?
}
String scriptCommand = getSampleVPScriptForEval();
// get a thread-local Dispatch from sCon
Dispatch sc = script.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", scriptCommand);
System.out.println("eval(" + scriptCommand + ") = " + result);
script.quit();
System.out.println("called quit");
} catch (ComException e) {
e.printStackTrace();
fail("caught exception" + e);
} finally {
Integer I = null;
for (int i = 1; i < 1000000; i++) {
I = new Integer(i);
}
System.out.println(I);
ComThread.Release();
}
}
19
Source : ScriptTest.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
public void testStupidSpeedTest() {
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");
}
}
19
Source : ScriptTest.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
public void testCreatingDispatchEvents() {
ComThread.InitSTA(true);
DispatchEvents de = null;
Dispatch sControl = null;
try {
String scriptCommand = getSampleVPScriptForEval();
String lang = "VBScript";
ActiveXComponent sC = new ActiveXComponent("ScriptControl");
sControl = 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", scriptCommand);
// call it twice to see the objects reused
result = Dispatch.call(sControl, "Eval", scriptCommand);
// call it 3 times to see the objects reused
result = Dispatch.call(sControl, "Eval", scriptCommand);
System.out.println("eval(" + scriptCommand + ") = " + result);
} catch (ComException e) {
e.printStackTrace();
fail("Caught Exception " + e);
} finally {
Integer I = null;
for (int i = 1; i < 1000000; i++) {
I = new Integer(i);
}
System.out.println(I);
ComThread.Release();
ComThread.quitMainSTA();
}
}
19
Source : SafeArrayViaExcel.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* verify safe arrays work with standard applications, Excel in this case
*/
public void testSafeArrayViaExcel() {
ActiveXComponent xl = new ActiveXComponent("Excel.Application");
try {
Dispatch cell;
SafeArray sAProdText;
Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();
System.out.println("have workbooks");
Dispatch workbook = Dispatch.call(workbooks, "Open", getWindowsFilePathToPackageResource("SafeArrayViaExcel.xls", this.getClreplaced())).toDispatch();
System.out.println("Opened File - SafeArrayViaExcel.xls\n");
Dispatch sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
cell = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { "A1:D1000" }, new int[1]).toDispatch();
System.out.println("have cell:" + cell);
sAProdText = Dispatch.get(cell, "Value").toSafeArray();
System.out.println("sa: dim=" + sAProdText.getNumDim());
System.out.println("sa: start row=" + sAProdText.getLBound(1));
System.out.println("sa: start col=" + sAProdText.getLBound(2));
System.out.println("sa: end row=" + sAProdText.getUBound(1));
System.out.println("sa: end col=" + sAProdText.getUBound(2));
int i;
int lineNumber = 1;
int n = 0;
for (lineNumber = 1; lineNumber < 1000; lineNumber++) {
for (i = 1; i < 4; i++) {
System.out.println((n++) + " " + lineNumber + " " + i + " " + sAProdText.getString(lineNumber, i));
/*
* if (sAProdText.getString(lineNumber,i).compareTo("aaaa") !=
* 0 ) { System.out.println("Invalid String in line " +
* lineNumber + " Cell " + i + " Value = " +
* sAProdText.getString(lineNumber,i)); stringFound = false; } }
* if (stringFound) { System.out.println("Valid Strings in
* line " + lineNumber); lineNumber++; }
*/
}
}
Dispatch.call(workbook, "Close");
System.out.println("Closed File\n");
} catch (Exception e) {
e.printStackTrace();
fail("Caught Exception " + e);
} finally {
xl.invoke("Quit", new Variant[] {});
}
}
19
Source : SafeArrayDispatchTest.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
public void testDispatchWithSafeArray() {
try {
String scriptCommand = "1+(2*4)-3";
String lang = "VBScript";
ActiveXComponent sControl = new ActiveXComponent("ScriptControl");
Dispatch.put(sControl, "Language", lang);
Variant result = Dispatch.call(sControl, "Eval", scriptCommand);
replacedertTrue(result.toString().equals("6"));
// wrap the script control in a variant
Variant v = new Variant(sControl);
// create a safe array of type dispatch
SafeArray sa = new SafeArray(Variant.VariantDispatch, 1);
// put the variant in the array
sa.setVariant(0, v);
// take it back out
Variant v2 = sa.getVariant(0);
Dispatch d = v2.toDispatch();
// make sure you can call eval on it
result = Dispatch.call(d, "Eval", scriptCommand);
replacedertTrue(result.toString().equals("6"));
} catch (ComException e) {
e.printStackTrace();
fail("script failure " + e);
}
}
19
Source : PowerpointTest.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* main program that lets us run this as a test
*
* @param args
*/
public void testPowerpoint() {
ComThread.InitMTA();
ActiveXComponent component = new ActiveXComponent("Powerpoint.Application");
Dispatch comPowerpoint = component.getObject();
try {
PowerpointTestThread[] threads = new PowerpointTestThread[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++) {
threads[i] = new PowerpointTestThread(i + 1, comPowerpoint);
threads[i].start();
}
boolean allThreadsFinished = false;
while (!allThreadsFinished) {
allThreadsFinished = true;
for (int i = 0; i < NUM_THREADS; i++) {
if (threads[i].isAlive()) {
allThreadsFinished = false;
break;
}
}
if (!allThreadsFinished) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// no op
}
}
}
Dispatch.call(comPowerpoint, "Quit");
for (int i = 0; i < NUM_THREADS; i++) {
if (threads[i].threadFailedWithException != null) {
fail("caught unexpected exception in thread " + threads[i].threadFailedWithException);
}
}
} finally {
ComThread.Release();
}
}
19
Source : ActiveXComponent.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* sets a property on this object
*
* @param propertyName
* property name
* @param arg
* variant value to be set
*/
public void setProperty(String propertyName, Dispatch arg) {
Dispatch.put(this, propertyName, arg);
}
19
Source : ActiveXComponent.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* used by the doc and application listeners to get intelligent logging
*
* @param description
* event description
* @param args
* args preplaceded in (variants)
*/
public void logCallbackEvent(String description, Variant[] args) {
String argString = "";
if (args != null && ActiveXComponent.shouldLogEvents) {
if (args.length > 0) {
argString += " args: ";
}
for (int i = 0; i < args.length; i++) {
short argType = args[i].getvt();
argString += ",[" + i + "]";
// break out the byref bits if they are on this
if ((argType & Variant.VariantByref) == Variant.VariantByref) {
// show the type and the fact that its byref
argString += "(" + (args[i].getvt() & ~Variant.VariantByref) + "/" + Variant.VariantByref + ")";
} else {
// show the type
argString += "(" + argType + ")";
}
argString += "=";
if (argType == Variant.VariantDispatch) {
Dispatch foo = (args[i].getDispatch());
argString += foo;
} else if ((argType & Variant.VariantBoolean) == Variant.VariantBoolean) {
// do the boolean thing
if ((argType & Variant.VariantByref) == Variant.VariantByref) {
// boolean by ref
argString += args[i].getBooleanRef();
} else {
// boolean by value
argString += args[i].getBoolean();
}
} else if ((argType & Variant.VariantString) == Variant.VariantString) {
// do the string thing
if ((argType & Variant.VariantByref) == Variant.VariantByref) {
// string by ref
argString += args[i].getStringRef();
} else {
// string by value
argString += args[i].getString();
}
} else {
argString += args[i].toString();
}
}
System.out.println(description + argString);
}
}
19
Source : SystemMonitor.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* example run loop method called by main()
*/
public void runMonitor() {
ActiveXComponent wmi = null;
wmi = new ActiveXComponent("WbemScripting.SWbemLocator");
// no connection parameters means to connect to the local machine
Variant conRet = wmi.invoke("ConnectServer");
// the author liked the ActiveXComponent api style over the Dispatch
// style
ActiveXComponent wmiconnect = new ActiveXComponent(conRet.toDispatch());
// the WMI supports a query language.
String query = "select CategoryString, Message, TimeGenerated, User, Type " + "from Win32_NtLogEvent " + "where Logfile = 'Application' and TimeGenerated > '20070915000000.000000-***'";
Variant vCollection = wmiconnect.invoke("ExecQuery", new Variant(query));
EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
String resultString = "";
Dispatch item = null;
while (enumVariant.hasMoreElements()) {
resultString = "";
item = enumVariant.nextElement().toDispatch();
String categoryString = Dispatch.call(item, "CategoryString").toString();
String messageString = Dispatch.call(item, "Message").toString();
String timeGenerated = Dispatch.call(item, "TimeGenerated").toString();
String eventUser = Dispatch.call(item, "User").toString();
String eventType = Dispatch.call(item, "Type").toString();
resultString += "TimeGenerated: " + timeGenerated + " Category: " + categoryString + " User: " + eventUser + " EventType: " + eventType + " Message:" + messageString;
System.out.println(resultString);
}
}
19
Source : DiskUtils.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* Example VB script that grabs hard drive properties.
* <p>
* Source Forge posting
* http://sourceforge.net/forum/forum.php?thread_id=1785936&forum_id=375946
* <p>
* Enhance by clay_shooter with info from
* http://msdn2.microsoft.com/en-us/library/d6dw7aeh.aspx
*
* @author qstephenson
*/
public clreplaced DiskUtils {
/**
* formatters aren't thread safe but the sample only has one thread
*/
private static DecimalFormat sizeFormatter = new DecimalFormat("###,###,###,###");
/**
* a pointer to the scripting file system object
*/
private ActiveXComponent fileSystemApp = null;
/**
* the dispatch that points at the drive this DiskUtil operates against
*/
private Dispatch myDrive = null;
/**
* Standard constructor
*
* @param drive
* the drive to run the test against.
*/
public DiskUtils(String drive) {
setUp(drive);
}
/**
* open the connection to the scripting object
*
* @param drive
* the drive to run the test against
*/
public void setUp(String drive) {
if (fileSystemApp == null) {
ComThread.InitSTA();
fileSystemApp = new ActiveXComponent("Scripting.FileSystemObject");
myDrive = Dispatch.call(fileSystemApp, "GetDrive", drive).toDispatch();
}
}
/**
* Do any needed cleanup
*/
public void tearDown() {
ComThread.Release();
}
/**
* convenience method
*
* @return driver serial number
*/
public int getSerialNumber() {
return Dispatch.get(myDrive, "SerialNumber").getInt();
}
/**
* Convenience method. We go through these formatting hoops so we can make
* the size string pretty. We wouldn't have to do that if we didn't mind
* long strings with Exxx at the end or the fact that the value returned can
* vary in size based on the size of the disk.
*
* @return driver total size of the disk
*/
public String getTotalSize() {
Variant returnValue = Dispatch.get(myDrive, "TotalSize");
if (returnValue.getvt() == Variant.VariantDouble) {
return sizeFormatter.format(returnValue.getDouble());
} else if (returnValue.getvt() == Variant.VariantInt) {
return sizeFormatter.format(returnValue.getInt());
} else {
return "Don't know type: " + returnValue.getvt();
}
}
/**
* Convenience method. We wouldn't have to do that if we didn't mind long
* strings with Exxx at the end or the fact that the value returned can vary
* in size based on the size of the disk.
*
* @return driver free size of the disk
*/
public String getFreeSpace() {
Variant returnValue = Dispatch.get(myDrive, "FreeSpace");
if (returnValue.getvt() == Variant.VariantDouble) {
return sizeFormatter.format(returnValue.getDouble());
} else if (returnValue.getvt() == Variant.VariantInt) {
return sizeFormatter.format(returnValue.getInt());
} else {
return "Don't know type: " + returnValue.getvt();
}
}
/**
* @return file system on the drive
*/
public String getFileSystemType() {
// figure ot the actual variant type
// Variant returnValue = Dispatch.get(myDrive, "FileSystem");
// System.out.println(returnValue.getvt());
return Dispatch.get(myDrive, "FileSystem").getString();
}
/**
* @return volume name
*/
public String getVolumeName() {
return Dispatch.get(myDrive, "VolumeName").getString();
}
/**
* Simple main program that creates a DiskUtils object and queries for the
* C: drive
*
* @param args
* standard command line arguments
*/
public static void main(String[] args) {
// DiskUtils utilConnection = new DiskUtils("F");
DiskUtils utilConnection = new DiskUtils("C");
System.out.println("Disk serial number is: " + utilConnection.getSerialNumber());
System.out.println("FileSystem is: " + utilConnection.getFileSystemType());
System.out.println("Volume Name is: " + utilConnection.getVolumeName());
System.out.println("Disk total size is: " + utilConnection.getTotalSize());
System.out.println("Disk free space is: " + utilConnection.getFreeSpace());
utilConnection.tearDown();
}
}
19
Source : Outlook.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* standard run loop
*
* @param asArgs
* command line arguments
* @throws Exception
*/
public static void main(String[] asArgs) throws Exception {
System.out.println("Outlook: IN");
ActiveXComponent axOutlook = new ActiveXComponent("Outlook.Application");
try {
System.out.println("version=" + axOutlook.getProperty("Version"));
Dispatch oOutlook = axOutlook.getObject();
System.out.println("version=" + Dispatch.get(oOutlook, "Version"));
Dispatch oNameSpace = axOutlook.getProperty("Session").toDispatch();
System.out.println("oNameSpace=" + oNameSpace);
recurseFolders(0, oNameSpace);
} finally {
axOutlook.invoke("Quit", new Variant[] {});
}
}
19
Source : Outlook.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
private static void recurseFolders(int iIndent, Dispatch o) {
if (o == null) {
return;
}
Dispatch oFolders = Dispatch.get(o, "Folders").toDispatch();
// System.out.println("oFolders=" + oFolders);
if (oFolders == null) {
return;
}
Dispatch oFolder = Dispatch.get(oFolders, "GetFirst").toDispatch();
do {
Object oFolderName = Dispatch.get(oFolder, "Name");
if (null == oFolderName) {
break;
}
System.out.println(pad(iIndent) + oFolderName);
recurseFolders(iIndent + 3, oFolder);
oFolder = Dispatch.get(oFolders, "GetNext").toDispatch();
} while (true);
}
19
Source : WordDocumentProperties.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* Submitted to the Jacob SourceForge web site as a sample 3/2005
* <p>
* This sample is BROKEN because it doesn't call quit!
*
* @author Date Created Description Jason Twist 04 Mar 2005 Code opens a locally
* stored Word doreplacedent and extracts the Built In properties and Custom
* properties from it. This code just gives an intro to JACOB and there
* are sections that could be enhanced
*/
public clreplaced WordDoreplacedentProperties {
// Declare word object
private ActiveXComponent objWord;
// Declare Word Properties
private Dispatch custDocprops;
private Dispatch builtInDocProps;
// the doucments object is important in any real app but this demo doesn't
// use it
// private Dispatch doreplacedents;
private Dispatch doreplacedent;
private Dispatch wordObject;
/**
* Empty Constructor
*/
public WordDoreplacedentProperties() {
}
/**
* Opens a doreplacedent
*
* @param filename
*/
public void open(String filename) {
// Instantiate objWord
objWord = new ActiveXComponent("Word.Application");
// replacedign a local word object
wordObject = objWord.getObject();
// Create a Dispatch Parameter to hide the doreplacedent that is opened
Dispatch.put(wordObject, "Visible", new Variant(false));
// Instantiate the Doreplacedents Property
Dispatch doreplacedents = objWord.getProperty("Doreplacedents").toDispatch();
// Open a word doreplacedent, Current Active Doreplacedent
doreplacedent = Dispatch.call(doreplacedents, "Open", filename).toDispatch();
}
/**
* Creates an instance of the VBA CustomDoreplacedentProperties property
*/
public void selectCustomDoreplacedentProperitiesMode() {
// Create CustomDoreplacedentProperties and BuiltInDoreplacedentProperties
// properties
custDocprops = Dispatch.get(doreplacedent, "CustomDoreplacedentProperties").toDispatch();
}
/**
* Creates an instance of the VBA BuiltInDoreplacedentProperties property
*/
public void selectBuiltinPropertiesMode() {
// Create CustomDoreplacedentProperties and BuiltInDoreplacedentProperties
// properties
builtInDocProps = Dispatch.get(doreplacedent, "BuiltInDoreplacedentProperties").toDispatch();
}
/**
* Closes a doreplacedent
*/
public void close() {
// Close object
Dispatch.call(doreplacedent, "Close");
}
/**
* Custom Property Name is preplaceded in
*
* @param cusPropName
* @return String - Custom property value
*/
public String getCustomProperty(String cusPropName) {
try {
cusPropName = Dispatch.call(custDocprops, "Item", cusPropName).toString();
} catch (ComException e) {
// Do nothing
cusPropName = null;
}
return cusPropName;
}
/**
* Built In Property Name is preplaceded in
*
* @param builtInPropName
* @return String - Built in property value
*/
public String getBuiltInProperty(String builtInPropName) {
try {
builtInPropName = Dispatch.call(builtInDocProps, "Item", builtInPropName).toString();
} catch (ComException e) {
// Do nothing
builtInPropName = null;
}
return builtInPropName;
}
/**
* simple main program that gets some properties and prints them out
*
* @param args
*/
public static void main(String[] args) {
try {
// Instantiate the clreplaced
WordDoreplacedentProperties jacTest = new WordDoreplacedentProperties();
// Open the word doc
File doc = new File("samples/com/jacob/samples/office/TestDoreplacedent.doc");
jacTest.open(doc.getAbsolutePath());
// Set Custom Properties
jacTest.selectCustomDoreplacedentProperitiesMode();
// Set Built In Properties
jacTest.selectBuiltinPropertiesMode();
// Get custom Property Value
String custValue = jacTest.getCustomProperty("Information Source");
// Get built in prroperty Property Value
String builtInValue = jacTest.getBuiltInProperty("Author");
// Close Word Doc
jacTest.close();
// Output data
System.out.println("Doreplacedent Val One: " + custValue);
System.out.println("Doreplacedent Author: " + builtInValue);
} catch (Exception e) {
System.out.println(e);
}
}
}
19
Source : WordDocumentProperties.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* Opens a doreplacedent
*
* @param filename
*/
public void open(String filename) {
// Instantiate objWord
objWord = new ActiveXComponent("Word.Application");
// replacedign a local word object
wordObject = objWord.getObject();
// Create a Dispatch Parameter to hide the doreplacedent that is opened
Dispatch.put(wordObject, "Visible", new Variant(false));
// Instantiate the Doreplacedents Property
Dispatch doreplacedents = objWord.getProperty("Doreplacedents").toDispatch();
// Open a word doreplacedent, Current Active Doreplacedent
doreplacedent = Dispatch.call(doreplacedents, "Open", filename).toDispatch();
}
19
Source : VisioPrintTest.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* Runs the print ant lets the user say ok or cancel. Note the funky Visio
* behavior if someone hits the cancel button
*/
public void testPrintDialog() {
ActiveXComponent oActiveX = new ActiveXComponent("Visio.Application");
Dispatch oDoreplacedents = oActiveX.getProperty("Doreplacedents").toDispatch();
// create a blank doreplacedent
Dispatch.call(oDoreplacedents, "Add", "");
try {
Dispatch.call(oActiveX, "DoCmd", new Integer(1010)).getInt();
System.out.println("User hit the ok button.");
} catch (ComFailException e) {
System.out.println("User hit the cancel button: " + e);
} finally {
oActiveX.invoke("Quit");
}
return;
}
19
Source : MathTest.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* not clear why we need a clreplaced and run method but that's the way it was
* written
*/
public void runTest() {
// deprecated
// System.runFinalizersOnExit(true);
Dispatch test = new ActiveXComponent("MathTest.Math");
TestEvents te = new TestEvents();
DispatchEvents de = new DispatchEvents(test, te);
if (de == null) {
System.out.println("null returned when trying to create DispatchEvents");
}
System.out.println(Dispatch.call(test, "Add", new Variant(1), new Variant(2)));
System.out.println(Dispatch.call(test, "Mult", new Variant(2), new Variant(2)));
Variant v = Dispatch.call(test, "Mult", new Variant(2), new Variant(2));
// this should return false
System.out.println("v.isNull=" + v.isNull());
v = Dispatch.call(test, "getNothing");
// these should return nothing
System.out.println("v.isNull=" + v.isNull());
System.out.println("v.toDispatch=" + v.toDispatch());
}
19
Source : MultiFaceTest.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* standard main() test program
*
* @param args
* the command line arguments
*/
public static void main(String[] args) {
// this method has been deprecated as being unreliable.
// shutdown should be done through other means
// whoever wrote this example should explain what this was intended to
// do
// System.runFinalizersOnExit(true);
ActiveXComponent mf = new ActiveXComponent("MultiFace.Face");
try {
// I am now dealing with the default interface (IFace1)
Dispatch.put(mf, "Face1Name", new Variant("Hello Face1"));
System.out.println(Dispatch.get(mf, "Face1Name"));
// get to IFace2 through the IID
Dispatch f2 = mf.QueryInterface("{9BF24410-B2E0-11D4-A695-00104BFF3241}");
// I am now dealing with IFace2
Dispatch.put(f2, "Face2Nam", new Variant("Hello Face2"));
System.out.println(Dispatch.get(f2, "Face2Nam"));
// get to IFace3 through the IID
Dispatch f3 = mf.QueryInterface("{9BF24411-B2E0-11D4-A695-00104BFF3241}");
// I am now dealing with IFace3
Dispatch.put(f3, "Face3Name", new Variant("Hello Face3"));
System.out.println(Dispatch.get(f3, "Face3Name"));
} catch (Exception e) {
e.printStackTrace();
}
}
19
Source : Access.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* should return ?? for the preplaceded in ??
*
* @param qd
* @return Variant results of query?
*/
public static Variant getByQueryDef(Dispatch qd) {
// get a reference to the recordset
Dispatch recset = Dispatch.call(qd, "OpenRecordset").toDispatch();
// get the values as a safe array
String[] cols = getColumns(recset);
for (int i = 0; i < cols.length; i++) {
System.out.print(cols[i] + " ");
}
System.out.println("");
Variant vals = getValues(recset);
return vals;
}
19
Source : Access.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* Close a database
*
* @param openDB
* db to be closed
*/
public static void close(Dispatch openDB) {
Dispatch.call(openDB, "Close");
}
19
Source : Access.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* Extract the values from the recordset
*
* @param recset
* @return Variant that is the returned values
*/
public static Variant getValues(Dispatch recset) {
Dispatch.callSub(recset, "moveFirst");
Variant vi = new Variant(4096);
Variant v = Dispatch.call(recset, "GetRows", vi);
return v;
}
19
Source : IiTunes.java
with GNU General Public License v3.0
from 5zig-reborn
with GNU General Public License v3.0
from 5zig-reborn
/**
* Returns the currently targetd track.
*
* @return An ITTrack object corresponding to the currently targeted track.
* Will be set to NULL if there is no currently targeted track.
*/
public IITTrack getCurrentTrack() {
Variant variant = iTunes.getProperty("CurrentTrack");
if (variant.isNull()) {
return null;
}
Dispatch item = variant.toDispatch();
IITTrack track = new IITTrack(item);
if (track.getKind() == IITTrackKind.ITTrackKindFile) {
return new IITFileOrCDTrack(item);
} else if (track.getKind() == IITTrackKind.ITTrackKindCD) {
return new IITFileOrCDTrack(item);
} else if (track.getKind() == IITTrackKind.ITTrackKindURL) {
return new ITURLTrack(item);
} else {
return track;
}
}
19
Source : IITTrack.java
with GNU General Public License v3.0
from 5zig-reborn
with GNU General Public License v3.0
from 5zig-reborn
/**
* @return a collection containing the artwork for the track.
*/
public IITArtworkCollection getArtwork() {
Dispatch art = Dispatch.get(object, "Artwork").toDispatch();
return new IITArtworkCollection(art);
}
19
Source : IITObject.java
with GNU General Public License v3.0
from 5zig-reborn
with GNU General Public License v3.0
from 5zig-reborn
/**
* Defines a source, playlist or track.
* <p>
* An ITObject uniquely identifies a source, playlist, or track in iTunes using
* four separate IDs. These are runtime IDs, they are only valid while the
* current instance of iTunes is running.
* <p>
* As of iTunes 7.7, you can also identify an ITObject using a 64-bit persistent
* ID, which is valid across multiple invocations of iTunes.
* <p>
* The main use of the ITObject interface is to allow clients to track iTunes
* database changes using
* <code>iTunesEventsInterface.onDatabaseChangedEvent()</code>.
* <p>
* You can retrieve an ITObject with a specified runtime ID using
* <code>iTunes.gereplacedObjectByID()</code>.
* <p>
* An ITObject will always have a valid, non-zero source ID.
* <p>
* An ITObject corresponding to a playlist or track will always have a valid
* playlist ID. The playlist ID will be zero for a source.
* <p>
* An ITObject corresponding to a track will always have a valid track and
* track database ID. These IDs will be zero for a source or playlist.
* <p>
* A track ID is unique within the track's playlist. A track database ID is
* unique across all playlists. For example, if the same music file is in two
* different playlists, each of the tracks could have different track IDs, but
* they will have the same track database ID.
* <p>
* An ITObject also has a 64-bit persistent ID which can be used to identify
* the ITObject across multiple invocations of iTunes.
*/
public clreplaced IITObject {
protected Dispatch object;
public IITObject(Dispatch d) {
object = d;
}
/**
* Returns the JACOB Dispatch object for this object.
*
* @return Returns the JACOB Dispatch object for this object.
*/
public Dispatch fetchDispatch() {
return object;
}
/**
* Set the name of the object.
*
* @param name The new name of the object.
*/
public void setName(String name) {
Dispatch.put(object, "Name", name);
}
/**
* Returns the name of the object.
*
* @return Returns the name of the object.
*/
public String getName() {
return Dispatch.get(object, "Name").getString();
}
/**
* Returns the index of the object in internal application order.
*
* @return The index of the object in internal application order.
*/
public int getIndex() {
return Dispatch.get(object, "Index").getInt();
}
/**
* Returns the ID that identifies the source.
*
* @return Returns the ID that identifies the source.
*/
public int getSourceID() {
return Dispatch.get(object, "SourceID").getInt();
}
/**
* Returns the ID that identifies the playlist.
*
* @return Returns the ID that identifies the playlist.
*/
public int getPlaylistID() {
return Dispatch.get(object, "PlaylistID").getInt();
}
/**
* Returns the ID that identifies the track within the playlist.
*
* @return Returns the ID that identifies the track within the playlist.
*/
public int getTrackID() {
return Dispatch.get(object, "TrackID").getInt();
}
/**
* Returns the ID that identifies the track, independent of its playlist.
*
* @return Returns the ID that identifies the track, independent of its playlist.
*/
public int getTrackDatabaseID() {
return Dispatch.get(object, "TrackDatabaseID").getInt();
}
}
19
Source : IITArtworkCollection.java
with GNU General Public License v3.0
from 5zig-reborn
with GNU General Public License v3.0
from 5zig-reborn
/**
* Represents a collection of Artwork objects.
* <p>
* Note that collection indices are always 1-based.
* <p>
* You can retrieve all the Artworks defined for a source using
* <code>ITSource.getArtwork()</code>.
*/
public clreplaced IITArtworkCollection {
protected Dispatch object;
public IITArtworkCollection(Dispatch d) {
object = d;
}
/**
* Returns the number of playlists in the collection.
*
* @return Returns the number of playlists in the collection.
*/
public int getCount() {
return Dispatch.get(object, "Count").getInt();
}
/**
* Returns an ITArtwork object corresponding to the given index (1-based).
*
* @param index Index of the playlist to retrieve, must be less than or
* equal to <code>ITArtworkCollection.getCount()</code>.
* @return Returns an ITArtwork object corresponding to the given index.
* Will be set to NULL if no playlist could be retrieved.
*/
public IITArtwork gereplacedem(int index) {
Variant variant = Dispatch.call(object, "Item", index);
if (variant.isNull()) {
return null;
}
Dispatch item = variant.toDispatch();
return new IITArtwork(item);
}
/**
* Returns an ITArtwork object with the specified persistent ID. See the
* doreplacedentation on ITObject for more information on persistent IDs.
*
* @param highID The high 32 bits of the 64-bit persistent ID.
* @param lowID The low 32 bits of the 64-bit persistent ID.
* @return Returns an ITArtwork object with the specified persistent ID.
* Will be set to NULL if no playlist could be retrieved.
*/
public IITArtwork gereplacedemByPersistentID(int highID, int lowID) {
Variant variant = Dispatch.call(object, "ItemByPersistentID", highID, lowID);
if (variant.isNull()) {
return null;
}
Dispatch item = variant.toDispatch();
return new IITArtwork(item);
}
}
19
Source : IITArtworkCollection.java
with GNU General Public License v3.0
from 5zig-reborn
with GNU General Public License v3.0
from 5zig-reborn
/**
* Returns an ITArtwork object with the specified persistent ID. See the
* doreplacedentation on ITObject for more information on persistent IDs.
*
* @param highID The high 32 bits of the 64-bit persistent ID.
* @param lowID The low 32 bits of the 64-bit persistent ID.
* @return Returns an ITArtwork object with the specified persistent ID.
* Will be set to NULL if no playlist could be retrieved.
*/
public IITArtwork gereplacedemByPersistentID(int highID, int lowID) {
Variant variant = Dispatch.call(object, "ItemByPersistentID", highID, lowID);
if (variant.isNull()) {
return null;
}
Dispatch item = variant.toDispatch();
return new IITArtwork(item);
}
19
Source : IITArtworkCollection.java
with GNU General Public License v3.0
from 5zig-reborn
with GNU General Public License v3.0
from 5zig-reborn
/**
* Returns an ITArtwork object corresponding to the given index (1-based).
*
* @param index Index of the playlist to retrieve, must be less than or
* equal to <code>ITArtworkCollection.getCount()</code>.
* @return Returns an ITArtwork object corresponding to the given index.
* Will be set to NULL if no playlist could be retrieved.
*/
public IITArtwork gereplacedem(int index) {
Variant variant = Dispatch.call(object, "Item", index);
if (variant.isNull()) {
return null;
}
Dispatch item = variant.toDispatch();
return new IITArtwork(item);
}
18
Source : ImportOutlookContactsDialog.java
with GNU General Public License v2.0
from jfritz-org
with GNU General Public License v2.0
from jfritz-org
public Dispatch init() {
// $NON-NLS-1$
outlookElements.addElement("FirstName");
// $NON-NLS-1$
outlookElements.addElement("LastName");
// $NON-NLS-1$
outlookElements.addElement("MiddleName");
// $NON-NLS-1$
outlookElements.addElement("FullName");
// $NON-NLS-1$
outlookElements.addElement("HomeAddressStreet");
// $NON-NLS-1$
outlookElements.addElement("HomeAddressPostalCode");
// $NON-NLS-1$
outlookElements.addElement("HomeAddressCity");
// $NON-NLS-1$
outlookElements.addElement("BusinessTelephoneNumber");
// $NON-NLS-1$
outlookElements.addElement("Business2TelephoneNumber");
// $NON-NLS-1$
outlookElements.addElement("HomeTelephoneNumber");
// $NON-NLS-1$
outlookElements.addElement("Home2TelephoneNumber");
// $NON-NLS-1$
outlookElements.addElement("PrimaryTelephoneNumber");
// $NON-NLS-1$
outlookElements.addElement("MobileTelephoneNumber");
// $NON-NLS-1$
outlookElements.addElement("CarTelephoneNumber");
// $NON-NLS-1$
outlookElements.addElement("RadioTelephoneNumber");
// $NON-NLS-1$
outlookElements.addElement("CallbackTelephoneNumber");
// $NON-NLS-1$
outlookElements.addElement("replacedistantTelephoneNumber");
// $NON-NLS-1$
outlookElements.addElement("CompanyMainTelephoneNumber");
// $NON-NLS-1$
outlookElements.addElement("OtherTelephoneNumber");
// $NON-NLS-1$
outlookElements.addElement("Categories");
// $NON-NLS-1$
ActiveXComponent ol = new ActiveXComponent("Outlook.Application");
Dispatch olo = ol.getObject();
// $NON-NLS-1$
String olVersion = Dispatch.get(olo, "Version").toString();
if (olVersion.startsWith("11")) {
// $NON-NLS-1$
// $NON-NLS-1$
outlookElements.addElement("HasPicture");
}
Dispatch myNamespace = // $NON-NLS-1$, //$NON-NLS-2$
Dispatch.call(olo, "GetNamespace", "MAPI").toDispatch();
Dispatch myFolder = // $NON-NLS-1$
Dispatch.call(// $NON-NLS-1$
myNamespace, // $NON-NLS-1$
"GetDefaultFolder", Integer.valueOf(10)).toDispatch();
return myFolder;
}
18
Source : WMPlayer.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* This should demo the media player but it doesn't
*/
public void testOpenWMPlayer() {
// this file exists in windows 7 installations
File file = new File("C:/Windows/winsxs/x86_microsoft-windows-videosamples_31bf3856ad364e35_6.1.7600.16385_none_f583837f77a63ec7");
String filePath = file.getAbsolutePath();
String microsoftTestURL = filePath;
// use these instead if not on windows 7
// "http://support.microsoft.com/support/mediaplayer/wmptest/samples/new/mediaexample.wma";
// "http://support.microsoft.com/support/mediaplayer/wmptest/samples/new/mediaexample.wmv";
ActiveXComponent wmp = null;
// could use WMPlayer.OCX alias also
wmp = new ActiveXComponent(// ("WMPlayer.OCX");
"CLSID:{6BF52A52-394A-11D3-B153-00C04F79FAA6}");
wmp.setProperty("URL", microsoftTestURL);
replacedertEquals(wmp.getProperty("URL").toString(), microsoftTestURL);
// alternative way to get the controls
Dispatch controls = Dispatch.get(wmp, "controls").toDispatch();
Dispatch.call(controls, "Play");
// the sourceforge posting didn't post all the code so this is all we
// have we need some other information on how to set the doreplacedent
// so that we have a url to open
// pause to let it play a second or two
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
System.out.println("Thread interrupted");
}
for (int i = 0; i < 1000; i++) {
// Get media object
Dispatch vMedObj = wmp.getProperty("currentMedia").toDispatch();
// Get duration of media object
Variant vdur = Dispatch.call(vMedObj, "duration");
// why is this always 0?
// System.out.println(microsoftTestURL + " length is "
// + vdur.getDouble());
// System.out.println("the wmp url is "
// + wmp.getProperty("URL").toString());
}
}
18
Source : ExcelEventTest.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* extracted the listener hookup so we could try multiple listeners.
*
* @param axc
* @param programId
* @param typeLibLocation
*/
private void hookupListener(Dispatch axc, String programId, String typeLibLocation) {
// Add a listener (doesn't matter what it is).
DispatchEvents applicationEvents;
if (typeLibLocation == null) {
applicationEvents = new DispatchEvents(axc, new ExcelEvents(programId));
} else {
applicationEvents = new DispatchEvents(axc, new ExcelEvents(programId), programId, typeLibLocation);
}
if (applicationEvents == null) {
System.out.println("No exception thrown but no dispatch returned for Excel events");
} else {
// Yea!
System.out.println("Successfully attached listener to " + programId);
}
}
17
Source : Access.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* Open a database
*
* @param ax
* @param fileName
* @return dispatch object that was opened
*/
public static Dispatch open(ActiveXComponent ax, String fileName) {
Variant f = new Variant(false);
// open the file in read-only mode
Variant[] args = new Variant[] { new Variant(fileName), f, f };
Dispatch openDB = ax.invoke("OpenDatabase", args).toDispatch();
return openDB;
}
17
Source : Access.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* the main loop for the test
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ComThread.InitSTA();
// original test used this
// ActiveXComponent ax = new ActiveXComponent("DAO.PrivateDBEngine");
// my xp box with a later release of access needed this
ActiveXComponent ax = new ActiveXComponent("DAO.PrivateDBEngine.35");
// this only works for access files pre-access-2000
// this line doesn't work on my xp box in Eclipse
// Dispatch db = open(ax, ".\\sample2.mdb");
// this works when running in eclipse because the test cases run pwd
// project root
Dispatch db = open(ax, "samples/com/jacob/samples/access/sample2.mdb");
String sql = "select * from MainTable";
// make a temporary querydef
Dispatch qd = Dispatch.call(db, "CreateQueryDef", "").toDispatch();
// set the SQL string on it
Dispatch.put(qd, "SQL", sql);
Variant result = getByQueryDef(qd);
// the 2-d safearray is transposed from what you might expect
System.out.println("resulting array is " + result.toSafeArray());
close(db);
System.out.println("about to call ComThread.Release()");
ComThread.Release();
}
15
Source : Access.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* gets the columns form the rec set
*
* @param recset
* @return list of column names
*/
public static String[] getColumns(Dispatch recset) {
Dispatch flds = Dispatch.get(recset, "Fields").toDispatch();
int n_flds = Dispatch.get(flds, "Count").getInt();
String[] s = new String[n_flds];
Variant vi = new Variant();
for (int i = 0; i < n_flds; i++) {
vi.putInt(i);
// must use the invoke method because this is a method call
// that wants to have a Dispatch.Get flag...
Dispatch fld = Dispatch.invoke(recset, "Fields", Dispatch.Get, new Object[] { vi }, new int[1]).toDispatch();
Variant name = Dispatch.get(fld, "Name");
s[i] = name.toString();
}
return s;
}
13
Source : SafeArrayLeak.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* ----------------------------------------------------------------------------------------------------------------------------
*
* ----------------------------------------------------------------------------------------------------------------------------
*/
public void testLeakWithSetString() {
ActiveXComponent xl = null;
Dispatch workbooks = null;
Dispatch workbook = null;
Dispatch workSheets = null;
Dispatch sheet = null;
Dispatch tabCells = null;
SafeArray sa = null;
// -Dcom.jacob.autogc=true
System.out.println("Jacob version: " + JacobReleaseInfo.getBuildVersion());
for (int t = 0; t < 10; t++) {
// look at a large range of cells
String position = "A7:DM8934";
try {
xl = new ActiveXComponent("Excel.Application");
System.out.println("Excel version=" + xl.getProperty("Version"));
xl.setProperty("Visible", new Variant(false));
workbooks = xl.getProperty("Workbooks").toDispatch();
workbook = Dispatch.get(workbooks, "Add").toDispatch();
workSheets = Dispatch.get(workbook, "Worksheets").toDispatch();
sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
// grab the whole range specified above.
tabCells = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { position }, new int[1]).toDispatch();
sa = Dispatch.get(tabCells, "Value").toSafeArray(true);
// nbCol
System.out.println("Ub0=" + sa.getUBound(1));
// nbLgn
System.out.println("Ub1=" + sa.getUBound(2));
// number of rows
int nbLgn = sa.getUBound(2);
// number of columns
int nbCol = sa.getUBound(1);
int[] colLgn = new int[] { 0, 0 };
// now set a value on every cell in the range we retrieved
for (int i = 1; i <= nbLgn; i++) {
colLgn[1] = i;
for (int j = 1; j <= nbCol; j++) {
colLgn[0] = j;
// this one works with out a leak 1.13-M3
// sa.setString(j, i, "test");
// This one leaks with 1.13-M3 and earlier
sa.setString(colLgn, "test");
}
}
Dispatch.put(tabCells, "Value", sa);
Variant f = new Variant(false);
Dispatch.call(workbook, "Close", f);
System.out.println("Close");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sa != null) {
try {
sa.safeRelease();
} catch (Exception e) {
e.printStackTrace();
} finally {
sa = null;
}
}
if (tabCells != null) {
try {
tabCells.safeRelease();
} catch (Exception e) {
e.printStackTrace();
} finally {
tabCells = null;
}
}
if (sheet != null) {
try {
sheet.safeRelease();
} catch (Exception e) {
e.printStackTrace();
} finally {
sheet = null;
}
}
if (workSheets != null) {
try {
workSheets.safeRelease();
} catch (Exception e) {
e.printStackTrace();
} finally {
workSheets = null;
}
}
if (workbook != null) {
try {
workbook.safeRelease();
} catch (Exception e) {
e.printStackTrace();
} finally {
workbook = null;
}
}
if (workbooks != null) {
try {
workbooks.safeRelease();
} catch (Exception e) {
e.printStackTrace();
} finally {
workbooks = null;
}
}
if (xl != null) {
try {
xl.invoke("Quit", new Variant[] {});
} catch (Exception e) {
e.printStackTrace();
}
try {
xl.safeRelease();
} catch (Exception e) {
e.printStackTrace();
} finally {
xl = null;
}
}
ComThread.Release();
}
}
}
12
Source : ExcelEventTest.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* load up excel, register for events and make stuff happen
*
* @param args
*/
public void testExcelWithInvocationProxy() {
ComThread.InitSTA();
// we are going to listen to events on Application.
// You can probably also listen Excel.Sheet and Excel.Chart
String excelApplicationProgramId = "Excel.Application";
String excelSheetProgramId = "Excel.Sheet";
String typeLibLocation;
// office 2003
typeLibLocation = "C:\\Program Files\\Microsoft Office\\OFFICE11\\EXCEL.EXE";
// office 2007
typeLibLocation = "C:\\Program Files\\Microsoft Office\\OFFICE12\\EXCEL.EXE";
// office 2013 32 bit
typeLibLocation = "C:\\Program Files (x86)\\Microsoft Office\\Office14\\EXCEL.EXE";
// Office 2013 32
typeLibLocation = "C:\\Program Files (x86)\\Microsoft Office\\Office15\\EXCEL.EXE";
// Office 2019 32
typeLibLocation = "C:\\Program Files (x86)\\Microsoft Office\\root\\Office16\\EXCEL.EXE";
// Grab The Component.
ActiveXComponent axc = new ActiveXComponent(excelApplicationProgramId);
hookupListener(axc, excelApplicationProgramId, typeLibLocation);
try {
System.out.println("version=" + axc.getProperty("Version"));
System.out.println("version=" + Dispatch.get(axc, "Version"));
axc.setProperty("Visible", true);
Dispatch workbooks = axc.getPropertyAsComponent("Workbooks");
Dispatch workbook = Dispatch.get(workbooks, "Add").toDispatch();
Dispatch sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
System.out.println("Workbook: " + workbook);
System.out.println("Sheet: " + sheet);
if (typeLibLocation.contains("OFFICE11")) {
// office 2007 throws crashes the VM
System.out.println("Hooking up sheet listener");
hookupListener(sheet, excelSheetProgramId, typeLibLocation);
}
System.out.println("Retrieving cells");
Dispatch a1 = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { "A1" }, new int[1]).toDispatch();
Dispatch a2 = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { "A2" }, new int[1]).toDispatch();
System.out.println("Inserting value into A1");
System.out.println("Inserting calculation 2xA1 into A2");
Dispatch.put(a1, "Value", "123.456");
Dispatch.put(a2, "Formula", "=A1*2");
System.out.println("Retrieved a1 from excel:" + Dispatch.get(a1, "Value"));
System.out.println("Retrieved a2 from excel:" + Dispatch.get(a2, "Value"));
Variant f = new Variant(false);
Dispatch.call(workbook, "Close", f);
axc.invoke("Quit", new Variant[] {});
} catch (ComException cfe) {
cfe.printStackTrace();
fail("Failed to attach to " + excelApplicationProgramId + ": " + cfe.getMessage());
}
try {
// the sleep is required to let everything clear out after the quit
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ComThread.Release();
}
12
Source : ExcelDispatchTest.java
with GNU Lesser General Public License v2.1
from freemansoft
with GNU Lesser General Public License v2.1
from freemansoft
/**
* main run loop for test program
*
* @param args
* standard command line arguments
*/
public static void main(String[] args) {
ComThread.InitSTA();
ActiveXComponent xl = new ActiveXComponent("Excel.Application");
try {
System.out.println("version=" + xl.getProperty("Version"));
System.out.println("version=" + Dispatch.get(xl, "Version"));
Dispatch.put(xl, "Visible", new Variant(true));
Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();
Dispatch workbook = Dispatch.get(workbooks, "Add").toDispatch();
Dispatch sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
Dispatch a1 = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { "A1" }, new int[1]).toDispatch();
Dispatch a2 = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { "A2" }, new int[1]).toDispatch();
Dispatch.put(a1, "Value", "123.456");
Dispatch.put(a2, "Formula", "=A1*2");
System.out.println("a1 from excel:" + Dispatch.get(a1, "Value"));
System.out.println("a2 from excel:" + Dispatch.get(a2, "Value"));
Variant f = new Variant(false);
Dispatch.call(workbook, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
xl.invoke("Quit", new Variant[] {});
ComThread.Release();
}
}