?? oleautomation.java
字號:
/******************************************************************************* * Copyright (c) 2000, 2003 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.swt.ole.win32;import org.eclipse.swt.internal.ole.win32.*;import org.eclipse.swt.internal.win32.*;/** * OleAutomation provides a generic mechanism for accessing functionality that is * specific to a particular ActiveX Control or OLE Document. * * <p>The OLE Document or ActiveX Control must support the IDispatch interface in order to provide * OleAutomation support. The additional functionality provided by the OLE Object is specified in * its IDL file. The additional methods can either be to get property values (<code>getProperty</code>), * to set property values (<code>setProperty</code>) or to invoke a method (<code>invoke</code> or * <code>invokeNoReply</code>). Arguments are passed around in the form of <code>Variant</code> * objects. * * <p>Here is a sample IDL fragment: * * <pre> * interface IMyControl : IDispatch * { * [propget, id(0)] HRESULT maxFileCount([retval, out] int *c); * [propput, id(0)] HRESULT maxFileCount([in] int c); * [id(1)] HRESULT AddFile([in] BSTR fileName); * }; * </pre> * * <p>An example of how to interact with this extended functionality is shown below: * * <code><pre> * OleAutomation automation = new OleAutomation(myControlSite); * * // Look up the ID of the maxFileCount parameter * int[] rgdispid = automation.getIDsOfNames(new String[]{"maxFileCount"}); * int maxFileCountID = rgdispid[0]; * * // Set the property maxFileCount to 100: * if (automation.setProperty(maxFileCountID, new Variant(100))) { * System.out.println("Max File Count was successfully set."); * } * * // Get the new value of the maxFileCount parameter: * Variant pVarResult = automation.getProperty(maxFileCountID); * if (pVarResult != null) { * System.out.println("Max File Count is "+pVarResult.getInt()); * } * * // Invoke the AddFile method * // Look up the IDs of the AddFile method and its parameter * rgdispid = automation.getIDsOfNames(new String[]{"AddFile", "fileName"}); * int dispIdMember = rgdispid[0]; * int[] rgdispidNamedArgs = new int[] {rgdispid[1]}; * * // Convert arguments to Variant objects * Variant[] rgvarg = new Variant[1]; * String fileName = "C:\\testfile"; * rgvarg[0] = new Variant(fileName); * * // Call the method * Variant pVarResult = automation.invoke(dispIdMember, rgvarg, rgdispidNamedArgs); * * // Check the return value * if (pVarResult == null || pVarResult.getInt() != OLE.S_OK){ * System.out.println("Failed to add file "+fileName); * } * * automation.dispose(); * * </pre></code> */public final class OleAutomation { private IDispatch objIDispatch; private String exceptionDescription; private ITypeInfo objITypeInfo; OleAutomation(IDispatch idispatch) { if (idispatch == null) OLE.error(OLE.ERROR_INVALID_INTERFACE_ADDRESS); objIDispatch = idispatch; objIDispatch.AddRef(); int[] ppv = new int[1]; int result = objIDispatch.GetTypeInfo(0, COM.LOCALE_USER_DEFAULT, ppv); if (result == OLE.S_OK) { objITypeInfo = new ITypeInfo(ppv[0]); objITypeInfo.AddRef(); }}/** * Creates an OleAutomation object for the specified client. * * @param clientSite the site for the OLE Document or ActiveX Control whose additional functionality * you need to access * * @exception SWTError <ul> * <li>ERROR_INVALID_INTERFACE_ADDRESS when called with an invalid client site * </ul> */ public OleAutomation(OleClientSite clientSite) { if (clientSite == null) OLE.error(OLE.ERROR_INVALID_INTERFACE_ADDRESS); objIDispatch = clientSite.getAutomationObject(); int[] ppv = new int[1]; int result = objIDispatch.GetTypeInfo(0, COM.LOCALE_USER_DEFAULT, ppv); if (result == OLE.S_OK) { objITypeInfo = new ITypeInfo(ppv[0]); objITypeInfo.AddRef(); } }/** * Disposes the automation object. * <p> * This method releases the IDispatch interface on the OLE Document or ActiveX Control. * Do not use the OleAutomation object after it has been disposed. */public void dispose() { if (objIDispatch != null){ objIDispatch.Release(); } objIDispatch = null; if (objITypeInfo != null){ objITypeInfo.Release(); } objITypeInfo = null;}int getAddress() { return objIDispatch.getAddress();}public String getHelpFile(int dispId) { if (objITypeInfo == null) return null; String[] file = new String[1]; int rc = objITypeInfo.GetDocumentation(dispId, null, null, null, file ); if (rc == OLE.S_OK) return file[0]; return null; }public String getDocumentation(int dispId) { if (objITypeInfo == null) return null; String[] doc = new String[1]; int rc = objITypeInfo.GetDocumentation(dispId, null, doc, null, null ); if (rc == OLE.S_OK) return doc[0]; return null;}public OlePropertyDescription getPropertyDescription(int index) { if (objITypeInfo == null) return null; int[] ppVarDesc = new int[1]; int rc = objITypeInfo.GetVarDesc(index, ppVarDesc); if (rc != OLE.S_OK) return null; VARDESC1 vardesc = new VARDESC1(); COM.MoveMemory(vardesc, ppVarDesc[0], VARDESC1.sizeof); OlePropertyDescription data = new OlePropertyDescription(); data.id = vardesc.memid; data.name = getName(vardesc.memid); data.type = vardesc.elemdescVar_tdesc_vt; if (data.type == OLE.VT_PTR) { short[] vt = new short[1]; COM.MoveMemory(vt, vardesc.elemdescVar_tdesc_union + 4, 2); data.type = vt[0]; } data.flags = vardesc.wVarFlags; data.kind = vardesc.varkind; data.description = getDocumentation(vardesc.memid); data.helpFile = getHelpFile(vardesc.memid); objITypeInfo.ReleaseVarDesc(ppVarDesc[0]); return data;}public OleFunctionDescription getFunctionDescription(int index) { if (objITypeInfo == null) return null; int[] ppFuncDesc = new int[1]; int rc = objITypeInfo.GetFuncDesc(index, ppFuncDesc); if (rc != OLE.S_OK) return null; FUNCDESC1 funcdesc = new FUNCDESC1(); COM.MoveMemory(funcdesc, ppFuncDesc[0], FUNCDESC1.sizeof); OleFunctionDescription data = new OleFunctionDescription(); data.id = funcdesc.memid; data.optionalArgCount = funcdesc.cParamsOpt; data.invokeKind = funcdesc.invkind; data.funcKind = funcdesc.funckind; data.flags = funcdesc.wFuncFlags; data.callingConvention = funcdesc.callconv; data.documentation = getDocumentation(funcdesc.memid); data.helpFile = getHelpFile(funcdesc.memid); String[] names = getNames(funcdesc.memid, funcdesc.cParams + 1); if (names.length > 0) { data.name = names[0]; } data.args = new OleParameterDescription[funcdesc.cParams]; for (int i = 0; i < data.args.length; i++) { data.args[i] = new OleParameterDescription(); if (names.length > i + 1) { data.args[i].name = names[i + 1]; } short[] vt = new short[1]; COM.MoveMemory(vt, funcdesc.lprgelemdescParam + i * 16 + 4, 2); if (vt[0] == OLE.VT_PTR) { int[] pTypedesc = new int[1]; COM.MoveMemory(pTypedesc, funcdesc.lprgelemdescParam + i * 16, 4); short[] vt2 = new short[1]; COM.MoveMemory(vt2, pTypedesc[0] + 4, 2); vt[0] = (short)(vt2[0] | COM.VT_BYREF); } data.args[i].type = vt[0]; short[] wParamFlags = new short[1]; COM.MoveMemory(wParamFlags, funcdesc.lprgelemdescParam + i * 16 + 12, 2); data.args[i].flags = wParamFlags[0]; } data.returnType = funcdesc.elemdescFunc_tdesc_vt; if (data.returnType == OLE.VT_PTR) { short[] vt = new short[1]; COM.MoveMemory(vt, funcdesc.elemdescFunc_tdesc_union + 4, 2); data.returnType = vt[0]; } objITypeInfo.ReleaseFuncDesc(ppFuncDesc[0]); return data;}public TYPEATTR getTypeInfoAttributes() { if (objITypeInfo == null) return null; int[] ppTypeAttr = new int[1]; int rc = objITypeInfo.GetTypeAttr(ppTypeAttr); if (rc != OLE.S_OK) return null; TYPEATTR typeattr = new TYPEATTR(); COM.MoveMemory(typeattr, ppTypeAttr[0], TYPEATTR.sizeof); objITypeInfo.ReleaseTypeAttr(ppTypeAttr[0]); return typeattr;}public String getName(int dispId) { if (objITypeInfo == null) return null; String[] name = new String[1]; int rc = objITypeInfo.GetDocumentation(dispId, name, null, null, null ); if (rc == OLE.S_OK) return name[0]; return null;}public String[] getNames(int dispId, int maxSize) { if (objITypeInfo == null) return new String[0]; String[] names = new String[maxSize]; int[] count = new int[1]; int rc = objITypeInfo.GetNames(dispId, names, maxSize, count); if (rc == OLE.S_OK) { String[] newNames = new String[count[0]]; System.arraycopy(names, 0, newNames, 0, count[0]); return newNames; } return new String[0];}/** * Returns the positive integer values (IDs) that are associated with the specified names by the * IDispatch implementor. If you are trying to get the names of the parameters in a method, the first * String in the names array must be the name of the method followed by the names of the parameters. * * @param names an array of names for which you require the identifiers * * @return positive integer values that are associated with the specified names in the same * order as the names where provided; or null if the names are unknown */public int[] getIDsOfNames(String[] names) { int[] rgdispid = new int[names.length]; int result = objIDispatch.GetIDsOfNames(new GUID(), names, names.length, COM.LOCALE_USER_DEFAULT, rgdispid); if (result != COM.S_OK) return null; return rgdispid;}/** * Returns a description of the last error encountered. * * @return a description of the last error encountered */public String getLastError() { return exceptionDescription;}/** * Returns the value of the property specified by the dispIdMember. * * @param dispIdMember the ID of the property as specified by the IDL of the ActiveX Control; the * value for the ID can be obtained using OleAutomation.getIDsOfNames * * @return the value of the property specified by the dispIdMember or null */public Variant getProperty(int dispIdMember) { Variant pVarResult = new Variant(); int result = invoke(dispIdMember, COM.DISPATCH_PROPERTYGET, null, null, pVarResult);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -