home *** CD-ROM | disk | FTP | other *** search
- // ===========================================================================
- // File: Client.cpp
- //
- // This simple COM Client will create an instance of a Java COM object that
- // implements the java.io.Serializable interface, and will therefore by
- // default have the COM interfaces IPersist, IPersistStreamInit and
- // IPersistStorage. This application will save the state of the COM object
- // using these COM interfaces.
- //
- // The IPersistStreamInit interface will be used to save the state of the COM
- // object to an IStream in memory. The contents of the memory will then be
- // printed to standard out as characters.
- //
- // The IPersistStorage interface will save the contects of the COM object
- // into a newly created structured storage file called "jcomsave.doc" that
- // can be viewed using the dfview.exe tool that comes with Microsoft Visual
- // C++. Examining the contents of the doc file, you will find one entry which
- // has exactly the same bytes as those printed to the screen.
- //
- // (C) Copyright 1995 - 1999 Microsoft Corporation. All rights reserved.
- // ===========================================================================
-
- // %%Includes: ---------------------------------------------------------------
- #include <windows.h>
- #include <initguid.h>
- #include <stdio.h>
- #include <ocidl.h>
- #include <objbase.h>
- // %%Guids: ------------------------------------------------------------------
- DEFINE_GUID(CLSID_JavaObject, 0xFE59F510, 0x2D5D, 0x11d2, 0xB9, 0x0F, 0x00, 0xC0, 0x4F, 0x8C, 0x94, 0x91);
-
- // ---------------------------------------------------------------------------
- // %%Function: main
- //
- // This creates the COM object, and saves it to an IStream and IStorage.
- // ---------------------------------------------------------------------------
- int main( int argc, char **argv )
- {
- HRESULT hr =0; //Holds the result of each function call.
-
- IPersistStreamInit* pPersist = NULL;
- IStream* ppstm = NULL;
-
- // initialize COM
- hr = CoInitialize(NULL);
-
- if (SUCCEEDED(hr))
- {
- // create an instance of our Java implemented COM object.
- IUnknown* punk = NULL;
-
- hr = CoCreateInstance(CLSID_JavaObject,
- NULL,
- CLSCTX_INPROC_SERVER,
- IID_IUnknown,
- (void**)&punk);
- if (SUCCEEDED(hr))
- {
- // Get the IPersistStreamInit interface from our COM object.
- hr = punk->QueryInterface(IID_IPersistStreamInit,
- (void**)&pPersist);
-
- punk->Release();
- }
- }
- if (SUCCEEDED(hr))
- {
-
- // Create an in memory Stream that will be used to store the COM object.
-
- hr = CreateStreamOnHGlobal(NULL,
- false,
- &ppstm);
- if (SUCCEEDED(hr))
- {
-
- // Saves the contents of the COM object to the IStream in memory.
-
- hr = pPersist->Save(ppstm,
- TRUE);
- }
- }
- if (SUCCEEDED(hr))
- {
- // Get the number of bytes that were written to the IStream.
- //
- // NOTE: do not use IPersistStreamInit::GetSizeMax(ULARGE_INTEGER) as this will
- // always return E_NOTIMPL.
-
- STATSTG stat;
- hr = ppstm->Stat(&stat,1);
- if (SUCCEEDED(hr))
- {
- ULARGE_INTEGER streamSize = stat.cbSize;
-
- // To lock the IStream data when we are reading its contents we will need an HGlobal.
-
- HGLOBAL hGlobal;
- hr= GetHGlobalFromStream(ppstm,&hGlobal);
- if (SUCCEEDED(hr))
- {
-
- // Lock the IStream data , and get a pointer to the first byte.
-
- byte* pState = (byte*)GlobalLock(hGlobal);
-
- if (pState==NULL)
- {
- hr = E_FAIL;
- }
- if (SUCCEEDED(hr))
- {
- //Write the bytes stored in the IStream to standard out..
-
- for (DWORD i=0;i<streamSize.LowPart;i++)
- {
- printf("%c ",*pState);
- pState++;
- }
- // Unlock the IStream
-
- GlobalUnlock(hGlobal);
-
- ppstm->Release();
- ppstm=NULL;
- }
- }
- }
- }
- //
- // This section illustrates the use of the IPersistStorage interface.
- //
- if (SUCCEEDED(hr))
- {
- // Get the IPersistStreamInit interface from our COM object.
-
- IPersistStorage* pPersistStorage = NULL;
-
- hr = pPersist->QueryInterface(IID_IPersistStorage,
- (void**)&pPersistStorage);
- pPersist->Release();
- pPersist=NULL;
- if (SUCCEEDED(hr))
- {
- // Create a new file that will be our IStorage object.
-
- IStorage* pIStorage = NULL;
- WCHAR* foo =L"jcomsave.doc";
-
- hr = StgCreateDocfile(foo,
- STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE,
- 0,
- &pIStorage);
- if (SUCCEEDED(hr))
- {
- //Store the COM object in the IStorage file.
-
- hr = pPersistStorage->Save(pIStorage,FALSE);
- pIStorage->Release();
- }
-
- pPersistStorage->Release();
- }
- }
-
- // Free the memory associated with the IStream
- if (pPersist!=NULL) pPersist->Release();
- if (ppstm!=NULL) ppstm->Release();
- return hr;
- } // main