Hi
I am trying to use swt automation to open an existing excel file. I picked up code
from Snippet199.java to get it going http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet199.java?rev=HEAD&content-type=text/vnd.viewcvs-markup
The code works, as in it opens up the excel file for me. However my CPU usage
goes up to 100% when excel is launched!
I tried
to debug a little further and found that the following line of code in
org.eclipse.swt.ole.win32.OleClientSite causes the CPU usage to go to 100%
/********************************************************/
protected void addObjectReferences() {
...
...
// Notify the control object that it is embedded in an OLE container
COM.OleSetContainedObject(objIUnknown.getAddress(), true);
...
...
}
/********************************************************/
I commented out the call to OleSetContainedObject and recompiled swt.jar. The
application seems to run fine now. But probably I have added a few memory leaks
now.
The
msdn documentation for OleSetContainedObject says that the implementation
changed for OLE 2.01 http://msdn.microsoft.com/library/default.asp?url="">
I am
not sure if this is a bug or something wrong with my implementation. Please
find the relevant code below.
Environment
details: Win XP / Office 2003 / Eclipse3.2M4 / jdk1.4.2_08
Any
help would be greatly appreciated folks.
TIA
Aditya
/********************************************************/
public
static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
OleControlSite controlSite;
OleAutomation application;
OleAutomation workbook ;
try {
OleFrame frame = new OleFrame(shell, SWT.NONE);
controlSite = new OleControlSite(frame, SWT.None,
"Excel.Application");
} catch (SWTError e) {
System.out.println("Unable to open activeX
control");
return;
}
application = new OleAutomation(controlSite);
int[] dispIDs = application.getIDsOfNames(new String[]
{"Workbooks"});
Variant pVarResult = application.getProperty(dispIDs[0]);
OleAutomation workbooks = pVarResult.getAutomation();
pVarResult.dispose();
final int[] dispMIDs = workbooks.getIDsOfNames(new String[]
{"Open"});
Variant[] arguments = new Variant[]{new
Variant("C:\\excel test\\Book1.xls")};
if ((dispMIDs != null) && (dispMIDs.length > 0))
{
workbook=
workbooks.invoke(dispMIDs[0],arguments).getAutomation();
}
workbooks.dispose();
dispIDs = application.getIDsOfNames(new String[]
{"Visible"});
pVarResult = application.getProperty(dispIDs[0]);
application.setProperty(dispIDs[0], new Variant(true));
pVarResult.dispose();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
application.dispose();
display.dispose ();
}
/********************************************************/
PS: I
had posted this problem initially at eclipse.platform.swt. The original is here-
http://www.eclipse.org/newsportal/article.php?id=26779&group=eclipse.platform.swt#26779