use of com.sun.jna.platform.win32.COM.util.annotation.ComObject in project jna by java-native-access.
the class ObjectFactory method fetchObject.
/**
* 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);
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;
}
use of com.sun.jna.platform.win32.COM.util.annotation.ComObject in project jna by java-native-access.
the class ObjectFactory method discoverClsId.
GUID discoverClsId(ComObject annotation) {
assert COMUtils.comIsInitialized() : "COM not initialized";
String clsIdStr = annotation.clsId();
final String progIdStr = annotation.progId();
if (null != clsIdStr && !clsIdStr.isEmpty()) {
return new CLSID(clsIdStr);
} else if (null != progIdStr && !progIdStr.isEmpty()) {
final CLSID.ByReference rclsid = new CLSID.ByReference();
WinNT.HRESULT hr = Ole32.INSTANCE.CLSIDFromProgID(progIdStr, rclsid);
COMUtils.checkRC(hr);
return rclsid;
} else {
throw new COMException("ComObject must define a value for either clsId or progId");
}
}
use of com.sun.jna.platform.win32.COM.util.annotation.ComObject in project jna by java-native-access.
the class ObjectFactory method createObject.
/**
* Creates a new COM object (CoCreateInstance) for the given progId and
* returns a ProxyObject for the given interface.
*/
public <T> T createObject(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 = Ole32.INSTANCE.CoCreateInstance(guid, null, WTypes.CLSCTX_SERVER, IDispatch.IID_IDISPATCH, ptrDisp);
COMUtils.checkRC(hr);
Dispatch d = new Dispatch(ptrDisp.getValue());
T t = this.createProxy(comInterface, d);
//CoCreateInstance returns a pointer to COM object with a +1 reference count, so we must drop one
//Note: the createProxy adds one
int n = d.Release();
return t;
}
Aggregations