Skip to content

Commit d6675fa

Browse files
dhakehurstdblock
authored andcommitted
Improved COM support in com.sun.jna.platform.win32.COM.util.
Use of interfaces and annotations to provide easier implementation of COM interfaces (uses InvocationHandler). Support for COM event callbacks. Support for COM interface discovery by iteration over the RunningObjectTable.
1 parent de3a9ea commit d6675fa

84 files changed

Lines changed: 5579 additions & 36 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGES.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
NOTE: as of JNA 4.0, JNA is now dual-licensed under LGPL and ASL (see LICENSE).
1+
NOTE: as of JNA 4.0, JNA is now dual-licensed under LGPL and ASL (see LICENSE).
22

33
NOTE: JNI native support is typically incompatible between minor versions, and almost always incompatible between major versions.
44

5-
Release 4.2
6-
===========
5+
Release 4.2 (Next)
6+
==================
77

88
Features
99
--------
10+
11+
* [#387](https://github.com/twall/jna/pull/397): Use of interfaces and annotations to provide easier implementation of COM interfaces (with `InvocationHandler`) - [@dhakehurst](https://github.com/dhakehurst).
12+
* [#387](https://github.com/twall/jna/pull/397): Support for COM event callbacks - [@dhakehurst](https://github.com/dhakehurst).
13+
* [#387](https://github.com/twall/jna/pull/397): Support for COM interface discovery by iteration over `RunningObjectTable` - [@dhakehurst](https://github.com/dhakehurst).
1014
* Updated AIX natives and build - [@twall](https://github.com/twall).
1115
* [#290](https://github.com/twall/jna/pull/290): Improved the stacktrace for the exceptions thrown by `com.sun.jna.Structure` - [@ebourg](https://github.com/ebourg).
1216
* [#332](https://github.com/twall/jna/pull/332): Added Win32 Monitor Configuration API in `com.sun.jna.platform.win32.Dxva2` - [@msteiger](https://github.com/msteiger).
@@ -40,7 +44,7 @@ Bug Fixes
4044
* [#382](https://github.com/twall/jna/pull/382): Fixed memory allocation in `com.sun.jna.platform.win32.WTypes.LPWSTR` and `LPSTR` constructors - [@junak-michal](https://github.com/junak-michal).
4145
* Fix publish doc links - [@bhamail](https://github.com/bhamail).
4246
* [#388](https://github.com/twall/jna/issues/388): Ensure native library always opened with provided flags - [@zolyfarkas](https://github.com/zolyfarkas).
43-
* [#403](https://github.com/twall/jna/pull/403): Make com.sun.jna.platform.win32.COM.COMUtils.SUCCEEDED and FAILED conform to MSDN specification for said macros - [@lwahonen](https://github.com/lwahonen).
47+
* [#403](https://github.com/twall/jna/pull/403): Fix `com.sun.jna.platform.win32.COM.COMUtils.SUCCEEDED` and `FAILED` - [@lwahonen](https://github.com/lwahonen).
4448

4549
Release 4.1
4650
===========

contrib/msoffice/.classpath

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<classpath>
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5-
<classpathentry combineaccessrules="false" kind="src" path="/jnalib"/>
6-
<classpathentry kind="var" path="JNA_LIB"/>
5+
<classpathentry combineaccessrules="false" kind="src" path="/com.sun.jna"/>
6+
<classpathentry combineaccessrules="false" kind="src" path="/com.sun.jna.platform"/>
77
<classpathentry kind="output" path="bin"/>
88
</classpath>

contrib/msoffice/jnatest.xls

6 KB
Binary file not shown.

contrib/msoffice/src/com/sun/jna/platform/win32/COM/office/MSOfficeDemo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public static void main(String[] args) {
1818
+ File.separator;
1919

2020
public MSOfficeDemo() {
21-
this.testMSWord();
22-
// this.testMSExcel();
21+
//this.testMSWord();
22+
this.testMSExcel();
2323
}
2424

2525
public void testMSWord() {
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* Copyright (c) 2014 Dr David H. Akehurst (itemis), All Rights Reserved
2+
*
3+
* This library is free software; you can redistribute it and/or
4+
* modify it under the terms of the GNU Lesser General Public
5+
* License as published by the Free Software Foundation; either
6+
* version 2.1 of the License, or (at your option) any later version.
7+
*
8+
* This library is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* Lesser General Public License for more details.
12+
*/
13+
package com.sun.jna.platform.win32.COM.util.office;
14+
15+
import java.io.File;
16+
17+
import com.sun.jna.platform.win32.COM.office.MSExcel;
18+
import com.sun.jna.platform.win32.COM.util.AbstractComEventCallbackListener;
19+
import com.sun.jna.platform.win32.COM.util.Factory;
20+
import com.sun.jna.platform.win32.COM.util.office.excel.ComExcel_Application;
21+
import com.sun.jna.platform.win32.COM.util.office.excel.ComIAppEvents;
22+
import com.sun.jna.platform.win32.COM.util.office.excel.ComIApplication;
23+
import com.sun.jna.platform.win32.COM.util.office.excel.ComIRange;
24+
import com.sun.jna.platform.win32.COM.util.office.excel.ComIWorksheet;
25+
import com.sun.jna.platform.win32.COM.util.office.word.ComWord_Application;
26+
27+
public class MSOfficeExcelDemo {
28+
29+
/**
30+
* @param args
31+
*/
32+
public static void main(String[] args) {
33+
new MSOfficeExcelDemo();
34+
}
35+
36+
private String currentWorkingDir = new File("").getAbsolutePath() + File.separator;
37+
38+
public MSOfficeExcelDemo() {
39+
this.testMSExcel();
40+
}
41+
42+
public void testMSExcel() {
43+
ComExcel_Application excelObject = null;
44+
ComIApplication msExcel = null;
45+
Factory factory = null;
46+
try {
47+
factory = new Factory();
48+
excelObject = factory.createObject(ComExcel_Application.class);
49+
msExcel = excelObject.queryInterface(ComIApplication.class);
50+
System.out.println("MSExcel version: " + msExcel.getVersion());
51+
msExcel.setVisible(true);
52+
// msExcel.newExcelBook();
53+
msExcel.getWorkbooks().Open(currentWorkingDir + "jnatest.xls");
54+
msExcel.getActiveSheet().getRange("A1").setValue("Hello from JNA!");
55+
// wait 1sec. before closing
56+
Thread.currentThread().sleep(1000);
57+
// // close and save the active sheet
58+
// msExcel.getActiveWorkbook().Close(true);
59+
// msExcel.setVisible(true);
60+
// // msExcel.newExcelBook();
61+
// msExcel.getWorkbooks().Open(currentWorkingDir + "jnatest.xls");
62+
// msExcel.getActiveSheet().getRange("A2").setValue("Hello again from JNA!");
63+
64+
class Listener extends AbstractComEventCallbackListener implements ComIAppEvents {
65+
boolean SheetSelectionChange_called;
66+
67+
@Override
68+
public void errorReceivingCallbackEvent(String message, Exception exception) {
69+
}
70+
71+
@Override
72+
public void SheetSelectionChange(ComIWorksheet sheet, ComIRange target) {
73+
SheetSelectionChange_called = true;
74+
}
75+
76+
};
77+
Listener listener = new Listener();
78+
msExcel.advise(ComIAppEvents.class, listener);
79+
//
80+
// msExcel.getActiveSheet().getRange("A5").Activate();
81+
//
82+
// Thread.currentThread().sleep(500);
83+
84+
// close and save the active sheet
85+
msExcel.getActiveWorkbook().Close(true);
86+
87+
msExcel.Quit();
88+
msExcel = null;
89+
} catch (Exception e) {
90+
e.printStackTrace();
91+
} finally {
92+
if (null != msExcel) {
93+
msExcel.Quit();
94+
}
95+
if (null != factory) {
96+
factory.disposeAll();
97+
}
98+
}
99+
}
100+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/* Copyright (c) 2014 Dr David H. Akehurst (itemis), All Rights Reserved
2+
*
3+
* This library is free software; you can redistribute it and/or
4+
* modify it under the terms of the GNU Lesser General Public
5+
* License as published by the Free Software Foundation; either
6+
* version 2.1 of the License, or (at your option) any later version.
7+
*
8+
* This library is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* Lesser General Public License for more details.
12+
*/
13+
package com.sun.jna.platform.win32.COM.util.office;
14+
15+
import java.io.File;
16+
17+
import com.sun.jna.platform.win32.COM.COMException;
18+
import com.sun.jna.platform.win32.COM.util.Factory;
19+
import com.sun.jna.platform.win32.COM.util.office.word.ComIApplication;
20+
import com.sun.jna.platform.win32.COM.util.office.word.ComWord_Application;
21+
import com.sun.jna.platform.win32.COM.util.office.word.WdOriginalFormat;
22+
import com.sun.jna.platform.win32.COM.util.office.word.WdSaveFormat;
23+
24+
public class MSOfficeWordDemo {
25+
26+
/**
27+
* @param args
28+
*/
29+
public static void main(String[] args) {
30+
new MSOfficeWordDemo();
31+
}
32+
33+
private String currentWorkingDir = new File("").getAbsolutePath() + File.separator;
34+
35+
public MSOfficeWordDemo() {
36+
this.testMSWord();
37+
}
38+
39+
public void testMSWord() {
40+
ComWord_Application msWordObject = null;
41+
ComIApplication msWord = null;
42+
Factory factory = null;
43+
try {
44+
String tempDir = System.getProperty("java.io.tmpdir");
45+
System.out.println("Files in temp dir: "+tempDir);
46+
47+
factory = new Factory();
48+
msWordObject = factory.createObject(ComWord_Application.class);
49+
msWord = msWordObject.queryInterface(ComIApplication.class);
50+
51+
System.out.println("MSWord version: " + msWord.getVersion());
52+
53+
msWord.setVisible(true);
54+
// msWord.newDocument();
55+
msWord.getDocuments().Open(currentWorkingDir + "jnatest.doc");
56+
msWord.getSelection().TypeText("Hello from JNA! \n\n");
57+
// wait 10sec. before closing
58+
Thread.sleep(1000);
59+
// save in different formats
60+
// pdf format is only supported in MSWord 2007 and above
61+
msWord.getActiveDocument().SaveAs(tempDir+"\\jnatestSaveAs.doc", WdSaveFormat.wdFormatDocument);
62+
msWord.getActiveDocument().SaveAs(tempDir+"\\jnatestSaveAs.pdf", WdSaveFormat.wdFormatPDF);
63+
msWord.getActiveDocument().SaveAs(tempDir+"\\jnatestSaveAs.rtf", WdSaveFormat.wdFormatRTF);
64+
msWord.getActiveDocument().SaveAs(tempDir+"\\jnatestSaveAs.html", WdSaveFormat.wdFormatHTML);
65+
// close and save the document
66+
msWord.getActiveDocument().Close(false);
67+
msWord.getDocuments().Add();
68+
// msWord.openDocument(currentWorkingDir + "jnatest.doc", true);
69+
msWord.getSelection()
70+
.TypeText(
71+
"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!");
72+
// save with no user prompt
73+
msWord.getActiveDocument().SaveAs(tempDir+"\\jnatestNewDoc1.docx", WdSaveFormat.wdFormatDocumentDefault);
74+
msWord.getActiveDocument().SaveAs(tempDir+"\\jnatestNewDoc2.docx", WdSaveFormat.wdFormatDocumentDefault);
75+
msWord.getActiveDocument().SaveAs(tempDir+"\\jnatestNewDoc3.docx", WdSaveFormat.wdFormatDocumentDefault);
76+
// close and save the document
77+
msWord.getActiveDocument().Close(false);
78+
// open 3 documents
79+
msWord.getDocuments().Open(tempDir+"\\jnatestNewDoc1.docx");
80+
msWord.getSelection().TypeText("Hello some changes from JNA!\n");
81+
msWord.getDocuments().Open(tempDir+"\\jnatestNewDoc2.docx");
82+
msWord.getSelection().TypeText("Hello some changes from JNA!\n");
83+
msWord.getDocuments().Open(tempDir+"\\jnatestNewDoc3.docx");
84+
msWord.getSelection().TypeText("Hello some changes from JNA!\n");
85+
// save the document and prompt the user
86+
msWord.getDocuments().Save(false, WdOriginalFormat.wdPromptUser);
87+
// wait then close word
88+
msWord.Quit();
89+
msWord = null;
90+
} catch (InterruptedException ie) {
91+
ie.printStackTrace();
92+
} catch (COMException e) {
93+
if (e.getExcepInfo() != null) {
94+
System.out.println("bstrSource: " + e.getExcepInfo().bstrSource);
95+
System.out.println("bstrDescription: " + e.getExcepInfo().bstrDescription);
96+
}
97+
98+
// print stack trace
99+
e.printStackTrace();
100+
} finally {
101+
if (msWord != null) {
102+
msWord.Quit();
103+
}
104+
if (null != factory) {
105+
factory.getComThread().terminate(500);
106+
}
107+
}
108+
}
109+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.sun.jna.platform.win32.COM.util.office.excel;
2+
3+
import com.sun.jna.platform.win32.COM.util.IUnknown;
4+
import com.sun.jna.platform.win32.COM.util.annotation.ComMethod;
5+
import com.sun.jna.platform.win32.COM.util.annotation.ComObject;
6+
7+
@ComObject(progId="Excel.Application")
8+
public interface ComExcel_Application extends IUnknown {
9+
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.sun.jna.platform.win32.COM.util.office.excel;
2+
3+
import com.sun.jna.platform.win32.COM.util.annotation.ComEventCallback;
4+
import com.sun.jna.platform.win32.COM.util.annotation.ComInterface;
5+
6+
@ComInterface(iid="{00024413-0000-0000-C000-000000000046}")
7+
public interface ComIAppEvents {
8+
9+
@ComEventCallback(dispid=1558)
10+
public void SheetSelectionChange(ComIWorksheet sheet, ComIRange target);
11+
12+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.sun.jna.platform.win32.COM.util.office.excel;
2+
3+
import com.sun.jna.platform.win32.COM.util.IConnectionPoint;
4+
import com.sun.jna.platform.win32.COM.util.IUnknown;
5+
import com.sun.jna.platform.win32.COM.util.annotation.ComInterface;
6+
import com.sun.jna.platform.win32.COM.util.annotation.ComMethod;
7+
import com.sun.jna.platform.win32.COM.util.annotation.ComProperty;
8+
9+
@ComInterface(iid="{000208D5-0000-0000-C000-000000000046}")
10+
public interface ComIApplication extends IUnknown, IConnectionPoint {
11+
12+
@ComProperty
13+
String getVersion();
14+
15+
@ComProperty
16+
boolean getVisible();
17+
18+
@ComProperty
19+
void setVisible(boolean value);
20+
21+
@ComProperty
22+
ComIWorkbooks getWorkbooks();
23+
24+
@ComProperty
25+
ComIWorksheet getActiveSheet();
26+
27+
@ComProperty
28+
ComIWorkbook getActiveWorkbook();
29+
30+
@ComMethod
31+
void Quit();
32+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.sun.jna.platform.win32.COM.util.office.excel;
2+
3+
import com.sun.jna.platform.win32.COM.util.annotation.ComInterface;
4+
import com.sun.jna.platform.win32.COM.util.annotation.ComMethod;
5+
import com.sun.jna.platform.win32.COM.util.annotation.ComProperty;
6+
7+
@ComInterface(iid = "{00020846-0000-0000-C000-000000000046}")
8+
public interface ComIRange {
9+
10+
@ComProperty
11+
ComIApplication getApplication();
12+
13+
@ComProperty
14+
String getText();
15+
16+
@ComMethod
17+
void Select();
18+
19+
@ComProperty
20+
void setValue(String value);
21+
22+
@ComMethod
23+
void Activate();
24+
25+
}

0 commit comments

Comments
 (0)