home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-05-04 | 4.4 KB | 102 lines |
- //
- // CustomClassObjectDemo.java
- //
- // Copyright (c) 1999, Microsoft Corporation. All rights reserved.
- //
- // This source file demonstrates an implementation of a custom COM class
- // object.
- //
-
- import com.ms.com.*;
- import com.ms.win32.win;
-
- /**
- * @com.register(clsid=76689510-4A14-11d3-8261-0080C7A6A9A1,
- * typelib=76689511-4A14-11d3-8261-0080C7A6A9A1,
- * progid="sample.CustomClassObjectDemo")
- */
- public class CustomClassObjectDemo
- {
- // When the virtual machine is creating the external COM class factory,
- // it searches for a static inner class with the name COMClassObject.
- //
- // This class must be a direct or indirect subclass of StdCOMClassObject--
- // for example, it's possible to make a stock implementation of a license
- // aware class factory and make all of the top level COM objects have a
- // simple COMClassObject that defers to the intermediate class.
- //
- // The custom class object does not need to implement IClassFactory or
- // related interfaces. If a class object does not implement IClassFactory
- // itself, then the stock virtual machine implementation of IClassFactory
- // will be used.
- static class COMClassObject extends StdCOMClassObject implements
- IClassFactory2
- {
- private static final boolean isControl = false;
- private static final String licenseKey = "licenseKey";
- private static final int CLASS_E_NOTLICENSED = 0x80040112;
-
- // Note that if multiple threads attempt to retrieve the COM class
- // object for this COM class then multiple instances of this inner
- // class could be created, but only one instance will actually be used
- // and kept in the virtual machine's cache of class objects. Thus care
- // should be taken with instance variables, especially if the goal of
- // the custom class object is to have singleton semantics.
- public COMClassObject() {
- }
-
- // Implemention of IClassFactory::CreateInstance.
- public IUnknown CreateInstance(IUnknown punkOuter, _Guid iid) {
- return CreateInstanceLic(punkOuter, null, iid, null);
- }
-
- // Implemention of IClassFactory::LockServer.
- public void LockServer(boolean b) {
- // Delegate to the superclass implemention.
- super.lockServerImpl(b);
- }
-
- // Implemention of IClassFactory2::GetLicInfo.
- public LICINFO GetLicInfo() {
- return new LICINFO(true, true);
- }
-
- // Implemention of IClassFactory2::RequestLicKey.
- public String RequestLicKey(int dwReserved) {
- return licenseKey;
- }
-
- // Implemention of IClassFactory2::CreateInstanceLic.
- public IUnknown CreateInstanceLic(IUnknown pUnkOuter, IUnknown
- pUnkReserved, _Guid riid, String bstrKey) {
- if (pUnkOuter != null && !ComLib.IID_IUnknown.equals(riid)) {
- // Enforce the COM requirement that the returned interface
- // must be IID_IUnknown if aggregation is requested.
- throw new ComFailException(win.CLASS_E_NOAGGREGATION);
- } else if (licenseKey.equals(bstrKey)) {
- // The license key matches so create an instance of the object
- // using the superclass's helper.
- //
- // createInstanceImpl allows aggregate COM objects to be
- // created as well as marking the COM Callable Wrapper (CCW)
- // with it's original CLSID, so you should try to use this
- // method instead of directly creating an instance of the
- // class.
- //
- // createControlImpl performs the same work as
- // createInstanceImpl, but also wraps the CCW with ActiveX
- // control code. This is equivalent to what the virtual
- // machine would do if the Control key existed under the
- // CLSID's registry branch.
- if (isControl) {
- return super.createControlImpl(pUnkOuter);
- } else {
- return super.createInstanceImpl(pUnkOuter);
- }
- } else {
- throw new ComFailException(CLASS_E_NOTLICENSED);
- }
- }
- }
- }
-