home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap18 / cosmo1.0 / oleinit.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  6KB  |  207 lines

  1. /*
  2.  * OLEINIT.C
  3.  *
  4.  * Handles all application and instance-specific initialization that
  5.  * is also specific to OLE.  That which is specific only to the
  6.  * application or instance resides in INIT.C
  7.  *
  8.  * Copyright(c) Microsoft Corp. 1992-1994 All Rights Reserved
  9.  * Win32 version, January 1994
  10.  */
  11.  
  12. #ifdef MAKEOLESERVER
  13.  
  14. #include <windows.h>
  15. #include <ole.h>
  16. #include "cosmo.h"
  17. #include "oleglobl.h"
  18.  
  19.  
  20.  
  21.  
  22. /*
  23.  * FOLEInstanceInit
  24.  *
  25.  * Purpose:
  26.  *  Handles OLE-server specific initialziation:
  27.  *      1.  Register clipboard formats for "Native", "OwnerLink",
  28.  *          and "ObjectLink."
  29.  *      2.  Initialize VTBLs for server, document, and object.
  30.  *      3.  Register the server with OLESVR.
  31.  *      4.  Parse the command line to determine initial window state.
  32.  *      5.  Register a document depending on contents of command line.
  33.  *
  34.  * Parameters:
  35.  *  pOLE            LPXOLEGLOBALS to OLE-specific global variable block.
  36.  *  hInstance       HINSTANCE of the application instance.
  37.  *  pszClass        LPSTR to classname of the server.
  38.  *  ppszCmds        LPSTR FAR to command line argument strings.
  39.  *  nCmdShow        UINT initial ShowWindow command passed to WinMain.
  40.  *
  41.  * Return Value:
  42.  *  BOOL            FALSE if an error occurred, otherwise TRUE and
  43.  *                  pOLE->wCmdShow contains a valid ShowWindow command
  44.  *                  for the initial state of the window.
  45.  */
  46.  
  47. BOOL WINAPI FOLEInstanceInit(LPXOLEGLOBALS pOLE, HINSTANCE hInstance
  48.     , LPSTR pszClass, LPSTR FAR *ppszCmds, UINT nCmdShow)
  49.     {
  50.     OLESTATUS       os;
  51.     LPSTR           pszT;
  52.     LPCOSMOSERVER   pSvr;
  53.     LPCOSMODOC      pDoc;
  54.     BOOL            fTemp;
  55.  
  56.  
  57.     //1. Register clipboard formats.
  58.     pOLE->cfNative    =RegisterClipboardFormat(rgpsz[IDS_NATIVE]);
  59.     pOLE->cfOwnerLink =RegisterClipboardFormat(rgpsz[IDS_OWNERLINK]);
  60.     pOLE->cfObjectLink=RegisterClipboardFormat(rgpsz[IDS_OBJECTLINK]);
  61.  
  62.     if (0==pOLE->cfNative || 0==pOLE->cfOwnerLink || 0==pOLE->cfObjectLink)
  63.         return FALSE;
  64.  
  65.  
  66.     /*
  67.      * 2. Initialize the method tables with functions in OLEVTBL.C
  68.      *    and mark the vtbl's as initialized.
  69.      */
  70.     fTemp=TRUE;
  71.     fTemp&=FOLEVtblInitServer  (hInstance, &pOLE->vtblSvr);
  72.     fTemp&=FOLEVtblInitDocument(hInstance, &pOLE->vtblDoc);
  73.     fTemp&=FOLEVtblInitObject  (hInstance, &pOLE->vtblObj);
  74.  
  75.     if (!fTemp)
  76.         return FALSE;   //Cleanup will follow through FApplicationExit.
  77.  
  78.  
  79.     /*
  80.      * 3. Allocate and initialize the OLESERVER structure, in this
  81.      *    application we call it COSMOSERVER.  Uses the server
  82.      *    constructor in OLESVR.C.
  83.      */
  84.  
  85.     pSvr=PServerAllocate(&pOLE->vtblSvr);
  86.  
  87.     if ((LPCOSMOSERVER)NULL==pSvr)
  88.         return FALSE;
  89.  
  90.     pOLE->pSvr=pSvr;
  91.     pSvr->nCmdShow=nCmdShow;        //By default, use what the app was given.
  92.  
  93.  
  94.     //4. Register the server application with OLESVR.DLL
  95.     os=OleRegisterServer(pszClass, (LPOLESERVER)pSvr, &pSvr->lh,
  96.                          hInstance, OLE_SERVER_MULTI);
  97.  
  98.     if (OLE_OK!=os)
  99.         {
  100.         //ServerRelease will NOT be called so we must free the memory.
  101.         LocalFree(pSvr->hMem);
  102.         return FALSE;
  103.         }
  104.  
  105.  
  106.  
  107.     /*
  108.      * 5. Given the pointer to the command-line strings, ppszCmds,
  109.      *    check if we have "[/ | -]Embedding" and a possible filename.
  110.      *
  111.      * NOTE:  We trust whoever called us to find a filename in
  112.      *        the command line, ignoring anything that starts with
  113.      *        a - or / as an argument.
  114.      */
  115.  
  116.     pszT=*ppszCmds++;
  117.  
  118.     //Assume stand-alone configuration.
  119.     pGlob->fOLE=FALSE;
  120.     pSvr->fEmbed=FALSE;
  121.     pSvr->fLink=FALSE;
  122.  
  123.     if (NULL!=pszT)
  124.         {
  125.         /*
  126.          * If the first parameter has - or / leading, skip that character
  127.          * so we can check for "Embedding"
  128.          */
  129.         if('-'==*pszT || '/'==*pszT)
  130.             pszT++;
  131.  
  132.         //See if we have Embedding at all, in which case we're doing OLE.
  133.         pGlob->fOLE=!lstrcmp(pszT, rgpsz[IDS_EMBEDDING]);
  134.  
  135.         //Change the ShowWindow command appropriately if we're doing OLE.
  136.         if (pGlob->fOLE)
  137.             pSvr->nCmdShow=SW_HIDE;
  138.  
  139.  
  140.         /*
  141.          * Set fLink if there is an additional filename on the
  142.          * command-line, and point pszT there if so.
  143.          */
  144.         if (NULL!=*ppszCmds)
  145.             {
  146.             pSvr->fLink=TRUE;
  147.             pszT=*ppszCmds;
  148.             }
  149.         else
  150.             {
  151.             pSvr->fEmbed=TRUE;
  152.             pszT=NULL;
  153.             }
  154.  
  155.         //pszT is either NULL or points to a filename.
  156.         }
  157.  
  158.     /*
  159.      * 6.  Allocate an OLESERVERDOC (COSMODOC) and initialize.  LPTR
  160.      *     to LocalAlloc initializes everything to NULL.
  161.      */
  162.     pDoc=PDocumentAllocate(&pOLE->vtblDoc);
  163.  
  164.     if ((LPCOSMODOC)NULL==pDoc)
  165.         {
  166.         //DocRelease will not be called, but ServerRelease will.
  167.         LocalFree(pDoc->hMem);
  168.         OleRevokeServer(pSvr->lh);
  169.         return FALSE;
  170.         }
  171.  
  172.     pSvr->pDoc=pDoc;
  173.  
  174.  
  175.     /*
  176.      * 7.  Register documents as necessary.  Remember to register any
  177.      *     stand-alone or linked document regardless of what you are
  178.      *     doing with OLE.  The only case where you do NOT register is
  179.      *     for starting the application as embedded.
  180.      *
  181.      *     In step 5 we set pszT equal to either NULL (embedding or no
  182.      *     file) or to a filename (linking or stand-alone with a file).
  183.      *     So we only have to point pszT to "(Untitled)" if it's NULL
  184.      *     then call OleRegisterServerDoc.
  185.      */
  186.  
  187.     //Register a document in any case except embedding.
  188.     if (!pSvr->fEmbed)
  189.         {
  190.         pszT=(NULL==pszT) ? rgpsz[IDS_UNTITLED] : pszT;
  191.         os=OleRegisterServerDoc(pSvr->lh, pszT, (LPOLESERVERDOC)pDoc, &pDoc->lh);
  192.  
  193.         //Must revoke on any error.
  194.         if (OLE_OK!=os)
  195.             {
  196.             OleRevokeServer(pSvr->lh);
  197.             return FALSE;
  198.             }
  199.         }
  200.  
  201.     //All done!
  202.     return TRUE;
  203.     }
  204.  
  205.  
  206. #endif //MAKEOLESERVER
  207.