use of com.sun.jna.platform.win32.WinNT.HRESULT in project jna by java-native-access.
the class HybdridCOMInvocationTest method testOfficeInvocationDemonstration.
public void testOfficeInvocationDemonstration() {
// THIS IS NOT A TEST
//
// This reproduces the problem by using the dispatch directly.
PointerByReference pDispatch = new PointerByReference();
HRESULT hr = Ole32.INSTANCE.CoCreateInstance(CLSID_WORD, null, WTypes.CLSCTX_SERVER, IID_APPLICATION, pDispatch);
if (!COMUtils.SUCCEEDED(hr)) {
LOG.log(Level.INFO, "HybdridCOMInvocationTest test was not run, MS Word object could not be instantiated.");
return;
}
Dispatch dp = new Dispatch(pDispatch.getValue());
// DispID of InchesToPoints
DISPID dispId = new OaIdl.DISPID(0x00000172);
// Interface _Application of MS Word type library
WinDef.LCID LOCALE_SYSTEM_DEFAULT = Kernel32.INSTANCE.GetSystemDefaultLCID();
Variant.VARIANT.ByReference result = new Variant.VARIANT.ByReference();
OaIdl.EXCEPINFO.ByReference pExcepInfo = new OaIdl.EXCEPINFO.ByReference();
IntByReference puArgErr = new IntByReference();
WORD wFlagsMethod = new WinDef.WORD(OleAuto.DISPATCH_METHOD);
WORD wFlagsGet = new WinDef.WORD(OleAuto.DISPATCH_PROPERTYGET);
WORD wFlagsCombined = new WinDef.WORD(OleAuto.DISPATCH_METHOD | OleAuto.DISPATCH_PROPERTYGET);
OleAuto.DISPPARAMS.ByReference pDispParams = new OleAuto.DISPPARAMS.ByReference();
VARIANT[] params = new VARIANT[] { new VARIANT(1f) };
pDispParams.setArgs(params);
// Call InchesToPoints as a method
hr = dp.Invoke(dispId, new REFIID(Guid.IID_NULL), LOCALE_SYSTEM_DEFAULT, wFlagsMethod, pDispParams, result, pExcepInfo, puArgErr);
assertTrue(COMUtils.FAILED(hr));
// Call InchesToPoints as a property getter
hr = dp.Invoke(dispId, new REFIID(Guid.IID_NULL), LOCALE_SYSTEM_DEFAULT, wFlagsGet, pDispParams, result, pExcepInfo, puArgErr);
assertTrue(COMUtils.FAILED(hr));
// Call InchesToPoints as a hybrid
hr = dp.Invoke(dispId, new REFIID(Guid.IID_NULL), LOCALE_SYSTEM_DEFAULT, wFlagsCombined, pDispParams, result, pExcepInfo, puArgErr);
assertTrue(COMUtils.SUCCEEDED(hr));
assertEquals(72.0f, result.floatValue());
}
use of com.sun.jna.platform.win32.WinNT.HRESULT in project jna by java-native-access.
the class Kernel32UtilTest method testFreeGlobalMemory.
public void testFreeGlobalMemory() {
try {
Pointer ptr = new Pointer(0xFFFFFFFFFFFFFFFFL);
Kernel32Util.freeGlobalMemory(ptr);
fail("Unexpected success to free bad global memory");
} catch (Win32Exception e) {
HRESULT hr = e.getHR();
int code = W32Errors.HRESULT_CODE(hr.intValue());
assertEquals("Mismatched failure reason code", WinError.ERROR_INVALID_HANDLE, code);
}
}
use of com.sun.jna.platform.win32.WinNT.HRESULT in project jna by java-native-access.
the class Kernel32UtilTest method testFreeLocalMemory.
public void testFreeLocalMemory() {
try {
Pointer ptr = new Pointer(0xFFFFFFFFFFFFFFFFL);
Kernel32Util.freeLocalMemory(ptr);
fail("Unexpected success to free bad local memory");
} catch (Win32Exception e) {
HRESULT hr = e.getHR();
int code = W32Errors.HRESULT_CODE(hr.intValue());
assertEquals("Mismatched failure reason code", WinError.ERROR_INVALID_HANDLE, code);
}
}
use of com.sun.jna.platform.win32.WinNT.HRESULT in project jna by java-native-access.
the class ProxyObject method advise.
public IComEventCallbackCookie advise(Class<?> comEventCallbackInterface, final IComEventCallbackListener comEventCallbackListener) {
assert COMUtils.comIsInitialized() : "COM not initialized";
try {
ComInterface comInterfaceAnnotation = comEventCallbackInterface.getAnnotation(ComInterface.class);
if (null == comInterfaceAnnotation) {
throw new COMException("advise: Interface must define a value for either iid via the ComInterface annotation");
}
final IID iid = this.getIID(comInterfaceAnnotation);
final ConnectionPoint rawCp = this.fetchRawConnectionPoint(iid);
// create the dispatch listener
final IDispatchCallback rawListener = factory.createDispatchCallback(comEventCallbackInterface, comEventCallbackListener);
// store it the comEventCallback argument, so it is not garbage
// collected.
comEventCallbackListener.setDispatchCallbackListener(rawListener);
// set the dispatch listener to listen to events from the connection
// point
final DWORDByReference pdwCookie = new DWORDByReference();
HRESULT hr = rawCp.Advise(rawListener, pdwCookie);
// release before check in case check
int n = rawCp.Release();
// throws exception
COMUtils.checkRC(hr);
// return the cookie so that a call to stop listening can be made
return new ComEventCallbackCookie(pdwCookie.getValue());
} catch (Exception e) {
throw new COMException("Error occured in advise when trying to connect the listener " + comEventCallbackListener, e);
}
}
use of com.sun.jna.platform.win32.WinNT.HRESULT in project jna by java-native-access.
the class ProxyObject method unadvise.
public void unadvise(Class<?> comEventCallbackInterface, final IComEventCallbackCookie cookie) {
assert COMUtils.comIsInitialized() : "COM not initialized";
try {
ComInterface comInterfaceAnnotation = comEventCallbackInterface.getAnnotation(ComInterface.class);
if (null == comInterfaceAnnotation) {
throw new COMException("unadvise: Interface must define a value for iid via the ComInterface annotation");
}
IID iid = this.getIID(comInterfaceAnnotation);
final ConnectionPoint rawCp = this.fetchRawConnectionPoint(iid);
HRESULT hr = rawCp.Unadvise(((ComEventCallbackCookie) cookie).getValue());
rawCp.Release();
COMUtils.checkRC(hr);
} catch (Exception e) {
throw new COMException("Error occured in unadvise when trying to disconnect the listener from " + this, e);
}
}
Aggregations