home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / COM / COMClassObject / client.cpp next >
Encoding:
C/C++ Source or Header  |  2000-05-04  |  2.3 KB  |  82 lines

  1. //
  2. //  client.cpp
  3. //
  4. //  Copyright (c) 1999, Microsoft Corporation.  All rights reserved.
  5. //
  6. //  This source files demonstrates using a custom class object of a COM object
  7. //  implemented in Java.
  8. //
  9.  
  10. #include <stdio.h>
  11. #include <windows.h>
  12. #include <olectl.h>
  13.  
  14. void main(int argc, char *argv[])
  15. {
  16.     HRESULT hr;
  17.     CLSID clsid;
  18.     IUnknown *punk;
  19.     IClassFactory2 *pFactory2;
  20.     BSTR bstrKey;
  21.  
  22.     hr = CoInitialize(NULL);
  23.  
  24.     if (hr == S_OK) {
  25.  
  26.         hr = CLSIDFromString(L"sample.CustomClassObjectDemo", &clsid);
  27.  
  28.         if (hr == S_OK) {
  29.  
  30.             //  Attempt to create an instance of the object.  This should fail
  31.             //  because we're not "licensed" to create the Java object.
  32.             hr = CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_IUnknown,
  33.                 (LPVOID *) &punk);
  34.  
  35.             if (hr == S_OK) {
  36.                 printf("successfully called CoCreateInstance\n");
  37.                 punk->Release();
  38.             } else if (hr == CLASS_E_NOTLICENSED) {
  39.                 printf("failed to CoCreateInstance object as expected (hr=CLASS_E_NOTLICENSED)\n");
  40.             } else {
  41.                 printf("failed to CoCreateInstance object (hr=%08x)\n", hr);
  42.             }
  43.  
  44.             hr = CoGetClassObject(clsid, CLSCTX_ALL, NULL, IID_IClassFactory2,
  45.                 (LPVOID *) &pFactory2);
  46.  
  47.             if (hr == S_OK) {
  48.  
  49.                 hr = pFactory2->RequestLicKey(0, &bstrKey);
  50.  
  51.                 if (hr == S_OK) {
  52.  
  53.                     hr = pFactory2->CreateInstanceLic(NULL, NULL, IID_IUnknown,
  54.                         bstrKey, (LPVOID *) &punk);
  55.  
  56.                     if (hr == S_OK) {
  57.                         printf("successfully called CreateInstanceLic\n");
  58.                         punk->Release();
  59.                     } else {
  60.                         printf("failed call to CreateInstanceLic (hr=%08x)\n", hr);
  61.                     }
  62.  
  63.                     SysFreeString(bstrKey);
  64.  
  65.                 } else {
  66.                     printf("failed to get license key (hr=%08x)\n", hr);
  67.                 }
  68.  
  69.                 pFactory2->Release();
  70.  
  71.             } else {
  72.                 printf("failed to get class object (hr=%08x)\n", hr);
  73.             }
  74.  
  75.         } else {
  76.             printf("failed to convert progid to clsid (hr=%08x)\n", hr);
  77.         }
  78.  
  79.         CoUninitialize();
  80.     }
  81. }
  82.