Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 318942

Summary: [OLE] new OleAutomation(progId) fails because of missing flag
Product: [Eclipse Project] Platform Reporter: Christopher Deckers <chrriis>
Component: SWTAssignee: Nikita Nemkin <nikita>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: dimitar.donchev, eclipse.felipe, email, grant_gayed, Lars.Vogel, mike.blocker, mlehmannm, nicolas.sarazin.pro, nikita, niraj.modi, pinnau, robert.simons, zygota.v2
Version: 3.6   
Target Milestone: 4.8 M7   
Hardware: PC   
OS: Windows 7   
See Also: https://git.eclipse.org/r/121062
https://git.eclipse.org/c/platform/eclipse.platform.swt.git/commit/?id=66bbce07c55f39dcf5080ace1ab49e9e4c9f379c
Whiteboard:
Attachments:
Description Flags
PR318942_Example.java none

Description Christopher Deckers CLA 2010-07-05 17:09:29 EDT
Build Identifier: 3.6

Creating an instance of an OLE component (not embedded) fails:
OleAutomation application = new OleAutomation("Excel.Application");
=> org.eclipse.swt.SWTException: Failed to create Ole Client. result = -2147221164

If we change the code in the OleAutomation(String) constructor to add the "COM.CLSCTX_LOCAL_SERVER" flag, then it works:
int result = COM.CoCreateInstance(appClsid, 0, COM.CLSCTX_LOCAL_SERVER | COM.CLSCTX_INPROC_SERVER, COM.IIDIUnknown, ppvObject);


Reproducible: Always

Steps to Reproduce:
1. new OleAutomation("Excel.Application");
Comment 1 Felipe Heidrich CLA 2010-08-12 15:59:57 EDT
Fixed in HEAD > 20100812
Comment 2 Peter Pinnau CLA 2011-06-16 09:18:57 EDT
Same problem with a fresh install:

---
Eclipse for RCP and RAP Developers

Version: Helios Service Release 2
Build id: 20110218-0911
---

Why is the "COM.CLSCTX_LOCAL_SERVER" flag is still missing in my sources?
Comment 3 Felipe Heidrich CLA 2011-06-17 10:31:40 EDT
The code is not in Helios, Indigo only.
Comment 4 Peter Pinnau CLA 2011-06-22 09:25:01 EDT
Switched to Indigo and problem remains. The COM.CLSCTX_LOCAL_SERVER flag is only added in case of excel automation:

if (progId.startsWith("Excel")) flags |= COM.CLSCTX_LOCAL_SERVER;

Since I work with MS Word I can tell you that it is needed for Word automation, too.
Probably it's the same with all MS Office products.
Comment 5 Felipe Heidrich CLA 2011-06-22 10:16:24 EDT
Okay, I can't do anything for Indigo anymore. Lets target this for 3.8
Comment 6 Bob CLA 2011-11-29 14:01:47 EST
Is it possible for this fix to come as part of a patch update. I have an application that uses Outlook as a key component. My entire office is in the process of migrating to Windows 7, and this causes a major issue for me.
Comment 7 Felipe Heidrich CLA 2011-11-29 15:36:00 EST
Hey Bob, what version of Eclipse are you using ?

Can you provide a patch for this bug ?
It seems that all that is needed is to set COM.CLSCTX_LOCAL_SERVER in more cases than progId.startsWith("Excel")

