JavaClient.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:2k
源码类别:

Windows编程

开发平台:

Visual C++

  1. #define _WIN32_DCOM
  2. #include <iostream.h>
  3. #include <stdio.h>
  4. #include <windows.h>
  5. #include <ocidl.h>
  6. //DFFE86C9-ED1D-11D2-BB52-006097B5EAFC
  7. const CLSID CLSID_PersistentJava = {0xDFFE86C9, 0xED1D, 0x11D2, {0xBB, 0x52, 0x00, 
  8.     0x60, 0x97, 0xB5, 0xEA, 0xFC}};
  9. void main()
  10. {
  11.     HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  12.     if(FAILED(hr))
  13.         cout << "CoInitializeEx failed. " << endl;
  14.     IUnknown* pUnknown;
  15.    hr = CoCreateInstance(CLSID_PersistentJava, NULL, CLSCTX_INPROC_SERVER, 
  16.       IID_IUnknown, (void**)&pUnknown);
  17.     if(FAILED(hr))
  18.       cout << "CoCreateInstance failed. " << endl;
  19.     IPersistStreamInit* pPersistStreamInit;
  20.     hr = pUnknown->QueryInterface(IID_IPersistStreamInit, (void**)&pPersistStreamInit);
  21.     if(FAILED(hr))
  22.         cout << "IID_IPersistStreamInit not supported. " << endl;
  23. IStream* pStream = 0;
  24. hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
  25.     if(FAILED(hr))
  26.         cout << "CreateStreamOnHGlobal failed. " << endl;
  27. hr = pPersistStreamInit->InitNew();
  28.    if(FAILED(hr))
  29.      printf("IPersistStreamInit::InitNew failed %0xn", hr);
  30. hr = pPersistStreamInit->Save(pStream, TRUE);
  31.    if(FAILED(hr))
  32.      printf("IPersistStreamInit::Save failed %0xn", hr);
  33.    LARGE_INTEGER zero = { 0, 0 };
  34.    pStream->Seek(zero, STREAM_SEEK_SET, NULL);
  35.     IUnknown* pUnknown2;
  36.    hr = CoCreateInstance(CLSID_PersistentJava, NULL, CLSCTX_INPROC_SERVER, 
  37.       IID_IUnknown, (void**)&pUnknown2);
  38.     if(FAILED(hr))
  39.       cout << "CoCreateInstance failed. " << endl;
  40.  
  41.     IPersistStreamInit* pPersistStreamInit2;
  42.     hr = pUnknown2->QueryInterface(IID_IPersistStreamInit, (void**)&pPersistStreamInit2);
  43.     if(FAILED(hr))
  44.         cout << "IID_IPersistStreamInit not supported. " << endl;
  45. hr = pPersistStreamInit2->Load(pStream);
  46.     if(FAILED(hr))
  47.        printf("IPersistStreamInit::Load failed %0xn", hr);
  48.     pPersistStreamInit2->Release();
  49.     pUnknown2->Release();
  50.     pPersistStreamInit->Release();
  51. pStream->Release();
  52.     pUnknown->Release();
  53.     CoUninitialize();
  54. }