You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
From my understanding, this method should only return a Proxy if an instance of the requested application is already running.
But instead, it throws an exception. This is not the expected behaviour of a get/fetch method.
Throwing an exception here would point to some internal problems, which do not appear in either cases.
It also makes checking for a running app more complicated than necessary.
My solution: simply replace the call
COMUtils.checkRC(hr);
with the check
if(COMUtils.FAILED(hr)) return null;
/**
* Gets and existing COM object (GetActiveObject) for the given progId and
* returns a ProxyObject for the given interface.
*/
public <T> T fetchObject(Class<T> comInterface) {
assert COMUtils.comIsInitialized() : "COM not initialized";
ComObject comObectAnnotation = comInterface.getAnnotation(ComObject.class);
if (null == comObectAnnotation) {
throw new COMException(
"createObject: Interface must define a value for either clsId or progId via the ComInterface annotation");
}
final GUID guid = this.discoverClsId(comObectAnnotation);
final PointerByReference ptrDisp = new PointerByReference();
WinNT.HRESULT hr = OleAuto.INSTANCE.GetActiveObject(guid, null, ptrDisp);
// COMUtils.checkRC(hr);
if(COMUtils.FAILED(hr)) return null;
Dispatch d = new Dispatch(ptrDisp.getValue());
T t = this.createProxy(comInterface, d);
//GetActiveObject returns a pointer to COM object with a +1 reference count, so we must drop one
//Note: the createProxy adds one
d.Release();
return t;
}
In addition, you should also add a method fetchOrCreateObject(), like this.
public <T> T fetchOrCreateObject(final Class<T> comInterface) {
T ret = fetchObject(comInterface);
if(ret != null) return ret;
return createObject(comInterface);
}
The text was updated successfully, but these errors were encountered:
The described behavior can be accomplished by handling the COMException and checking the HRESULT. See the commit message for the first commit in the PR: 2581ef6
From my understanding, this method should only return a Proxy if an instance of the requested application is already running.
But instead, it throws an exception. This is not the expected behaviour of a get/fetch method.
Throwing an exception here would point to some internal problems, which do not appear in either cases.
It also makes checking for a running app more complicated than necessary.
My solution: simply replace the call
In addition, you should also add a method fetchOrCreateObject(), like this.
The text was updated successfully, but these errors were encountered: