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 / doserver / utils.cpp < prev    next >
C/C++ Source or Header  |  1997-05-28  |  5KB  |  164 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 1997 Microsoft Corporation.  All Rights Reserved.
  8. **************************************************************************/
  9.  
  10. /**************************************************************************
  11.  
  12.    File:          Utils.cpp
  13.    
  14.    Description:   Contains utility routines for the DOServer sample.
  15.  
  16. **************************************************************************/
  17.  
  18. /**************************************************************************
  19.    #include statements
  20. **************************************************************************/
  21.  
  22. #include <windows.h>
  23. #include "utils.h"
  24.  
  25. /**************************************************************************
  26.  
  27.    ParseCommandLine()
  28.    
  29.    Parses the Windows command line which was passed to WinMain.
  30.    This function determines if the -Embedding switch has been given.
  31.  
  32. **************************************************************************/
  33.  
  34. STDAPI_(void) ParseCommandLine(LPSTR pszCommandLine, LPBOOL pfEmbedFlag, LPTSTR pszFileName)
  35. {
  36. int      i;
  37. TCHAR    szCmdLine[MAX_PATH];
  38. LPTSTR   pszCmdLine = szCmdLine;
  39. TCHAR    szBuf[MAX_PATH];
  40.  
  41. if(!pszCommandLine)
  42.    return;
  43.    
  44. if(!pfEmbedFlag)
  45.    return;
  46.    
  47. *pfEmbedFlag = FALSE;
  48.  
  49. #ifdef UNICODE
  50. MultiByteToWideChar( CP_ACP, 
  51.                      MB_PRECOMPOSED, 
  52.                      pszCommandLine, 
  53.                      -1, 
  54.                      pszCmdLine,
  55.                      ARRAYSIZE(szCmdLine));
  56. #else
  57. lstrcpy(pszCmdLine, pszCommandLine);
  58. #endif
  59.  
  60. // skip blanks
  61. while(isspace(*pszCmdLine)) 
  62.    pszCmdLine++;
  63.  
  64. // No filename or options, so start a fresh document.
  65. if(!*pszCmdLine)   
  66.    return;
  67.  
  68. // Check for "-Embedding" or "/Embedding" and set fEmbedding.
  69. if((*pszCmdLine == '-') || (*pszCmdLine == '/'))
  70.    {
  71.    pszCmdLine++;
  72.    pszCmdLine = GetWord(pszCmdLine, szBuf);
  73.    *pfEmbedFlag = !lstrcmp(szBuf, EMBEDDINGFLAG);
  74.    }
  75.  
  76. if(!pszFileName)
  77.    return;
  78.    
  79. *pszFileName = 0;
  80.  
  81. // skip blanks
  82. while(isspace(*pszCmdLine)) 
  83.    pszCmdLine++;
  84.  
  85. // set pszFileName to argument
  86. for(i = 0; *(pszCmdLine + i); i++) 
  87.    {
  88.    *(pszFileName + i) = *(pszCmdLine + i);
  89.    }
  90.  
  91. *(pszFileName + i) = 0;
  92. }
  93.  
  94.  
  95. /**************************************************************************
  96.  
  97.    GetWord()
  98.    
  99.    LPSTR lpszSrc - Pointer to a source string
  100.    LPSTR lpszDst - Pointer to destination buffer
  101.  
  102.    Will copy one space-terminated or null-terminated word from the source
  103.    string to the destination buffer.
  104.    
  105.    returns: pointer to next character following the word.
  106.  
  107. **************************************************************************/
  108.  
  109. static LPTSTR GetWord(LPTSTR lpszSrc, LPTSTR lpszDst)
  110. {
  111. while (*lpszSrc && !isspace(*lpszSrc))
  112.    *lpszDst++ = *lpszSrc++;
  113.  
  114. *lpszDst = '\0';
  115.  
  116. return lpszSrc;
  117. }
  118.  
  119. /* GetOleObjectDataHere
  120. ** ----------------------
  121. **    Render CF_EMBEDSOURCE/CF_EMBEDDEDOBJECT data on an TYMED_ISTORAGE
  122. **    medium by asking the object to save into the storage.
  123. **    the object must support IPersistStorage.
  124. **
  125. **    if lpMedium->tymed == TYMED_NULL, then a delete-on-release
  126. **    storage is allocated (either file-based or memory-base depending
  127. **    the value of fUseMemory). this is useful to support an
  128. **    IDataObject::GetData call where the callee must allocate the
  129. **    medium.
  130. **
  131. **    if lpMedium->tymed == TYMED_ISTORAGE, then the data is writen
  132. **    into the passed in IStorage. this is useful to support an
  133. **    IDataObject::GetDataHere call where the caller has allocated his
  134. **    own IStorage.
  135. */
  136. STDAPI GetOleObjectDataHere(  LPPERSISTSTORAGE lpPStg,
  137.                               LPFORMATETC lpformatetc,
  138.                               LPSTGMEDIUM lpMedium)
  139. {
  140. LPSTORAGE   lpstg = NULL;
  141. DWORD       reserved = 0;
  142. HRESULT     hr = DATA_E_FORMATETC;
  143.  
  144. lpMedium->pUnkForRelease = NULL;
  145.  
  146. if(lpMedium->tymed != TYMED_ISTORAGE) 
  147.    {
  148.    return hr;
  149.    }
  150.  
  151. //AddRef the IStorage pointer
  152. lpPStg->AddRef();
  153.  
  154. // NOTE: even if OleSave returns an error you should still call
  155. // SaveCompleted.
  156.  
  157. hr = OleSave(lpPStg, lpMedium->pstg, FALSE /* fSameAsLoad */);
  158.  
  159. lpPStg->SaveCompleted(NULL);
  160.  
  161. return hr;
  162. }
  163.  
  164.