public abstract class COM4J extends Object
Provides various global services that don't fit into the rest of classes.
Modifier and Type | Field and Description |
---|---|
protected static List<Runnable> |
applicationShutdownTasks
List of application defined task, they are executed _before_ com4j shuts down.
|
protected static List<Runnable> |
com4JShutdownTasks
List of shutdown task defined by Com4J itself.
|
static GUID |
IID_IDispatch
GUID of IDispatch.
|
static GUID |
IID_IFont
IID of IFont
|
static GUID |
IID_IFontDisp
IID of IFontDisp
|
static GUID |
IID_IPicture
IID of IPicture
|
static GUID |
IID_IPictureDisp
IID of IPictureDisp
|
static GUID |
IID_IUnknown
GUID of IUnknown.
|
Modifier and Type | Method and Description |
---|---|
static void |
addListener(ComObjectListener listener)
Registers a
ComObjectListener to the current thread. |
static void |
addShutdownTask(Runnable task)
Add a shutdown task.
|
static void |
cleanUp()
Cleans up COM resources for the current thread.
|
static ByteBuffer |
createBuffer(long ptr,
int size)
Maps the memory region into
ByteBuffer so that it can be
then accessed nicely from Java code. |
static <T extends Com4jObject> |
createInstance(Class<T> primaryInterface,
GUID clsid)
Creates a new COM object of the given CLSID and returns
it in a wrapped interface.
|
static <T extends Com4jObject> |
createInstance(Class<T> primaryInterface,
String clsid)
Creates a new COM object of the given CLSID and returns
it in a wrapped interface.
|
static <T extends Com4jObject> |
createInstance(Class<T> primaryInterface,
String clsid,
int clsctx)
Creates a new COM object of the given CLSID and returns
it in a wrapped interface.
|
static <T extends Com4jObject> |
getActiveObject(Class<T> primaryInterface,
GUID clsid)
Gets an already running object from the running object table.
|
static <T extends Com4jObject> |
getActiveObject(Class<T> primaryInterface,
String clsid)
Gets an already object from the running object table.
|
static GUID |
getIID(Class<?> _interface)
Gets the interface GUID associated with the given interface.
|
static <T extends Com4jObject> |
getObject(Class<T> primaryInterface,
String fileName,
String progId)
Returns a reference to a COM object primarily by loading a file.
|
static ROT |
getROT()
Returns the singleton ROT instance.
|
static Com4jObject |
loadTypeLibrary(File typeLibraryFile)
Loads a type library from a given file and returns its IUnknown.
|
static void |
removeListener(ComObjectListener listener)
Removes a registered
ComObjectListener from the current thread. |
static boolean |
removeShutdownTask(Runnable task)
Remove an already registered shutdown task
|
static <T extends Com4jObject> |
wrap(Class<T> primaryInterface,
long ptr)
Wraps an externally obtained interface pointer into a COM wrapper object.
|
public static final GUID IID_IUnknown
public static final GUID IID_IDispatch
public static final GUID IID_IPicture
public static final GUID IID_IPictureDisp
public static final GUID IID_IFont
public static final GUID IID_IFontDisp
protected static final List<Runnable> applicationShutdownTasks
public static <T extends Com4jObject> T createInstance(Class<T> primaryInterface, GUID clsid) throws ComException
T
- the type of the return value and the type parameter of the class object of primaryInterfaceprimaryInterface
- The created COM object is returned as this interface.
Must be non-null. Passing in Com4jObject
allows
the caller to create a new instance without knowing
its primary interface.clsid
- The CLSID of the COM object to be created. Must be non-null.ComException
- if the instantiation fails.public static <T extends Com4jObject> T createInstance(Class<T> primaryInterface, String clsid) throws ComException
T
- the type of the return value and the type parameter of the class object of primaryInterfaceprimaryInterface
- The created COM object is returned as this interface.
Must be non-null. Passing in Com4jObject
allows
the caller to create a new instance without knowing
its primary interface.clsid
- The CLSID of the COM object in the
"{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" format,
or the ProgID of the object (like "Microsoft.XMLParser.1.0")ComException
- if the instantiation fails.public static <T extends Com4jObject> T createInstance(Class<T> primaryInterface, String clsid, int clsctx) throws ComException
Compared to createInstance(Class,String)
,
this method allows the caller to specify CLSCTX_XXX
constants to control the server instantiation.
T
- the type of the return value and the type parameter of the class object of primaryInterfaceprimaryInterface
- type parameter of the primaryInterface typeclsid
- a string representation of the class idclsctx
- Normally this is CLSCTX.ALL
, but can be any combination of CLSCTX
constants.ComException
- if an error occurred in the native COM partCLSCTX
public static <T extends Com4jObject> T wrap(Class<T> primaryInterface, long ptr) throws ComException
This method doesn't call addRef on the given interface pointer. Instead, this method takes over the ownership of the given pointer.
primaryInterface
- The interface type to wrap the pointer into.ptr
- The rar interface pointer value.ComException
public static <T extends Com4jObject> T getActiveObject(Class<T> primaryInterface, GUID clsid)
T
- the type of the return value and the type parameter of the class object of primaryInterfaceprimaryInterface
- The returned COM object is returned as this interface.
Must be non-null. Passing in Com4jObject
allows
the caller to create a new instance without knowing
its primary interface.clsid
- The CLSID of the object to be retrieved.ComException
- if the retrieval fails.public static <T extends Com4jObject> T getActiveObject(Class<T> primaryInterface, String clsid)
T
- the type of the return value and the type parameter of the class object of primaryInterfaceprimaryInterface
- The returned COM object is returned as this interface.
Must be non-null. Passing in Com4jObject
allows
the caller to create a new instance without knowing
its primary interface.clsid
- The CLSID of the COM object to be retrieved, in the
"{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" format,
or the ProgID of the object (like "Microsoft.XMLParser.1.0")ComException
- if the retrieval fails.getActiveObject(Class,GUID)
public static <T extends Com4jObject> T getObject(Class<T> primaryInterface, String fileName, String progId)
This method implements the semantics of the GetObject
Visual Basic
function. See MSDN reference
for its semantics.
This function really has three different mode of operation:
fileName
and progId
are specified,
a COM object of the given progId is created and its state is loaded
from the given file name. This is normally used to activate a OLE server
by loading a file.
fileName
is specified, it is treated as a moniker.
The moniker will be bound and the resulting COM object will be returned.
In a simple case a moniker is a file path, in which case the associated
application is activated and loads the data. But monikers in OLE are
extensible, so in more general case the semantics really depends on
the moniker provider.
progId
is specified, this method would just work like
getActiveObject(Class, String)
.
T
- the type of the return value and the type parameter of the class object of primaryInterfaceprimaryInterface
- The returned COM object is returned as this interface.
Must be non-null. Passing in Com4jObject
allows
the caller to create a new instance without knowing
its primary interface.fileName
- path to the fileprogId
- the progID in string representationComException
- if the retrieval fails.public static ROT getROT()
public static GUID getIID(Class<?> _interface)
This method retrieves the associated IID
annotation from the
interface and return it.
_interface
- reference to an object that has the IID
annotation.GUID
object.IllegalArgumentException
- if the interface doesn't have any IID
annotation.public static Com4jObject loadTypeLibrary(File typeLibraryFile)
Loads a type library from a given file and returns its IUnknown.
Exposed for tlbimp.typeLibraryFile
- the path to the file containing the type librarypublic static ByteBuffer createBuffer(long ptr, int size)
ByteBuffer
so that it can be
then accessed nicely from Java code.
When bridging native code to Java, it's often necessary to be able to read/write arbitrary portion of the memory, and this method lets you do that.
Neither this code nor ByteBuffer
does anything about
making sure that the memory region pointed by ptr
remains
valid. It's the caller's responsibility.
ptr
- The pointer value that points to the top of the buffer.size
- The size of the memory region to be mapped to ByteBuffer
.ByteBuffer
.public static void addListener(ComObjectListener listener)
ComObjectListener
to the current thread.
The registered listener will receive a notification each time a new proxy is created.
listener
- the listener to be addedIllegalArgumentException
- If the listener is null or it is already registered.removeListener(ComObjectListener)
public static void removeListener(ComObjectListener listener)
ComObjectListener
from the current thread.listener
- the listener to remove.IllegalArgumentException
- if the listener is not currently registered.addListener(ComObjectListener)
public static void cleanUp()
This method can be invoked explicitly by a thread that used COM objects, to clean up resources, such as references to out-of-process COM objects.
In COM terminology, this effectively amounts to callingCoUninitialize
.
After this method is invoked, a thread can still go use other COM resources.public static void addShutdownTask(Runnable task)
Com4J adds a shutdown hook to the java runtime. As soon as the java runtime shuts down, Com4J shuts down, too. To enable the developer to release external resources, accessed via Com4J, such as an automation server object that is running in the background, this method provides a mechanism to add shutdown tasks, that are executed before Com4J really shuts down.
Do not add your own shutdown hooks to the java runtime to do such things, because it is likely that the com4j shutdown hook is running first, and then you can't access the resources any more.
Tasks are executed sequential in LIFO order.
task
- The task to be executed before Com4J shuts down.public static boolean removeShutdownTask(Runnable task)
task
- The task that is to remove from the list.true
if the task was registered.Copyright © 2014. All rights reserved.