home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / dcom / simple / sclient.cpp < prev    next >
C/C++ Source or Header  |  1996-07-05  |  6KB  |  178 lines

  1. // ===========================================================================
  2. // File: S C L I E N T . C P P
  3. // 
  4. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  5. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  6. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  7. // PARTICULAR PURPOSE.
  8. //
  9. // Description:
  10. // 
  11. //  This is the client-portion of the SIMPLE Distributed COM sample. This
  12. // application uses the CLSID_SimpleObject class implemented by the SSERVER.CPP
  13. // module. Pass the machine-name to instantiate the object on, or pass no
  14. // arguments to instantiate the object on the same machine. See the comments
  15. // in SSERVER.CPP for further details.
  16. // 
  17. //  The purpose of this sample is to demonstrate what is minimally required
  18. // to use a COM object, whether it runs on the same machine or on a different
  19. // machine.
  20. // 
  21. // Instructions:
  22. // 
  23. //  To use this sample:
  24. //   * build it using the NMAKE command. NMAKE will create SSERVER.EXE and
  25. //     SCLIENT.EXE.
  26. //   * install the SSERVER.EXE on the current machine or on a remote machine
  27. //     according to the installation instructions found in SSERVER.CPP.
  28. //   * run SCLIENT.EXE. use no command-line arguments to instantiate the object
  29. //     on the current machine. use a single command-line argument of the remote
  30. //     machine-name (UNC or DNS) to instantiate the object on a remote machine.
  31. //   * SCLIENT.EXE displays some simple information about the calls it is
  32. //     making on the object.
  33. // 
  34. // Copyright 1996 Microsoft Corporation.  All Rights Reserved.
  35. // ===========================================================================
  36.  
  37. // %%Includes: ---------------------------------------------------------------
  38. #define INC_OLE2
  39. #include <stdio.h>
  40. #include <windows.h>
  41. #include <initguid.h>
  42. #include <tchar.h>
  43. #include <conio.h>
  44.  
  45. // %%GUIDs: ------------------------------------------------------------------
  46. DEFINE_GUID(CLSID_SimpleObject, 0x5e9ddec7, 0x5767, 0x11cf, 0xbe, 0xab, 0x0, 0xaa, 0x0, 0x6c, 0x36, 0x6);
  47.  
  48. // %%Constants: --------------------------------------------------------------
  49. const ULONG cbDefault = 4096;    // default # of bytes to Read/Write
  50.  
  51. // ---------------------------------------------------------------------------
  52. // %%Function: Message
  53. // 
  54. //  Formats and displays a message to the console.
  55. // ---------------------------------------------------------------------------
  56.  void
  57. Message(LPTSTR szPrefix, HRESULT hr)
  58. {
  59.     LPTSTR   szMessage;
  60.  
  61.     if (hr == S_OK)
  62.         {
  63.         wprintf(szPrefix);
  64.         return;
  65.         }
  66.  
  67.     if (HRESULT_FACILITY(hr) == FACILITY_WINDOWS)
  68.         hr = HRESULT_CODE(hr);
  69.  
  70.     FormatMessage(
  71.         FORMAT_MESSAGE_ALLOCATE_BUFFER |
  72.         FORMAT_MESSAGE_FROM_SYSTEM,
  73.         NULL,
  74.         hr,
  75.         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //The user default language
  76.         (LPTSTR)&szMessage,
  77.         0,
  78.         NULL);
  79.  
  80.     wprintf(TEXT("%s: %s(%lx)\n"), szPrefix, szMessage, hr);
  81.     
  82.     LocalFree(szMessage);
  83. }  // Message
  84.  
  85. // ---------------------------------------------------------------------------
  86. // %%Function: OutputTime
  87. // ---------------------------------------------------------------------------
  88.  void
  89. OutputTime(LARGE_INTEGER* pliStart, LARGE_INTEGER* pliFinish)
  90. {
  91.     LARGE_INTEGER liFreq;
  92.  
  93.     QueryPerformanceFrequency(&liFreq);
  94.     wprintf(TEXT("%0.4f seconds\n"),
  95.         (float)(pliFinish->LowPart - pliStart->LowPart)/(float)liFreq.LowPart);
  96. }  // OutputTime
  97.  
  98. // ---------------------------------------------------------------------------
  99. // %%Function: main
  100. // ---------------------------------------------------------------------------
  101.  void __cdecl
  102. main(int argc, CHAR **argv)
  103. {
  104.     HRESULT hr;
  105.     MULTI_QI    mq;
  106.     COSERVERINFO csi, *pcsi=NULL;
  107.     WCHAR wsz [MAX_PATH];
  108.     ULONG cb = cbDefault;
  109.     LARGE_INTEGER liStart, liFinish;
  110.  
  111.     // allow a machine-name as the command-line argument
  112.     if (argc > 1)
  113.         {
  114.         MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, argv[1], -1,
  115.             wsz, MAX_PATH);
  116.         memset(&csi, 0, sizeof(COSERVERINFO));
  117.         csi.pwszName = wsz;
  118.         pcsi = &csi;
  119.         }
  120.  
  121.     // allow a byte-size to be passed on the command-line
  122.     if (argc > 2)
  123.         cb = atol(argv[2]);
  124.         
  125.     // initialize COM for free-threaded use
  126.     hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  127.     if (FAILED(hr))
  128.         {
  129.         Message(TEXT("Client: CoInitializeEx"), hr);
  130.         exit(hr);
  131.         }
  132.  
  133.     // create a remote instance of the object on the argv[1] machine
  134.     Message(TEXT("Client: Creating Instance..."), S_OK);
  135.     mq.pIID = &IID_IStream;
  136.     mq.pItf = NULL;
  137.     mq.hr = S_OK;
  138.     QueryPerformanceCounter(&liStart);
  139.     hr = CoCreateInstanceEx(CLSID_SimpleObject, NULL, CLSCTX_SERVER, pcsi, 1, &mq);
  140.     QueryPerformanceCounter(&liFinish);
  141.     OutputTime(&liStart, &liFinish);
  142.  
  143.     if (FAILED(hr))
  144.         Message(TEXT("Client: CoCreateInstanceEx"), hr);
  145.     else
  146.         {
  147.         LPSTREAM    pstm = (IStream*)mq.pItf;
  148.         LPVOID      pv;
  149.  
  150.         // "read" some data from the object
  151.         Message(TEXT("Client: Reading data..."), S_OK);
  152.         pv = CoTaskMemAlloc(cb);
  153.         QueryPerformanceCounter(&liStart);
  154.         hr = pstm->Read(pv, cb, NULL);
  155.         QueryPerformanceCounter(&liFinish);
  156.         OutputTime(&liStart, &liFinish);
  157.         if (FAILED(hr))
  158.             Message(TEXT("Client: IStream::Read"), hr);
  159.  
  160.         // "write" some data to the object
  161.         Message(TEXT("Client: Writing data..."), S_OK);
  162.         QueryPerformanceCounter(&liStart);
  163.         hr = pstm->Write(pv, cb, NULL);
  164.         QueryPerformanceCounter(&liFinish);
  165.         OutputTime(&liStart, &liFinish);
  166.         if (FAILED(hr))
  167.             Message(TEXT("Client: IStream::Write"), hr);
  168.  
  169.         // let go of the object
  170.         pstm->Release();
  171.         }
  172.  
  173.     CoUninitialize();
  174.     Message(TEXT("Client: Done"), S_OK);
  175. }  // main
  176.  
  177. // EOF =======================================================================
  178.