home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / activedocument / range / range.c < prev    next >
C/C++ Source or Header  |  1996-07-17  |  5KB  |  167 lines

  1. // ===========================================================================
  2. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  3. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  4. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  5. // PARTICULAR PURPOSE.
  6. //
  7. // Copyright 1996 Microsoft Corporation.  All Rights Reserved.
  8. // ===========================================================================
  9. #include <windows.h>
  10. #include <urlmon.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "range.h"
  14.  
  15. DWORD g_dwThread;
  16.  
  17. //==============================================================================
  18. void DumpBytes (PBYTE pBuf, DWORD cbBuf)
  19. {
  20.     DWORD i;
  21.     for (i=0; i<cbBuf; i++)
  22.     {
  23.         switch(pBuf[i])
  24.         {
  25.             case 10:  printf("[LF]"); break;
  26.             case 13:  printf("[CR]"); break;
  27.             default:  printf("%c", pBuf[i]); break;
  28.         }
  29.     }
  30. }
  31.  
  32. //==============================================================================
  33. void PrintResultCode (HRESULT hr)
  34. {
  35. #define CASEMSG(hr) case hr: printf(#hr); break;
  36.  
  37.     printf ("Done with result ");
  38.     switch (hr)
  39.     {
  40.         CASEMSG(S_OK)
  41.         CASEMSG(S_FALSE)
  42.         CASEMSG(E_FAIL)
  43.  
  44.         CASEMSG(INET_E_INVALID_URL)
  45.         CASEMSG(INET_E_NO_SESSION)
  46.         CASEMSG(INET_E_CANNOT_CONNECT)
  47.         CASEMSG(INET_E_RESOURCE_NOT_FOUND)
  48.         CASEMSG(INET_E_OBJECT_NOT_FOUND)
  49.         CASEMSG(INET_E_DATA_NOT_AVAILABLE)
  50.         CASEMSG(INET_E_DOWNLOAD_FAILURE)
  51.         CASEMSG(INET_E_AUTHENTICATION_REQUIRED)
  52.         CASEMSG(INET_E_NO_VALID_MEDIA)  
  53.         CASEMSG(INET_E_CONNECTION_TIMEOUT)
  54.         CASEMSG(INET_E_INVALID_REQUEST)
  55.         CASEMSG(INET_E_UNKNOWN_PROTOCOL)
  56.         CASEMSG(INET_E_SECURITY_PROBLEM)
  57.         CASEMSG(INET_E_CANNOT_LOAD_DATA)
  58.         CASEMSG(INET_E_CANNOT_INSTANTIATE_OBJECT)
  59.         
  60.         default:
  61.             printf ("0x%x", hr);
  62.     }
  63.     printf ("\n");
  64. }
  65.  
  66. //==============================================================================
  67. BOOL RequestCallback (PHTTP_REQUESTCB_PARAM pCBParam)
  68. {
  69.     printf ("RequestCallback[%d] ", pCBParam->dwRequestCtx);
  70.  
  71.     switch (pCBParam->CallbackType)
  72.     {
  73.         case REQUESTCB_STARTED:
  74.             printf ("Started\n");
  75.             printf ("  Status: %d\n", pCBParam->dwResponseCode);
  76.             printf ("  Content-Length: %d\n", pCBParam->dwContentLength);
  77.             if (pCBParam->fdwRequestFlags & HTTP_REQUEST_ACCEPT_RANGES)
  78.                 printf ("  Accept-Ranges: bytes\n");
  79.             if (pCBParam->fdwRequestFlags & HTTP_REQUEST_FROM_CACHE)
  80.                 printf ("  From Cache\n");
  81.             return TRUE;
  82.  
  83.         case REQUESTCB_DATA:
  84.  
  85.             // Dump some bytes so we can verify them.
  86.             printf ("offset=%5d got %4d bytes:",
  87.                 pCBParam->dwDataOffset, pCBParam->cbDataLength);
  88.             DumpBytes ((PBYTE) pCBParam->lpDataBuffer, min(pCBParam->cbDataLength, 15));
  89.             printf ("\n");
  90.             return TRUE;
  91.  
  92.         case REQUESTCB_DONE:
  93.             PrintResultCode (pCBParam->hrRequest);
  94.             // Let the console app thread exit the message loop.
  95.             PostThreadMessage (g_dwThread, WM_QUIT, 0, 0);
  96.             return TRUE;
  97.     }
  98. }
  99.  
  100. //==============================================================================
  101. int __cdecl main (int argc, char *argv[])
  102. {
  103.     MSG msg;
  104.     HTTP_REQUEST_PARAM Param;
  105.  
  106.     SYSTEMTIME st;
  107.     PSTR pszUrl;
  108.     HTTP_REQUEST_RANGE ranges[10];
  109.     DWORD cRanges;
  110.  
  111.     // Check usage.
  112.     if (argc<2 || argc%2 || argc>(2*(sizeof(ranges))/sizeof(ranges[0])))
  113.     {
  114.         fprintf (stderr, "Usage:   range <http-url> [<offset1> <size1>]  [<offset2> <size2>]  [<offset3> <size3>]  ...\n");
  115.         fprintf (stderr, "Example: range http://www.microsoft.com 2 5 10 52 20 4\n");
  116.         exit (1);
  117.     }
  118.  
  119.     // Get URL and ranges from command line.
  120.     pszUrl = argv[1];
  121.     cRanges = 0;
  122.     while (argc -= 2)
  123.     {
  124.         if (argc <= 0)
  125.             break;
  126.  
  127.         ranges[cRanges].dwOffset = atoi(argv[2 * cRanges + 2]);
  128.         ranges[cRanges].dwSize   = atoi(argv[2 * cRanges + 3]);
  129.         cRanges++;
  130.     }
  131.  
  132.     // Pass current time as unless-modified-since time.
  133.     GetSystemTime (&st);
  134.     
  135.     // Save the thread ID so the we can post ourselves
  136.     // WM_QUIT from the callback without creating a window.
  137.     g_dwThread = GetCurrentThreadId();
  138.  
  139.     // Read range depends on URL monikers which depend on COM.
  140.     if (FAILED(CoInitialize(NULL)))
  141.         exit (1);
  142.     
  143.     // Initiate the read range request.
  144.     ZeroMemory (&Param, sizeof(Param));
  145.     Param.cbStruct     = sizeof(Param);
  146.     Param.pszUrl       = pszUrl;
  147.     Param.pfnRequestCB = RequestCallback;
  148.     Param.dwRequestCtx = 15;
  149.     Param.pRanges      = &ranges[0];
  150.     Param.cRanges      = cRanges;
  151.     Param.pstUnlessModifiedSince = &st;
  152.       
  153.     if (HttpReadRequest (&Param))
  154.     {
  155.         // Must enter a message loop for the binding to progress.
  156.         while (GetMessage (&msg, NULL, 0, 0))
  157.         {
  158.             TranslateMessage (&msg);
  159.             DispatchMessage (&msg);
  160.         }
  161.     }
  162.  
  163.     CoUninitialize();
  164.     return 1;
  165. }
  166.  
  167.