home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / COM / COMClassObject / CustomClassObjectDemo.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  4.4 KB  |  102 lines

  1. //
  2. //  CustomClassObjectDemo.java
  3. //
  4. //  Copyright (c) 1999, Microsoft Corporation.  All rights reserved.
  5. //
  6. //  This source file demonstrates an implementation of a custom COM class
  7. //  object.
  8. //
  9.  
  10. import com.ms.com.*;
  11. import com.ms.win32.win;
  12.  
  13. /**
  14.  * @com.register(clsid=76689510-4A14-11d3-8261-0080C7A6A9A1,
  15.  *               typelib=76689511-4A14-11d3-8261-0080C7A6A9A1,
  16.  *               progid="sample.CustomClassObjectDemo")
  17.  */
  18. public class CustomClassObjectDemo
  19. {
  20.     //  When the virtual machine is creating the external COM class factory,
  21.     //  it searches for a static inner class with the name COMClassObject.
  22.     //
  23.     //  This class must be a direct or indirect subclass of StdCOMClassObject--
  24.     //  for example, it's possible to make a stock implementation of a license
  25.     //  aware class factory and make all of the top level COM objects have a
  26.     //  simple COMClassObject that defers to the intermediate class.
  27.     //
  28.     //  The custom class object does not need to implement IClassFactory or
  29.     //  related interfaces.  If a class object does not implement IClassFactory
  30.     //  itself, then the stock virtual machine implementation of IClassFactory
  31.     //  will be used.
  32.     static class COMClassObject extends StdCOMClassObject implements
  33.         IClassFactory2
  34.     {
  35.         private static final boolean isControl = false;
  36.         private static final String licenseKey = "licenseKey";
  37.         private static final int CLASS_E_NOTLICENSED = 0x80040112;
  38.  
  39.         //  Note that if multiple threads attempt to retrieve the COM class
  40.         //  object for this COM class then multiple instances of this inner
  41.         //  class could be created, but only one instance will actually be used
  42.         //  and kept in the virtual machine's cache of class objects.  Thus care
  43.         //  should be taken with instance variables, especially if the goal of
  44.         //  the custom class object is to have singleton semantics.
  45.         public COMClassObject() {
  46.         }
  47.  
  48.         //  Implemention of IClassFactory::CreateInstance.
  49.         public IUnknown CreateInstance(IUnknown punkOuter, _Guid iid) {
  50.             return CreateInstanceLic(punkOuter, null, iid, null);
  51.         }
  52.  
  53.         //  Implemention of IClassFactory::LockServer.
  54.         public void LockServer(boolean b) {
  55.             //  Delegate to the superclass implemention.
  56.             super.lockServerImpl(b);
  57.         }
  58.  
  59.         //  Implemention of IClassFactory2::GetLicInfo.
  60.         public LICINFO GetLicInfo() {
  61.             return new LICINFO(true, true);
  62.         }
  63.  
  64.         //  Implemention of IClassFactory2::RequestLicKey.
  65.         public String RequestLicKey(int dwReserved) {
  66.             return licenseKey;
  67.         }
  68.  
  69.         //  Implemention of IClassFactory2::CreateInstanceLic.
  70.         public IUnknown CreateInstanceLic(IUnknown pUnkOuter, IUnknown
  71.             pUnkReserved, _Guid riid, String bstrKey) {
  72.             if (pUnkOuter != null && !ComLib.IID_IUnknown.equals(riid)) {
  73.                 //  Enforce the COM requirement that the returned interface
  74.                 //  must be IID_IUnknown if aggregation is requested.
  75.                 throw new ComFailException(win.CLASS_E_NOAGGREGATION);
  76.             } else if (licenseKey.equals(bstrKey)) {
  77.                 //  The license key matches so create an instance of the object
  78.                 //  using the superclass's helper.
  79.                 //
  80.                 //  createInstanceImpl allows aggregate COM objects to be
  81.                 //  created as well as marking the COM Callable Wrapper (CCW)
  82.                 //  with it's original CLSID, so you should try to use this
  83.                 //  method instead of directly creating an instance of the
  84.                 //  class.
  85.                 //
  86.                 //  createControlImpl performs the same work as
  87.                 //  createInstanceImpl, but also wraps the CCW with ActiveX
  88.                 //  control code.  This is equivalent to what the virtual
  89.                 //  machine would do if the Control key existed under the
  90.                 //  CLSID's registry branch.
  91.                 if (isControl) {
  92.                     return super.createControlImpl(pUnkOuter);
  93.                 } else {
  94.                     return super.createInstanceImpl(pUnkOuter);
  95.                 }
  96.             } else {
  97.                 throw new ComFailException(CLASS_E_NOTLICENSED);
  98.             }
  99.         }
  100.     }
  101. }
  102.