Is that the fix for you too ?
Comment 8 Bob CLA 2011-11-29 15:49:05 EST
(In reply to comment #7)
> Hey Bob, what version of Eclipse are you using ?
> Can you provide a patch for this bug ?
> It seems that all that is needed is to set COM.CLSCTX_LOCAL_SERVER in more
> cases than progId.startsWith("Excel")
> Is that the fix for you too ?

I just got Indigo, and am running SWT 64-bit 3.7.1

I think that fix will work for me. The only difference with my current code is that I am making use of OleControlSite, then declaring 
    
   OleAutomation(OleControlSite)

Looking at that source, I don't see any of the CoCreate logic that is part of 

   OleAutomation(String)

However, if any fix can be applied, I'd take it, if there is no workaround.

I would hate to re-tool all my OLE code to use some other means.
Comment 9 Felipe Heidrich CLA 2011-12-01 10:10:55 EST
Robert, the fix suggested here goes in:

OleAutomation(String)

If you are saying you are not using the method then how is this going to help you ?

Try to get SWT source and try differents fixes ?
See http://www.eclipse.org/swt/git.php

You can also attach the snippet to this bug (or open a new one) so I can try to reproduce your problem. The sooner I'll have time to look into this is next week.
Comment 10 Bob CLA 2011-12-01 16:21:17 EST
(In reply to comment #9)
> Robert, the fix suggested here goes in:
> OleAutomation(String)
> If you are saying you are not using the method then how is this going to help
> you ?
> Try to get SWT source and try differents fixes ?
> See http://www.eclipse.org/swt/git.php
> You can also attach the snippet to this bug (or open a new one) so I can try to
> reproduce your problem. The sooner I'll have time to look into this is next
> week.

I figured how to export as a jar, but I am running Windows 7 64 bit. How do I export the SWT as 64 bit?
Comment 11 Felipe Heidrich CLA 2011-12-02 11:55:58 EST
> I figured how to export as a jar, but I am running Windows 7 64 bit. How do I
> export the SWT as 64 bit?

it would probable be easier to run with 32bit eclipse on a 32bit VM.
Anyhow, get the bundle org.eclipse.swt.win32.win32.x86_64 (instead of org.eclipse.swt.win32.win32.x86) and export that as Deployable plug-ins and fragments. I think that should take care of the 32->64 java code conversion before compiling and packing all up.
it that doesn't work try running the task replace.32.to.64 in org.eclipse.swt/buildSWT.xml and then export the jars the usual way.
Comment 12 Nicolas Sarazin CLA 2013-12-10 10:55:49 EST
Hello,

This bug is alwais present in Helios RCP distibution :

org.eclipse.swt.SWTException: Failed to create Ole Client. result = -2147221164
	at org.eclipse.swt.ole.win32.OLE.error(OLE.java:302)
	at org.eclipse.swt.ole.win32.OleAutomation.<init>(OleAutomation.java:159)

With this code :

OleAutomation outlook = new OleAutomation("Outlook.Application");

Is a correction planned for other MS applications?
Comment 13 Nicolas Sarazin CLA 2013-12-10 11:25:50 EST
Kepler RCP distribution, sorry for the mistake...
Comment 14 Zygmunt Horodyski CLA 2015-08-20 02:28:09 EDT
Any update on this case? Solving this is very easy. Just do following: (simple, retains backward compatibility, and do the job)

public OleAutomation(String progId, int flags) {
	initialize(progId, flags);
}

public OleAutomation(String progId) {
	int flags = COM.CLSCTX_INPROC_SERVER;
	if (progId.startsWith("Excel")) flags |= COM.CLSCTX_LOCAL_SERVER;
	
	initialize(progId, flags);
}

public void initialize(String progId, int flags) {
	try {
		OS.OleInitialize(0);
		GUID appClsid = getClassID(progId);
		if (appClsid == null) {
			OS.OleUninitialize();
			OLE.error(OLE.ERROR_INVALID_CLASSID);
		}
		
		long /*int*/[] ppvObject = new long /*int*/[1];
		int result = COM.CoCreateInstance(appClsid, 0, flags, COM.IIDIUnknown, ppvObject); 
		if (result != COM.S_OK) {
			OS.OleUninitialize();
			OLE.error(OLE.ERROR_CANNOT_CREATE_OBJECT, result);
		}
		objIUnknown = new IUnknown(ppvObject[0]);
		
		ppvObject[0] = 0;
		result = objIUnknown.QueryInterface(COM.IIDIDispatch, ppvObject); 
		if (result != COM.S_OK) OLE.error(OLE.ERROR_INTERFACE_NOT_FOUND);
		objIDispatch = new IDispatch(ppvObject[0]);

		ppvObject[0] = 0;
		result = objIDispatch.GetTypeInfo(0, COM.LOCALE_USER_DEFAULT, ppvObject);
		if (result == OLE.S_OK) {
			objITypeInfo = new ITypeInfo(ppvObject[0]);
		}
	} catch (SWTException e) {
		dispose();
		throw e;
	}
}
Comment 15 Niraj Modi CLA 2015-08-25 04:39:00 EDT
(In reply to Zygmunt Horodyski from comment #14)
> Any update on this case? Solving this is very easy. Just do following:
> (simple, retains backward compatibility, and do the job)
> 

Do you have a test snippet handy, which reproduces this problem ?
Comment 16 Zygmunt Horodyski CLA 2015-08-25 06:10:59 EDT
something simple like that:


import org.eclipse.swt.ole.win32.OleAutomation;
import org.junit.Test;

public class OleAutomationTest {

    @Test
    public void testThatLocalServerApplicationIsAccessible() {
        new OleAutomation("Outlook.Application");

        // No error has occurred
    }

}


Actually, I'm not using Outlook.Application in my case but company software, but this two has the same problem.
Comment 17 Niraj Modi CLA 2015-08-25 08:30:02 EDT
(In reply to Zygmunt Horodyski from comment #16)
> something simple like that:
> 
> 
> import org.eclipse.swt.ole.win32.OleAutomation;
> import org.junit.Test;
> 
> public class OleAutomationTest {
> 
>     @Test
>     public void testThatLocalServerApplicationIsAccessible() {
>         new OleAutomation("Outlook.Application");
> 
>         // No error has occurred
>     }
> 
> }
> 
> 
> Actually, I'm not using Outlook.Application in my case but company software,
> but this two has the same problem.

Same here, I'm also not using Outlook.. 
Please suggest any alternate out there in open/public to try ?
Comment 18 Zygmunt Horodyski CLA 2015-08-25 09:01:24 EDT
There are also applications like InternetExplorer.Application.1

Easiest way to find application that needs to be started as COM.CLSCTX_LOCAL_SERVER is to open registry and under HKEY_CLASSES_ROOT\CLSID search for LocalServer32 and if you will find one then if there is folder ProgID you can read its progId there.
Comment 19 Zygmunt Horodyski CLA 2015-12-01 04:46:16 EST
Any update here?
Comment 20 Peter Pinnau CLA 2017-11-09 11:05:54 EST
Problem still exists in Neon. I work on a project with Outlook. Version 2016 works but with Outlook 2010 the error occurs.

Passing the flag COM.CLSCTX_LOCAL_SERVER still does the trick. Outlook 2016 still works.

Unfortunately OleAutomation is a final class. Currently there is no other way than copy some OLE java-sources into the project to pass the flag.


Another conctructor to pass the COM.CLSCTX_LOCAL_SERVER flag will be helpful as a workaround.
Comment 21 Nikita Nemkin CLA 2018-04-12 07:33:45 EDT
Created attachment 273554 [details]
PR318942_Example.java

Local COM server access example: open eclipse.org in a separate MSIE window using SHDocVw.InternetExplorer object.
Comment 22 Eclipse Genie CLA 2018-04-12 07:35:10 EDT
New Gerrit change created: https://git.eclipse.org/r/121062
Comment 24 Lars Vogel CLA 2018-04-15 11:29:28 EDT
Thanks Nikita for the patch. Would it be possible to add a test for Windows to ensure this will also work in future versions?