Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COM support: Added significant enhancements. #397

Closed
wants to merge 51 commits into from
Closed
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
f279e84
COM: Added Running Object Table bits.
dhakehurst Oct 21, 2014
c5c8da5
Added pom files for building eclipse p2-repository, features and bundles
dhakehurst Oct 22, 2014
c153e96
Added eclipse project files
dhakehurst Oct 27, 2014
fe0ed1c
renaming packages and interfaces in new COM bits
dhakehurst Oct 28, 2014
0f399d5
Added more eclipse files
dhakehurst Oct 28, 2014
06c884a
Added .gitignore
dhakehurst Oct 28, 2014
cdff360
Calls to COM objects via ...utils should now all run on a single thread.
dhakehurst Oct 28, 2014
39e14d6
Improve COM.utils tests
dhakehurst Oct 29, 2014
58054dd
rename eclipse folder to maven folder
dhakehurst Oct 31, 2014
209bd2f
change MsWord example to use new com stuff
dhakehurst Oct 31, 2014
2ccf522
Adding support for COM event callbacks
dhakehurst Nov 3, 2014
1141286
Adding support for COM Event callbacks
dhakehurst Nov 3, 2014
00e6de5
COM Event Callback support
dhakehurst Nov 4, 2014
046169f
COM.utils.Factoy should not be an static INSTANCE, may want different
dhakehurst Nov 4, 2014
f368ad4
COM/COM.utils Copyright header modification
dhakehurst Nov 5, 2014
f5c4bd5
Now supporting Event callbacks
dhakehurst Nov 7, 2014
02d7d3d
Fix: Callback events executed on their own thread
dhakehurst Nov 7, 2014
048ed54
COM.utilÖ updates
dhakehurst Nov 7, 2014
c4e8aab
Remove UNUSED_ENV macro from Java_com_sun_jna_Native_getWindowHandle0…
dhakehurst Nov 11, 2014
b42ba9d
enable comparison of COM.util objects
dhakehurst Nov 12, 2014
b53fd3a
Iterator required 'remove' to be implemented for < Java 1.8
dhakehurst Nov 12, 2014
b5c542b
comment out inclusion of testlib
dhakehurst Nov 12, 2014
81ffec8
comment out dependency on missing testlib
dhakehurst Nov 12, 2014
4cd8d30
COM.util: bug fix: arguments to callback events now converted where
dhakehurst Nov 18, 2014
22915e0
...COM.util: proxy must AddRef on COM handle and Release it
dhakehurst Nov 18, 2014
581cc00
..COM.utils: maven build pom update
dhakehurst Nov 18, 2014
669697a
added parameter to pom to allow commandline additional.pluginRepository
dhakehurst Nov 18, 2014
58ea371
it needs a vaild default
dhakehurst Nov 18, 2014
88c9a5f
pom update
dhakehurst Nov 18, 2014
0cdd1cb
maven build: update
dhakehurst Nov 19, 2014
d91d5cc
...COM.util: Added (I think) correct management of COM Refs via
dhakehurst Nov 25, 2014
421a2dc
msoffice Demo: added ..COM.util version for Excel
dhakehurst Nov 25, 2014
b20e95b
Merge remote-tracking branch 'origin/master'
dhakehurst Nov 25, 2014
1ebbff4
Update Excel Test,
dhakehurst Nov 25, 2014
f110246
Using new version of p2 generator
dhakehurst Nov 26, 2014
4b9362f
..COM.util: reorganise disposing
dhakehurst Nov 27, 2014
ed27da3
Enable COM calls to timeout and throw exception
dhakehurst Nov 28, 2014
201139c
COM.util bug fix
dhakehurst Dec 5, 2014
57abe60
..COM.util: Catch exceptions in ComThread-firstTask
dhakehurst Dec 9, 2014
280f1e3
...COM.utild: add capability to return <? extends IComEnum> from methods
dhakehurst Dec 18, 2014
04fa536
fix null pointer issue in COMException
dhakehurst Jan 19, 2015
25f1b08
Improve thread safety
dhakehurst Jan 19, 2015
756685e
added use java 1.6 to pom.xml files
dhakehurst Jan 19, 2015
d60291b
...COM.util: improve equals and hashCode for ProxyObject
dhakehurst Jan 20, 2015
4e0aa5e
test update
dhakehurst Feb 9, 2015
c171c2f
added a file
dhakehurst Feb 9, 2015
af5db87
header comment modification
dhakehurst Feb 9, 2015
ce7a463
minor modifications
dhakehurst Feb 10, 2015
293a62e
...COM.util:
dhakehurst Feb 20, 2015
81ba1be
Merge branch 'master' of https://github.com/twall/jna
dhakehurst Feb 20, 2015
57aeb2f
updated full stops
dhakehurst Mar 4, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/>
<classpathentry kind="lib" path="lib/test/reflections-0.9.8.jar"/>
<classpathentry kind="lib" path="dist/jna.jar"/>
<classpathentry kind="output" path="build.eclipse/classes"/>
</classpath>
4 changes: 2 additions & 2 deletions contrib/msoffice/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry combineaccessrules="false" kind="src" path="/jnalib"/>
<classpathentry kind="var" path="JNA_LIB"/>
<classpathentry combineaccessrules="false" kind="src" path="/com.sun.jna"/>
<classpathentry combineaccessrules="false" kind="src" path="/com.sun.jna.platform"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Binary file modified contrib/msoffice/jnatest.xls
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public static void main(String[] args) {
+ File.separator;

public MSOfficeDemo() {
this.testMSWord();
// this.testMSExcel();
//this.testMSWord();
this.testMSExcel();
}

public void testMSWord() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* Copyright (c) 2014 Dr David H. Akehurst (itemis), All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package com.sun.jna.platform.win32.COM.util.office;

import java.io.File;

import com.sun.jna.platform.win32.COM.office.MSExcel;
import com.sun.jna.platform.win32.COM.util.AbstractComEventCallbackListener;
import com.sun.jna.platform.win32.COM.util.Factory;
import com.sun.jna.platform.win32.COM.util.office.excel.ComExcel_Application;
import com.sun.jna.platform.win32.COM.util.office.excel.ComIAppEvents;
import com.sun.jna.platform.win32.COM.util.office.excel.ComIApplication;
import com.sun.jna.platform.win32.COM.util.office.excel.ComIRange;
import com.sun.jna.platform.win32.COM.util.office.excel.ComIWorksheet;
import com.sun.jna.platform.win32.COM.util.office.word.ComWord_Application;

public class MSOfficeExcelDemo {

/**
* @param args
*/
public static void main(String[] args) {
new MSOfficeExcelDemo();
}

private String currentWorkingDir = new File("").getAbsolutePath() + File.separator;

public MSOfficeExcelDemo() {
this.testMSExcel();
}

public void testMSExcel() {
ComExcel_Application excelObject = null;
ComIApplication msExcel = null;
Factory factory = null;
try {
factory = new Factory();
excelObject = factory.createObject(ComExcel_Application.class);
msExcel = excelObject.queryInterface(ComIApplication.class);
System.out.println("MSExcel version: " + msExcel.getVersion());
msExcel.setVisible(true);
// msExcel.newExcelBook();
msExcel.getWorkbooks().Open(currentWorkingDir + "jnatest.xls");
msExcel.getActiveSheet().getRange("A1").setValue("Hello from JNA!");
// wait 1sec. before closing
Thread.currentThread().sleep(1000);
// // close and save the active sheet
// msExcel.getActiveWorkbook().Close(true);
// msExcel.setVisible(true);
// // msExcel.newExcelBook();
// msExcel.getWorkbooks().Open(currentWorkingDir + "jnatest.xls");
// msExcel.getActiveSheet().getRange("A2").setValue("Hello again from JNA!");

class Listener extends AbstractComEventCallbackListener implements ComIAppEvents {
boolean SheetSelectionChange_called;

@Override
public void errorReceivingCallbackEvent(String message, Exception exception) {
}

@Override
public void SheetSelectionChange(ComIWorksheet sheet, ComIRange target) {
SheetSelectionChange_called = true;
}

};
Listener listener = new Listener();
msExcel.advise(ComIAppEvents.class, listener);
//
// msExcel.getActiveSheet().getRange("A5").Activate();
//
// Thread.currentThread().sleep(500);

// close and save the active sheet
msExcel.getActiveWorkbook().Close(true);

msExcel.Quit();
msExcel = null;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != msExcel) {
msExcel.Quit();
}
if (null != factory) {
factory.disposeAll();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* Copyright (c) 2014 Dr David H. Akehurst (itemis), All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package com.sun.jna.platform.win32.COM.util.office;

import java.io.File;

import com.sun.jna.platform.win32.COM.COMException;
import com.sun.jna.platform.win32.COM.util.Factory;
import com.sun.jna.platform.win32.COM.util.office.word.ComIApplication;
import com.sun.jna.platform.win32.COM.util.office.word.ComWord_Application;
import com.sun.jna.platform.win32.COM.util.office.word.WdOriginalFormat;
import com.sun.jna.platform.win32.COM.util.office.word.WdSaveFormat;

public class MSOfficeWordDemo {

/**
* @param args
*/
public static void main(String[] args) {
new MSOfficeWordDemo();
}

private String currentWorkingDir = new File("").getAbsolutePath() + File.separator;

public MSOfficeWordDemo() {
this.testMSWord();
}

public void testMSWord() {
ComWord_Application msWordObject = null;
ComIApplication msWord = null;
Factory factory = null;
try {
factory = new Factory();
msWordObject = factory.createObject(ComWord_Application.class);
msWord = msWordObject.queryInterface(ComIApplication.class);

System.out.println("MSWord version: " + msWord.getVersion());

msWord.setVisible(true);
// msWord.newDocument();
msWord.getDocuments().Open(currentWorkingDir + "jnatest.doc");
msWord.getSelection().TypeText("Hello from JNA! \n\n");
// wait 10sec. before closing
Thread.sleep(1000);
// save in different formats
// pdf format is only supported in MSWord 2007 and above
msWord.getActiveDocument().SaveAs("C:\\TEMP\\jnatestSaveAs.doc", WdSaveFormat.wdFormatDocument);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you feel motivated, fetch the TMP directory (GetTemp...something) and combine it here. Not everybody has a C:\\TEMP.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done,

FYI,
String tempDir = System.getProperty("java.io.tmpdir");

msWord.getActiveDocument().SaveAs("C:\\TEMP\\jnatestSaveAs.pdf", WdSaveFormat.wdFormatPDF);
msWord.getActiveDocument().SaveAs("C:\\TEMP\\jnatestSaveAs.rtf", WdSaveFormat.wdFormatRTF);
msWord.getActiveDocument().SaveAs("C:\\TEMP\\jnatestSaveAs.html", WdSaveFormat.wdFormatHTML);
// close and save the document
msWord.getActiveDocument().Close(false);
msWord.getDocuments().Add();
// msWord.openDocument(currentWorkingDir + "jnatest.doc", true);
msWord.getSelection()
.TypeText(
"Hello from JNA! \n Please notice that JNA can control MS Word via the new COM interface! \nHere we are creating a new word document and we save it to the 'TEMP' directory!");
// save with no user prompt
msWord.getActiveDocument().SaveAs("C:\\TEMP\\jnatestNewDoc1.docx", WdSaveFormat.wdFormatDocumentDefault);
msWord.getActiveDocument().SaveAs("C:\\TEMP\\jnatestNewDoc2.docx", WdSaveFormat.wdFormatDocumentDefault);
msWord.getActiveDocument().SaveAs("C:\\TEMP\\jnatestNewDoc3.docx", WdSaveFormat.wdFormatDocumentDefault);
// close and save the document
msWord.getActiveDocument().Close(false);
// open 3 documents
msWord.getDocuments().Open("C:\\TEMP\\jnatestNewDoc1.docx");
msWord.getSelection().TypeText("Hello some changes from JNA!\n");
msWord.getDocuments().Open("C:\\TEMP\\jnatestNewDoc2.docx");
msWord.getSelection().TypeText("Hello some changes from JNA!\n");
msWord.getDocuments().Open("C:\\TEMP\\jnatestNewDoc3.docx");
msWord.getSelection().TypeText("Hello some changes from JNA!\n");
// save the document and prompt the user
msWord.getDocuments().Save(false, WdOriginalFormat.wdPromptUser);
// wait then close word
msWord.Quit();
msWord = null;
} catch (InterruptedException ie) {
ie.printStackTrace();
} catch (COMException e) {
if (e.getExcepInfo() != null) {
System.out.println("bstrSource: " + e.getExcepInfo().bstrSource);
System.out.println("bstrDescription: " + e.getExcepInfo().bstrDescription);
}

// print stack trace
e.printStackTrace();
} finally {
if (msWord != null) {
msWord.Quit();
}
if (null != factory) {
factory.getComThread().terminate(500);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.sun.jna.platform.win32.COM.util.office.excel;

import com.sun.jna.platform.win32.COM.util.IUnknown;
import com.sun.jna.platform.win32.COM.util.annotation.ComMethod;
import com.sun.jna.platform.win32.COM.util.annotation.ComObject;

@ComObject(progId="Excel.Application")
public interface ComExcel_Application extends IUnknown {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sun.jna.platform.win32.COM.util.office.excel;

import com.sun.jna.platform.win32.COM.util.annotation.ComEventCallback;
import com.sun.jna.platform.win32.COM.util.annotation.ComInterface;

@ComInterface(iid="{00024413-0000-0000-C000-000000000046}")
public interface ComIAppEvents {

@ComEventCallback(dispid=1558)
public void SheetSelectionChange(ComIWorksheet sheet, ComIRange target);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.sun.jna.platform.win32.COM.util.office.excel;

import com.sun.jna.platform.win32.COM.util.IConnectionPoint;
import com.sun.jna.platform.win32.COM.util.IUnknown;
import com.sun.jna.platform.win32.COM.util.annotation.ComInterface;
import com.sun.jna.platform.win32.COM.util.annotation.ComMethod;
import com.sun.jna.platform.win32.COM.util.annotation.ComProperty;

@ComInterface(iid="{000208D5-0000-0000-C000-000000000046}")
public interface ComIApplication extends IUnknown, IConnectionPoint {

@ComProperty
String getVersion();

@ComProperty
boolean getVisible();

@ComProperty
void setVisible(boolean value);

@ComProperty
ComIWorkbooks getWorkbooks();

@ComProperty
ComIWorksheet getActiveSheet();

@ComProperty
ComIWorkbook getActiveWorkbook();

@ComMethod
void Quit();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.sun.jna.platform.win32.COM.util.office.excel;

import com.sun.jna.platform.win32.COM.util.annotation.ComInterface;
import com.sun.jna.platform.win32.COM.util.annotation.ComMethod;
import com.sun.jna.platform.win32.COM.util.annotation.ComProperty;

@ComInterface(iid = "{00020846-0000-0000-C000-000000000046}")
public interface ComIRange {

@ComProperty
ComIApplication getApplication();

@ComProperty
String getText();

@ComMethod
void Select();

@ComProperty
void setValue(String value);

@ComMethod
void Activate();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sun.jna.platform.win32.COM.util.office.excel;

import com.sun.jna.platform.win32.COM.util.annotation.ComInterface;
import com.sun.jna.platform.win32.COM.util.annotation.ComMethod;

@ComInterface(iid="{0002096B-0000-0000-C000-000000000046}")
public interface ComIWorkbook {

@ComMethod
void Close(boolean saveChanges);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.sun.jna.platform.win32.COM.util.office.excel;

import com.sun.jna.platform.win32.COM.util.annotation.ComInterface;
import com.sun.jna.platform.win32.COM.util.annotation.ComMethod;
import com.sun.jna.platform.win32.COM.util.annotation.ComProperty;

@ComInterface(iid = "{000208DB-0000-0000-C000-000000000046}")
public interface ComIWorkbooks {

@ComProperty
long getCount();

@ComMethod
ComIWorkbook Item(long index);

@ComMethod
ComIWorkbook Open(Object FileName);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.sun.jna.platform.win32.COM.util.office.excel;

import com.sun.jna.platform.win32.COM.util.annotation.ComInterface;
import com.sun.jna.platform.win32.COM.util.annotation.ComMethod;
import com.sun.jna.platform.win32.COM.util.annotation.ComProperty;

@ComInterface(iid="{000208D8-0000-0000-C000-000000000046}")
public interface ComIWorksheet {

@ComProperty
String getName();

@ComProperty
ComIRange getRange(String cell1);

@ComProperty
ComIApplication getApplication();

}
Loading