-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
de3a9ea
commit d6675fa
Showing
84 changed files
with
5,579 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
contrib/msoffice/src/com/sun/jna/platform/win32/COM/util/office/MSOfficeExcelDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
contrib/msoffice/src/com/sun/jna/platform/win32/COM/util/office/MSOfficeWordDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* 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 { | ||
String tempDir = System.getProperty("java.io.tmpdir"); | ||
System.out.println("Files in temp dir: "+tempDir); | ||
|
||
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(tempDir+"\\jnatestSaveAs.doc", WdSaveFormat.wdFormatDocument); | ||
msWord.getActiveDocument().SaveAs(tempDir+"\\jnatestSaveAs.pdf", WdSaveFormat.wdFormatPDF); | ||
msWord.getActiveDocument().SaveAs(tempDir+"\\jnatestSaveAs.rtf", WdSaveFormat.wdFormatRTF); | ||
msWord.getActiveDocument().SaveAs(tempDir+"\\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(tempDir+"\\jnatestNewDoc1.docx", WdSaveFormat.wdFormatDocumentDefault); | ||
msWord.getActiveDocument().SaveAs(tempDir+"\\jnatestNewDoc2.docx", WdSaveFormat.wdFormatDocumentDefault); | ||
msWord.getActiveDocument().SaveAs(tempDir+"\\jnatestNewDoc3.docx", WdSaveFormat.wdFormatDocumentDefault); | ||
// close and save the document | ||
msWord.getActiveDocument().Close(false); | ||
// open 3 documents | ||
msWord.getDocuments().Open(tempDir+"\\jnatestNewDoc1.docx"); | ||
msWord.getSelection().TypeText("Hello some changes from JNA!\n"); | ||
msWord.getDocuments().Open(tempDir+"\\jnatestNewDoc2.docx"); | ||
msWord.getSelection().TypeText("Hello some changes from JNA!\n"); | ||
msWord.getDocuments().Open(tempDir+"\\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); | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...b/msoffice/src/com/sun/jna/platform/win32/COM/util/office/excel/ComExcel_Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
contrib/msoffice/src/com/sun/jna/platform/win32/COM/util/office/excel/ComIAppEvents.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
contrib/msoffice/src/com/sun/jna/platform/win32/COM/util/office/excel/ComIApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
25 changes: 25 additions & 0 deletions
25
contrib/msoffice/src/com/sun/jna/platform/win32/COM/util/office/excel/ComIRange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
contrib/msoffice/src/com/sun/jna/platform/win32/COM/util/office/excel/ComIWorkbook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
||
} |
19 changes: 19 additions & 0 deletions
19
contrib/msoffice/src/com/sun/jna/platform/win32/COM/util/office/excel/ComIWorkbooks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
contrib/msoffice/src/com/sun/jna/platform/win32/COM/util/office/excel/ComIWorksheet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
Oops, something went wrong.