use of com.sun.jna.platform.win32.Variant.VARIANT in project jna by java-native-access.
the class MSWord method SaveAs.
public void SaveAs(String FileName, LONG FileFormat) throws COMException {
VARIANT vtFileName = new VARIANT(FileName);
VARIANT vtFileFormat = new VARIANT(FileFormat);
this.invokeNoReply("SaveAs", this.getActiveDocument(), vtFileName, vtFileFormat);
}
use of com.sun.jna.platform.win32.Variant.VARIANT in project jna by java-native-access.
the class Excelautomation_KB_219151_Mod method main.
public static void main(String[] args) throws IOException {
// Initialize COM Subsystem
Ole32.INSTANCE.CoInitializeEx(Pointer.NULL, Ole32.COINIT_MULTITHREADED);
// Initialize Factory for COM object creation
Factory fact = new Factory();
// Set LCID for calls to english locale. Without this formulas need
// to be specified in the users locale.
fact.setLCID(new LCID(0x0409));
try {
// Start excel application
ComExcel_Application excel = fact.createObject(ComExcel_Application.class);
ComIApplication excelApp = excel.queryInterface(ComIApplication.class);
// Set visiblite of application
excelApp.setVisible(true);
Helper.sleep(5);
// Get a new workbook.
ComIWorkbook wb = excelApp.getWorkbooks().Add();
ComIWorksheet sheet = wb.getActiveSheet();
// Add table headers going cell by cell.
sheet.getCells().getItem(1, 1).setValue("First Name");
sheet.getCells().getItem(1, 2).setValue("Last Name");
sheet.getCells().getItem(1, 3).setValue("Full Name");
sheet.getCells().getItem(1, 4).setValue("Salary");
// Create an array to set multiple values at once.
SAFEARRAY saNames = safeVariantArrayFromJava(new String[][] { { "John", "Smith" }, { "Tom", "Brown" }, { "Sue", "Thomas" }, { "Jane", "Jones" }, { "Adam", "Johnson" } });
// Fill A2:B6 with an array of values (First and Last Names).
VARIANT valueHolder = new VARIANT();
valueHolder.setValue(Variant.VT_ARRAY | Variant.VT_VARIANT, saNames);
sheet.getRange("A2", "B6").setValue(valueHolder);
saNames.destroy();
// Fill C2:C6 with a relative formula (=A2 & " " & B2).
sheet.getRange("C2", "C6").setFormula("= A2 & \" \" & B2");
// Fill D2:D6 with a formula(=RAND()*100000) and apply format.
sheet.getRange("D2", "D6").setFormula("=RAND()*100000");
sheet.getRange("D2", "D6").setNumberFormat("$0.00");
// AutoFit columns A:D.
sheet.getRange("A1", "D2").getEntireColumn().AutoFit();
displayQuaterlySales(sheet);
File tempFile = Helper.createNotExistingFile("exceloutput", ".xlsx");
System.out.println("Writing output to: " + tempFile.getAbsolutePath());
wb.SaveAs(tempFile.getAbsolutePath());
excelApp.setUserControl(true);
} finally {
fact.disposeAll();
Ole32.INSTANCE.CoUninitialize();
}
System.exit(0);
}
use of com.sun.jna.platform.win32.Variant.VARIANT in project jna by java-native-access.
the class COMBindingBaseObject method oleMethod.
protected HRESULT oleMethod(int nType, VARIANT.ByReference pvResult, IDispatch pDisp, String name, VARIANT[] pArgs) throws COMException {
if (pDisp == null)
throw new COMException("pDisp (IDispatch) parameter is null!");
// variable declaration
WString[] ptName = new WString[] { new WString(name) };
DISPIDByReference pdispID = new DISPIDByReference();
// Get DISPID for name passed...
HRESULT hr = pDisp.GetIDsOfNames(new REFIID(Guid.IID_NULL), ptName, 1, LOCALE_USER_DEFAULT, pdispID);
COMUtils.checkRC(hr);
return this.oleMethod(nType, pvResult, pDisp, pdispID.getValue(), pArgs);
}
use of com.sun.jna.platform.win32.Variant.VARIANT in project jna by java-native-access.
the class COMUtils method comIsInitialized.
/**
* Check if COM was initialized correctly. The initialization status is not changed!
*
* <p>This is a debug function, not for normal usage!</p>
*
* @return whether COM has been initialized
*/
public static boolean comIsInitialized() {
WinNT.HRESULT hr = Ole32.INSTANCE.CoInitializeEx(Pointer.NULL, Ole32.COINIT_MULTITHREADED);
if (hr.equals(W32Errors.S_OK)) {
// User failed - uninitialize again and return false
Ole32.INSTANCE.CoUninitialize();
return false;
} else if (hr.equals(W32Errors.S_FALSE)) {
// OK Variant 1 - User initialized COM with same threading module as
// in this check. According to MSDN CoUninitialize needs to be called
// in this case.
Ole32.INSTANCE.CoUninitialize();
return true;
} else if (hr.intValue() == W32Errors.RPC_E_CHANGED_MODE) {
return true;
}
// If another result than the checked ones above happens handling is
// delegated to the "normal" COM exception handling and a COMException
// will be raised.
COMUtils.checkRC(hr);
// The return will not be met, as COMUtils#checkRC will raise an exception
return false;
}
use of com.sun.jna.platform.win32.Variant.VARIANT in project jna by java-native-access.
the class Convert method toJavaObject.
public static Object toJavaObject(VARIANT value, Class<?> targetClass, ObjectFactory factory, boolean addReference, boolean freeValue) {
if (null == value || value.getVarType().intValue() == VT_EMPTY || value.getVarType().intValue() == VT_NULL) {
return null;
}
if (targetClass != null && (!targetClass.isAssignableFrom(Object.class))) {
if (targetClass.isAssignableFrom(value.getClass())) {
return value;
}
Object vobj = value.getValue();
if (vobj != null && (targetClass.isAssignableFrom(vobj.getClass()))) {
return vobj;
}
}
VARIANT inputValue = value;
if (value.getVarType().intValue() == (VT_BYREF | VT_VARIANT)) {
value = (VARIANT) value.getValue();
}
// handling
if (targetClass == null || (targetClass.isAssignableFrom(Object.class))) {
targetClass = null;
int varType = value.getVarType().intValue();
switch(value.getVarType().intValue()) {
case VT_UI1:
case VT_I1:
case VT_BYREF | VT_UI1:
case VT_BYREF | VT_I1:
targetClass = Byte.class;
break;
case VT_I2:
case VT_BYREF | VT_I2:
targetClass = Short.class;
break;
case VT_UI2:
case VT_BYREF | VT_UI2:
targetClass = Character.class;
break;
case VT_INT:
case VT_UINT:
case VT_UI4:
case VT_I4:
case VT_BYREF | VT_I4:
case VT_BYREF | VT_UI4:
case VT_BYREF | VT_INT:
case VT_BYREF | VT_UINT:
targetClass = Integer.class;
break;
case VT_UI8:
case VT_I8:
case VT_BYREF | VT_I8:
case VT_BYREF | VT_UI8:
targetClass = Long.class;
break;
case VT_R4:
case VT_BYREF | VT_R4:
targetClass = Float.class;
break;
case VT_R8:
case VT_BYREF | VT_R8:
targetClass = Double.class;
break;
case VT_BOOL:
case VT_BYREF | VT_BOOL:
targetClass = Boolean.class;
break;
case VT_ERROR:
case VT_BYREF | VT_ERROR:
targetClass = WinDef.SCODE.class;
break;
case VT_CY:
case VT_BYREF | VT_CY:
targetClass = OaIdl.CURRENCY.class;
break;
case VT_DATE:
case VT_BYREF | VT_DATE:
targetClass = Date.class;
break;
case VT_BSTR:
case VT_BYREF | VT_BSTR:
targetClass = String.class;
break;
case VT_UNKNOWN:
case VT_BYREF | VT_UNKNOWN:
targetClass = com.sun.jna.platform.win32.COM.IUnknown.class;
break;
case VT_DISPATCH:
case VT_BYREF | VT_DISPATCH:
targetClass = IDispatch.class;
break;
case VT_BYREF | VT_VARIANT:
targetClass = Variant.class;
break;
case VT_BYREF:
targetClass = PVOID.class;
break;
case VT_BYREF | VT_DECIMAL:
targetClass = OaIdl.DECIMAL.class;
break;
case VT_RECORD:
default:
if ((varType & VT_ARRAY) > 0) {
targetClass = OaIdl.SAFEARRAY.class;
}
}
}
Object result;
if (Byte.class.equals(targetClass) || byte.class.equals(targetClass)) {
result = value.byteValue();
} else if (Short.class.equals(targetClass) || short.class.equals(targetClass)) {
result = value.shortValue();
} else if (Character.class.equals(targetClass) || char.class.equals(targetClass)) {
result = (char) value.intValue();
} else if (Integer.class.equals(targetClass) || int.class.equals(targetClass)) {
result = value.intValue();
} else if (Long.class.equals(targetClass) || long.class.equals(targetClass) || IComEnum.class.isAssignableFrom(targetClass)) {
result = value.longValue();
} else if (Float.class.equals(targetClass) || float.class.equals(targetClass)) {
result = value.floatValue();
} else if (Double.class.equals(targetClass) || double.class.equals(targetClass)) {
result = value.doubleValue();
} else if (Boolean.class.equals(targetClass) || boolean.class.equals(targetClass)) {
result = value.booleanValue();
} else if (Date.class.equals(targetClass)) {
result = value.dateValue();
} else if (String.class.equals(targetClass)) {
result = value.stringValue();
} else if (value.getValue() instanceof com.sun.jna.platform.win32.COM.IDispatch) {
com.sun.jna.platform.win32.COM.IDispatch d = (com.sun.jna.platform.win32.COM.IDispatch) value.getValue();
Object proxy = factory.createProxy(targetClass, d);
// call
if (!addReference) {
int n = d.Release();
}
result = proxy;
} else {
/*
WinDef.SCODE.class.equals(targetClass)
|| OaIdl.CURRENCY.class.equals(targetClass)
|| OaIdl.DECIMAL.class.equals(targetClass)
|| OaIdl.SAFEARRAY.class.equals(targetClass)
|| com.sun.jna.platform.win32.COM.IUnknown.class.equals(targetClass)
|| Variant.class.equals(targetClass)
|| PVOID.class.equals(targetClass
*/
result = value.getValue();
}
if (IComEnum.class.isAssignableFrom(targetClass)) {
result = targetClass.cast(Convert.toComEnum((Class<? extends IComEnum>) targetClass, result));
}
if (freeValue) {
free(inputValue, result);
}
return result;
}
Aggregations