CLIENT.CXX
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:3k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /*+----------------------------------------------------------------------------
  2. Microsoft Windows Sample Program
  3. Copyright (C) 1994 - 1997 Microsoft Corporation.  All rights reserved.
  4. FILE:       client.cxx
  5. USAGE:      client 
  6. PURPOSE:    OLE client application uses a custom interface to call the 
  7.             server and print "Hello".
  8. FUNCTIONS:  main() - Main entry point for the client application.
  9. COMMENTS:   The server program must be installed before running the client.
  10.             Run server.exe /REGSERVER to install the server program.
  11. -----------------------------------------------------------------------------*/
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include "ohello.h"     // generated by MIDL from ohello.idl
  15. // the class ID of the server exe
  16. const CLSID CLSID_OHello = 
  17.     {0xf9246031,0x9f33,0x11cd,{0xb2,0x3f,0x00,0xaa,0x00,0x33,0x9c,0xce}};
  18. void PrintErrorMessage(HRESULT hr);
  19. //+---------------------------------------------------------------------------
  20. //
  21. //  Function:   main
  22. //
  23. //  Synopsis:   Main entry point for the client application.  First we 
  24. //              call CoCreateInstance to create an instance of the server
  25. //              object.  Next, we use the IHello custom interface to 
  26. //              call the server object and print "Hello".  After waiting
  27. //              5 seconds, we call the server object to print "Goodbye".
  28. //              Finally, we release the IHello interface pointer and
  29. //              terminate.
  30. //
  31. //----------------------------------------------------------------------------
  32. void __cdecl main(int argc, char *argv[])
  33. {
  34.     HRESULT hr;
  35.     IHello *pHello = 0;
  36.     IClassFactory *pClassFactory;
  37.     hr = CoInitialize(NULL);
  38.     if(SUCCEEDED(hr))
  39.     {
  40.         hr = CoCreateInstance(CLSID_OHello, 0, CLSCTX_LOCAL_SERVER, IID_IHello, (void **) &pHello);
  41.         if(SUCCEEDED(hr))
  42.         {
  43.             hr = pHello->HelloProc((unsigned char *) "Hello");
  44.             if(SUCCEEDED(hr))
  45.             {
  46.                 printf("Successfully printed Hello.n");
  47.             }
  48.             else
  49.             {
  50.                 printf("IHello::HelloProc failed.n");
  51.                 PrintErrorMessage(hr);
  52.             }
  53.             printf(" <pausing for 5 sec>n");
  54.             Sleep( 5000 ); // 5000 msec = 5 sec
  55.             hr = pHello->HelloProc((unsigned char *) "Goodbye");
  56.             if(SUCCEEDED(hr))
  57.             {
  58.                 printf("Successfully printed Goodbye.n");
  59.             }
  60.             else
  61.             {
  62.                 printf("IHello::HelloProc failed.n");
  63.                 PrintErrorMessage(hr);
  64.             }
  65.             pHello->Release();
  66.         }
  67.         else
  68.         {
  69.             printf("CoCreateInstance failed.n");
  70.             PrintErrorMessage(hr);
  71.             if(hr == REGDB_E_CLASSNOTREG)
  72.                 printf("Run server.exe /REGSERVER to install the server program.n");
  73.         }
  74.         CoUninitialize();
  75.     }
  76.     else
  77.     {
  78.         printf("CoInitialize failed.n");
  79.         PrintErrorMessage(hr);
  80.     }   
  81. }