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

  1. // ===========================================================================
  2. // File: Client.cpp
  3. // 
  4. // This simple COM Client will create an instance of a Java COM object that 
  5. // implements the java.io.Serializable interface, and will therefore by 
  6. // default have the COM interfaces IPersist, IPersistStreamInit and 
  7. // IPersistStorage.  This application will save the state of the COM object 
  8. // using these COM interfaces.
  9. //
  10. // The IPersistStreamInit interface will be used to save the state of the COM 
  11. // object to an IStream in memory.  The contents of the memory will then be 
  12. // printed to standard out as characters.
  13. //
  14. // The IPersistStorage interface will save the contects of the COM object 
  15. // into a newly created structured storage file called "jcomsave.doc" that 
  16. // can be viewed using the dfview.exe tool that comes with Microsoft Visual 
  17. // C++.  Examining the contents of the doc file, you will find one entry which 
  18. // has exactly the same bytes as those printed to the screen.
  19. // 
  20. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  21. // ===========================================================================
  22.  
  23. // %%Includes: ---------------------------------------------------------------
  24. #include <windows.h>
  25. #include <initguid.h>
  26. #include <stdio.h>
  27. #include <ocidl.h>
  28. #include <objbase.h>
  29. // %%Guids: ------------------------------------------------------------------
  30. DEFINE_GUID(CLSID_JavaObject, 0xFE59F510, 0x2D5D, 0x11d2, 0xB9, 0x0F, 0x00, 0xC0, 0x4F, 0x8C, 0x94, 0x91);
  31.  
  32. // ---------------------------------------------------------------------------
  33. // %%Function: main
  34. //  
  35. // This creates the COM object, and saves it to an IStream and IStorage. 
  36. // ---------------------------------------------------------------------------
  37. int main( int argc, char **argv )
  38.  {   
  39.      HRESULT hr =0; //Holds the result of each function call.
  40.  
  41.      IPersistStreamInit* pPersist = NULL;
  42.      IStream* ppstm = NULL;
  43.  
  44.      // initialize COM
  45.     hr = CoInitialize(NULL);
  46.     
  47.     if (SUCCEEDED(hr))
  48.     {
  49.     // create an instance of our Java implemented COM object.
  50.         IUnknown* punk = NULL;
  51.  
  52.         hr = CoCreateInstance(CLSID_JavaObject,
  53.                         NULL,
  54.                         CLSCTX_INPROC_SERVER,
  55.                         IID_IUnknown,
  56.                         (void**)&punk);
  57.         if (SUCCEEDED(hr))
  58.         {
  59.     // Get the IPersistStreamInit interface from our COM object.
  60.            hr = punk->QueryInterface(IID_IPersistStreamInit,
  61.                             (void**)&pPersist);
  62.            
  63.            punk->Release();
  64.         }
  65.     }
  66.     if (SUCCEEDED(hr))
  67.     {
  68.     
  69.     // Create an in memory Stream that will be used to store the COM object.
  70.  
  71.         hr = CreateStreamOnHGlobal(NULL,
  72.                                 false,
  73.                                 &ppstm);
  74.         if (SUCCEEDED(hr))
  75.         {
  76.     
  77.     // Saves the contents of the COM object to the IStream in memory.
  78.  
  79.             hr = pPersist->Save(ppstm,
  80.                     TRUE);
  81.         }
  82.     }
  83.     if (SUCCEEDED(hr))
  84.     {
  85.     // Get the number of bytes that were written to the IStream.
  86.     //
  87.     // NOTE: do not use IPersistStreamInit::GetSizeMax(ULARGE_INTEGER) as this will
  88.     //              always return E_NOTIMPL.
  89.  
  90.         STATSTG stat;
  91.         hr = ppstm->Stat(&stat,1);
  92.         if (SUCCEEDED(hr))
  93.         {
  94.             ULARGE_INTEGER streamSize = stat.cbSize;
  95.  
  96.     // To lock the IStream data when we are reading its contents we will need an HGlobal.
  97.  
  98.             HGLOBAL hGlobal;
  99.             hr= GetHGlobalFromStream(ppstm,&hGlobal);
  100.             if (SUCCEEDED(hr))
  101.             {
  102.     
  103.     // Lock the IStream data , and get a pointer to the first byte.
  104.  
  105.                 byte* pState = (byte*)GlobalLock(hGlobal);
  106.  
  107.                 if (pState==NULL)
  108.                 {
  109.                     hr = E_FAIL;
  110.                 }
  111.                 if (SUCCEEDED(hr))
  112.                 {
  113.     //Write the bytes stored in the IStream to standard out..
  114.  
  115.                     for (DWORD i=0;i<streamSize.LowPart;i++)
  116.                     {
  117.                         printf("%c ",*pState);
  118.                         pState++;
  119.                     }
  120.     // Unlock the IStream
  121.  
  122.                     GlobalUnlock(hGlobal);
  123.  
  124.                     ppstm->Release();
  125.                     ppstm=NULL;
  126.                 }
  127.             }
  128.         }
  129.     }
  130. //
  131. // This section illustrates the use of the IPersistStorage interface.
  132. //
  133.     if (SUCCEEDED(hr))
  134.     {
  135.     // Get the IPersistStreamInit interface from our COM object.
  136.  
  137.         IPersistStorage* pPersistStorage = NULL;
  138.  
  139.         hr = pPersist->QueryInterface(IID_IPersistStorage,
  140.                             (void**)&pPersistStorage);
  141.         pPersist->Release();
  142.         pPersist=NULL;
  143.         if (SUCCEEDED(hr))
  144.         {
  145.     // Create a new file that will be our IStorage object.
  146.  
  147.             IStorage* pIStorage = NULL;
  148.             WCHAR* foo =L"jcomsave.doc";
  149.  
  150.             hr = StgCreateDocfile(foo,
  151.                             STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE,
  152.                             0,
  153.                             &pIStorage);
  154.             if (SUCCEEDED(hr))
  155.             {
  156.     //Store the COM object in the IStorage file.
  157.  
  158.                 hr = pPersistStorage->Save(pIStorage,FALSE);
  159.                 pIStorage->Release();
  160.             }
  161.  
  162.             pPersistStorage->Release();
  163.         }
  164.     }
  165.  
  166.     // Free the memory associated with the IStream
  167.     if (pPersist!=NULL) pPersist->Release();
  168.     if (ppstm!=NULL) ppstm->Release();
  169.  return hr;
  170. }  // main