📄 dispatchptr.java
字号:
/*
* DispatchPtr.java -
*
* This file is part of the Jawin Project: http://jawinproject.sourceforge.net/
*
* Please consult the LICENSE file in the project root directory,
* or at the project site before using this software.
*/
/* $Id: DispatchPtr.java,v 1.8 2004/07/31 20:59:50 arosii_moa Exp $ */
package org.jawin;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.jawin.constants.DispatchConstants;
import org.jawin.constants.VarTypes;
import org.jawin.constants.WellKnownGUIDs;
import org.jawin.io.LittleEndianInputStream;
import org.jawin.io.LittleEndianOutputStream;
import org.jawin.io.NakedByteStream;
import org.jawin.marshal.GenericStub;
import org.jawin.win32.ITypeInfo;
/**
*
*
* @version $Revision: 1.8 $
* @author Stuart Halloway, http://www.relevancellc.com/halloway/weblog/<br>
* Morten Andersen, arosii_moa (at) users.sourceforge.net
*/
public class DispatchPtr extends COMPtr {
public static final GUID PROXY_IID = WellKnownGUIDs.IID_IDispatch;
public static final int IID_TOKEN;
static {
IID_TOKEN = IdentityManager.registerProxy(PROXY_IID, DispatchPtr.class);
}
/**
* call to initialize (work actually done in static initializer)
*/
static final void init() {}
/**
* should usually be overridden by subclasses, to return their registered
* interface id token. This default implementation returns the iid token
* for the IDispatch interface.
*/
public int getIIDToken() {
return IID_TOKEN;
}
/**
* The required public no arg constructor.
* <br><br>
* <b>Important:</b>Should never be used as this creates an uninitialized
* DispatchPtr (it is required by Jawin for some internal working though).
*/
public DispatchPtr() {
super();
}
/**
* For creating a new COM-object with the given progid and with
* the IDispatch interface.
*
* @param progid the progid of the COM-object to create.
*/
public DispatchPtr(String progid) throws COMException {
super(progid, PROXY_IID);
}
/**
* For creating a new COM-object with the given clsid and with
* the IDispatch interface.
*
* @param clsid the GUID of the COM-object to create.
*/
public DispatchPtr(GUID clsid) throws COMException {
super(clsid, PROXY_IID);
}
/**
* For creating a new COM-object with the given progid and iid.
* This should usually be called by subclasses.
*
* @param progid the progid of the COM-object to create.
* @param iid the interface id of the requested interface on the COM-object.
*/
protected DispatchPtr(String progid, GUID iid) throws COMException {
super(progid, iid);
}
/**
* For creating a new COM-object with the given clsid and iid.
* This should usually be called by subclasses.
*
* @param clsid the GUID of the COM-object to create.
* @param iid the interface id of the requested interface on the COM-object.
*/
protected DispatchPtr(GUID clsid, GUID iid) throws COMException {
super(clsid, iid);
}
/**
* For getting the IDispatch interface on an existing COM-object.
* This is an alternative to calling {@link #queryInterface(Class)}
* on comObject.
*
* @param comObject the COM-object to get the IDispatch interface on.
*/
public DispatchPtr(COMPtr comObject) throws COMException {
super(comObject);
}
/**
* FIXME - what are these used for?
*/
static public final int DISPID_VALUE=0; //The default member for the object. This property or method is invoked when an ActiveX client specifies the object name without a property or method.
static public final int DISPID_NEWENUM=-4; //The _NewEnum property. This special, restricted property is required for collection objects. It returns an enumerator object that supports IEnumVARIANT, and should have the restricted attribute specified in Object Description Language.
static public final int DISPID_EVALUATE=-5; //The Evaluate method. This method is implicitly invoked when the ActiveX client encloses the arguments in square brackets. For example, the following two lines are equivalent:
//x.[A1:C1].value =10
//x.Evaluate("A1:C1").value = 10
//The Evaluate method has the DISPID DISPID_EVALUATE.
static public final int DISPID_PROPERTYPUT=-3; //The parameter that receives the value of an assignment in a PROPERTYPUT.
static public final int DISPID_CONSTRUCTOR=-6; //The C++ constructor function for the object.
static public final int DISPID_DESTRUCTOR=-7; //The C++ destructor function for the object.
static public final int DISPID_UNKNOWN=-1; //The value returned by IDispatch::GetIDsOfNames to indicate that a member or parameter name was not found.
static public final int DISPID_COLLECT=-8; //The Collect property. You use this property if the method you are calling through Invoke is an accessor function.
static public final int DISPID_Name=-800;
static public final int DISPID_Delete=-801;
static public final int DISPID_Object=-802;
static public final int DISPID_Parent=-803;
public int getTypeInfoCount(int[] pctinfo) throws COMException {
return GenericStub.getTypeInfoCount(getPeer(), getUnknown(), pctinfo);
}
/**
* FIXME - native implementation not finished?
*/
public int getTypeInfo(int iTInfo, GUID lcid, ITypeInfo[] ppTInfo) throws COMException {
return GenericStub.getTypeInfo(IID_TOKEN, getPeer(), getUnknown(), iTInfo, lcid, ppTInfo);
}
/**
* FIXME - native implementation not finished?
*/
public int getIDsOfNames(GUID riid, String[] rgszNames, int cNames, GUID lcid, int[] rgDispId) {
return GenericStub.getIDsOfNames(getPeer(), getUnknown(), riid, rgszNames, cNames, lcid, rgDispId);
}
/**
* For getting a property identified just by the property name.
* <br><br>
* See {@link Variant#readObject(LittleEndianInputStream)} for further
* information about the typeconversion that takes place when
* converting the native property value to a java type.
*
* @param prop the name of the property.
* @return the value of the property.
*
* @throws COMException if no such property, or if the native
* code failed in any other way.
* @throws NullPointerException if prop is null.
*/
public Object get(String prop) throws COMException {
return getN(prop, null, 0);
}
/**
* For getting a property identified by the property name and
* a single index. Does not necessarely have to be a an integer -
* it depends on how the native get-method interprets the
* index.
* <br><br>
* See {@link Variant#readObject(LittleEndianInputStream)} for further
* information about the typeconversion that takes place when
* converting the native property value to a java type.
*
* @param prop the name of the property.
* @param index the index to pass to the the native property
* get method.
* @return the value of the property.
*
* @throws COMException if no such property, or if the native
* code failed in any other way.
* @throws NullPointerException if prop is null.
*/
public Object get(String prop, Object index) throws COMException {
return getN(prop, new Object[] { index }, 1);
}
/**
* For getting a property identified by the property name and
* multiple index'es. Does not necessarely have to be integers -
* it depends on how the native get-method interprets the
* indexes.
* <br><br>
* One scenario where this can be used, is eg. for retriving the
* value of a single cell in a spreadsheet, where two "indexes"
* (row and column) are needed for identifying the cell.
* <br><br>
* See {@link Variant#readObject(LittleEndianInputStream)} for further
* information about the typeconversion that takes place when
* converting the native property value to a java type.
*
* @param prop the name of the property.
* @param indexes the parameters to pass to the the native property
* get method.
* @return the value of the property.
*
* @throws COMException if no such property, or if the native
* code failed in any other way.
* @throws NullPointerException if prop is null.
*/
public Object getN(String prop, Object[] indexes) throws COMException {
return getN(prop, indexes, (indexes != null ? indexes.length : 0));
}
/**
* @see #getN(String, Object[])
*/
public Object getN(String prop, Object[] indexes, int indexesExpected) throws COMException {
if (prop == null) {
throw new NullPointerException("Property <null> not allowed.");
}
NakedByteStream nbs = new NakedByteStream();
LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
int len = (indexes != null) ? indexes.length : 0;
// Marshal missing args
try {
for (int n = 0, extra = (indexesExpected - len); n < extra; n++) {
leos.writeShort(VarTypes.VT_ERROR);
}
} catch (IOException e) {
throw new COMException(e);
}
for (int n = (len - 1); n >= 0; n--) {
Variant.writeObject(indexes[n], leos);
}
byte[] result = GenericStub.dispatchInvoke(INVOKE_INST[indexesExpected] + "V:", indexesExpected,
nbs.size(), nbs.getInternalBuffer(), prop, DispatchConstants.DISPATCH_PROPERTYGET,
getPeer(), getUnknown());
LittleEndianInputStream leis = new LittleEndianInputStream(new ByteArrayInputStream(result));
return Variant.readObject(leis);
}
/**
* For setting a new value for a property identified by name only.
* <br><br>
* The {@link Variant#writeObject(Object, LittleEndianOutputStream)} should be
* consulted for further information about what type of object should
* be passed as value. One of the shortcut methods for simple types could also
* be used.
*
* @param prop the name of the property.
* @param val the new value of the property.
*
* @throws COMException if no such property, or if the native
* code failed in any other way.
* @throws NullPointerException if prop is null.
*/
public void put(String prop, Object val) throws COMException {
putN(prop, null, 0, val);
}
/**
* Shortcut method for setting a boolean <code>VT_BOOL</code> property.
*
* @see #put(String, Object)
*/
public void put(String prop, boolean val) throws COMException {
put(prop, (val ? Boolean.TRUE : Boolean.FALSE));
}
/**
* Shortcut method for setting a byte <code>VT_UI1</code> property.
*
* @see #put(String, Object)
*/
public void put(String prop, byte val) throws COMException {
put(prop, new Byte(val));
}
/**
* Shortcut method for setting a short <code>VT_I2</code> property.
*
* @see #put(String, Object)
*/
public void put(String prop, short val) throws COMException {
put(prop, new Short(val));
}
/**
* Shortcut method for setting an integer <code>VT_I4</code> property.
*
* @see #put(String, Object)
*/
public void put(String prop, int val) throws COMException {
put(prop, new Integer(val));
}
/**
* Shortcut method for setting a long <code>VT_I8</code> property.
*
* @see #put(String, Object)
*/
public void put(String prop, long val) throws COMException {
put(prop, new Long(val));
}
/**
* Shortcut method for setting a float <code>VT_R4</code> property.
*
* @see #put(String, Object)
*/
public void put(String prop, float val) throws COMException {
put(prop, new Float(val));
}
/**
* Shortcut method for setting a double <code>VT_R8</code> property.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -