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 / olesvr.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  14KB  |  446 lines

  1. /*
  2.  * OLESVR.C
  3.  *
  4.  * Contains all callback functions in the OLESERVERVTBL struture:
  5.  *      ServerCreate
  6.  *      ServerCreateFromTemplate
  7.  *      ServerEdit
  8.  *      ServerExecute
  9.  *      ServerExit
  10.  *      ServerOpen
  11.  *      ServerRelease
  12.  *
  13.  * Also includes the constructor function PServerAllocate.
  14.  *
  15.  * Copyright(c) Microsoft Corp. 1992-1994 All Rights Reserved
  16.  * Win32 version, January 1994
  17.  */
  18.  
  19.  
  20. #ifdef MAKEOLESERVER
  21.  
  22. #include <windows.h>
  23. #include <ole.h>
  24. #include "cosmo.h"
  25. #include "oleglobl.h"
  26.  
  27.  
  28.  
  29. /*
  30.  * PServerAllocate
  31.  *
  32.  * Purpose:
  33.  *  Allocates a COSMOSERVER structure and sets the defaults in its fields.
  34.  *  Used from application initialization.
  35.  *
  36.  * Parameters:
  37.  *  pVtblSvr        LPOLESERVERVTBL used to initialize the pvtbl field.
  38.  *
  39.  * Return Value:
  40.  *  LPCOSMOSERVER   Pointer to the allocated structure in local memory.
  41.  *                  The hMem field will contain a handle to the structure
  42.  *                  to pass to LocalFree.
  43.  *
  44.  */
  45.  
  46. LPCOSMOSERVER WINAPI PServerAllocate(LPOLESERVERVTBL pVtblSvr)
  47.     {
  48.     HLOCAL            hMem;
  49.     LPCOSMOSERVER     pSvr;
  50.  
  51.     hMem=LocalAlloc(LPTR, CBCOSMOSERVER);
  52.  
  53.     if (NULL==hMem)
  54.         return FALSE;
  55.  
  56.     pSvr=(LPCOSMOSERVER)(PSTR)hMem;
  57.  
  58.     //All fields are initially NULL from LMEM_ZEROINIT.
  59.     pSvr->hMem=hMem;
  60.     pSvr->pvtbl=pVtblSvr;
  61.     pSvr->fRelease=TRUE;            //Indicate a released state.
  62.  
  63.     return pSvr;
  64.     }
  65.  
  66.  
  67.  
  68. /*
  69.  * ServerCreate
  70.  *
  71.  * Purpose:
  72.  *  Creates a new object of the class in pszClass which will be
  73.  *  embedded in a client application document whose name is in
  74.  *  pszDoc.
  75.  *
  76.  * Parameters:
  77.  *  pSvr            LPCOSMOSERVER to structure identifying the server.
  78.  *  lhDoc           LHSERVERDOC identifying the document.
  79.  *  pszClass        OLE_LPCSTR specifying the class of document to create.
  80.  *  pszDoc          OLE_LPCSTR specifying the document for use in window titles.
  81.  *  ppServerDoc     LPLPOLESERVERDOC in which to store a pointer
  82.  *                  to the OLESERVERDOC structure for this new document.
  83.  *
  84.  * Return Value:
  85.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  86.  */
  87.  
  88. OLESTATUS WINAPI ServerCreate(LPCOSMOSERVER pSvr, LHSERVERDOC lhDoc
  89.     , OLE_LPCSTR pszClass, OLE_LPCSTR pszDoc, LPLPOLESERVERDOC ppServerDoc)
  90.     {
  91.     LPCOSMODOC      pDoc;
  92.  
  93.     /*
  94.      * 1.   Create a document of the specified class.
  95.      * 2.   Allocate and initialize an OLESERVERDOC structure.
  96.      * 3.   Store lhDoc in the OLESERVERDOC structure.
  97.      * 4.   Store a pointer to the new OLESERVERDOC structure in ppServerDoc.
  98.      * 5.   Return OLE_OK if successful, OLE_ERROR_NEW otherwise.
  99.      */
  100.  
  101.     /*
  102.      * If this were an MDI application, then we would want to create
  103.      * a new window and a new document, including a new OLESERVERDOC
  104.      * structure.  The protocol supports this, however, this application
  105.      * is single-document.
  106.      */
  107.  
  108.     pDoc=PDocumentAllocate(&pOLE->vtblDoc);
  109.  
  110.     if ((LPCOSMODOC)NULL==pDoc)
  111.         return OLE_ERROR_NEW;
  112.  
  113.     pSvr->pDoc=pDoc;
  114.  
  115.     pDoc->lh=lhDoc;
  116.     *ppServerDoc=(LPOLESERVERDOC)pDoc;
  117.     return OLE_OK;
  118.     }
  119.  
  120.  
  121.  
  122.  
  123. /*
  124.  * ServerCreateFromTemplate
  125.  *
  126.  * Purpose:
  127.  *  Creates a new document initialized with the data in a specified
  128.  *  file.  Thw new document is opened for editing and is embedded
  129.  *  within the client when the server is updated or closed.
  130.  *
  131.  * Parameters:
  132.  *  pSvr            LPCOSMOSERVER to structure identifying the server.
  133.  *  lhDoc           LHSERVERDOC identifying the document.
  134.  *  pszClass        OLE_LPCSTR specifying the class of document to create.
  135.  *  pszDoc          OLE_LPCSTR to the permanent name of the document to open.
  136.  *  pszTemp         OLE_LPCSTR to the file to use as a template.
  137.  *  ppServerDoc     LPLPOLESERVERDOC in which to store a pointer
  138.  *                  to the OLESERVERDOC structure for this new document.
  139.  *
  140.  * Return Value:
  141.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  142.  */
  143.  
  144. OLESTATUS WINAPI ServerCreateFromTemplate(LPCOSMOSERVER pSvr
  145.     , LHSERVERDOC lhDoc, OLE_LPCSTR pszClass, OLE_LPCSTR pszDoc
  146.     , OLE_LPCSTR pszTemp, LPLPOLESERVERDOC ppServerDoc)
  147.     {
  148.     POLYLINE        pl;
  149.     LPCOSMODOC      pDoc;
  150.  
  151.     /*
  152.      * 1.   Create a document of the specified class.
  153.      * 2.   Read the contents of the specified file and initialize the document.
  154.      * 3.   Allocate and initialize an OLESERVERDOC structure.
  155.      * 4.   Store lhDoc in the OLESERVERDOC structure.
  156.      * 5.   Store a pointer to the new OLESERVERDOC structure in ppServerDoc.
  157.      * 6.   Return OLE_OK if successful, OLE_ERROR_TEMPLATE otherwise.
  158.      */
  159.  
  160.     //pszTemp contains a filename.
  161.     if (!FCosFileRead(pGlob, (LPSTR)pszTemp, &pl))
  162.         return OLE_ERROR_TEMPLATE;
  163.  
  164.     pDoc=PDocumentAllocate(&pOLE->vtblDoc);
  165.  
  166.     if ((LPCOSMODOC)NULL==pDoc)
  167.         return OLE_ERROR_TEMPLATE;
  168.  
  169.     pSvr->pDoc=pDoc;
  170.  
  171.     pDoc->lh=lhDoc;
  172.     *ppServerDoc=(LPOLESERVERDOC)pDoc;
  173.  
  174.     SendMessage(pGlob->hWndPolyline, PLM_POLYLINESET, TRUE, (LONG)(LPSTR)&pl);
  175.  
  176.     //The titles in this window will be set later through SetHostNames.
  177.     pGlob->fOpenFile=TRUE;
  178.     return OLE_OK;
  179.     }
  180.  
  181.  
  182.  
  183.  
  184. /*
  185.  * ServerEdit
  186.  *
  187.  * Purpose:
  188.  *  Creates a document initialized with data from a subsequent call
  189.  *  to the SetData function.  The object is embedded in the client
  190.  *  application and the server is not visible.
  191.  *
  192.  * Parameters:
  193.  *  pSvr            LPCOSMOSERVER to structure identifying the server.
  194.  *  lhDoc           LHSERVERDOC identifying the document.
  195.  *  pszClass        OLE_LPCSTR specifying the class of document to create.
  196.  *  pszDoc          OLE_LPCSTR specifying the document for use in window titles.
  197.  *  ppServerDoc     LPLPOLESERVERDOC in which to store a pointer
  198.  *                  to the OLESERVERDOC structure for this new document.
  199.  *
  200.  * Return Value:
  201.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  202.  */
  203.  
  204. OLESTATUS WINAPI ServerEdit(LPCOSMOSERVER pSvr, LHSERVERDOC lhDoc
  205.     , OLE_LPCSTR pszClass, OLE_LPCSTR pszDoc, LPLPOLESERVERDOC ppServerDoc)
  206.     {
  207.     LPCOSMODOC      pDoc;
  208.  
  209.     /*
  210.      * This is the same as Open, but tells the server to expect
  211.      * a SetData call and that the server is initially hidden.
  212.      *
  213.      * 1.   Create a document of the specified class.
  214.      * 2.   Allocate and initialize an OLESERVERDOC structure.
  215.      * 3.   Store lhDoc in the OLESERVERDOC structure.
  216.      * 4.   Store pointer to the new OLESERVERDOC structure in ppServerDoc.
  217.      * 5.   Return OLE_OK if successful, OLE_ERROR_EDIT otherwise.
  218.      */
  219.  
  220.     pDoc=PDocumentAllocate(&pOLE->vtblDoc);
  221.  
  222.     if ((LPCOSMODOC)NULL==pDoc)
  223.         return OLE_ERROR_EDIT;
  224.  
  225.     pSvr->pDoc=pDoc;
  226.  
  227.     pDoc->lh=lhDoc;
  228.     *ppServerDoc=(LPOLESERVERDOC)pDoc;
  229.  
  230.     return OLE_OK;
  231.     }
  232.  
  233.  
  234.  
  235.  
  236. /*
  237.  * ServerExecute
  238.  *
  239.  * Purpose:
  240.  *  Handles DDE Execute commands sent from the server library
  241.  *  from the client application.  Not all applications need to support
  242.  *  DDE Execute.
  243.  *
  244.  * Parameters:
  245.  *  pSvr            LPCOSMOSERVER identifying the server closing.
  246.  *  hCommands       HGLOBAL to memory containing the DDE execute
  247.  *                  string.
  248.  *
  249.  * Return Value:
  250.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  251.  *                  This functions returns OLE_ERROR_COMMAND to indicate
  252.  *                  that it is not implemented.
  253.  */
  254.  
  255. OLESTATUS WINAPI ServerExecute(LPCOSMOSERVER pSvr, HGLOBAL hCommands)
  256.     {
  257.     /*
  258.      * 1.   Lock the hCommands handle to access the execute strings.
  259.      * 2.   Parse and execute the commands as necessary.
  260.      * 3.   DO NOT FREE hCommands.
  261.      * 4.   Return OLE_OK if successful, OLE_ERROR_COMMAND otherwise.
  262.      */
  263.  
  264.     return OLE_ERROR_COMMAND;
  265.     }
  266.  
  267.  
  268.  
  269.  
  270.  
  271. /*
  272.  * ServerExit
  273.  *
  274.  * Purpose:
  275.  *  Instructs the server application to close documents and shut down.
  276.  *
  277.  * Parameters:
  278.  *  pSvr            LPCOSMOSERVER identifying the server closing.
  279.  *
  280.  * Return Value:
  281.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  282.  */
  283.  
  284. OLESTATUS WINAPI ServerExit(LPCOSMOSERVER pSvr)
  285.     {
  286.  
  287.     /*
  288.      * 1.   Hide the window to prevent any user interaction.
  289.      * 2.   Call OleRevokeServer.  Ignore an OLE_WAIT_FOR_RELEASE return value.
  290.      * 3.   Perform whatever action is necessary to cause the application
  291.      *      to terminate, such as DestroyWindow.
  292.      * 4.   Return OLE_OK if successful, OLE_ERROR_GENERIC otherwise.
  293.      */
  294.  
  295.     ShowWindow(pGlob->hWnd, SW_HIDE);
  296.  
  297.     pSvr->fRelease=FALSE;
  298.     OleRevokeServer(pSvr->lh);
  299.  
  300.     DestroyWindow(pGlob->hWnd);
  301.     return OLE_OK;
  302.     }
  303.  
  304.  
  305.  
  306.  
  307.  
  308. /*
  309.  * ServerOpen
  310.  *
  311.  * Purpose:
  312.  *  Opens an exiting file (document) and prepares the document for
  313.  *  editing, generally done when a user double-clicks a linked
  314.  *  object in the client.  Note that the server is hidden at this
  315.  *  point.
  316.  *
  317.  * Parameters:
  318.  *  pSvr            LPCOSMOSERVER to structure identifying the server.
  319.  *  lhDoc           LHSERVERDOC identifying the document.
  320.  *  pszDoc          OLE_LPCSTR to the permanent name of the document to
  321.  *                  be opened.
  322.  *  ppServerDoc     LPLPOLESERVERDOC in which to store a pointer
  323.  *                  to the OLESERVERDOC structure for this new document.
  324.  *
  325.  * Return Value:
  326.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  327.  */
  328.  
  329. OLESTATUS WINAPI ServerOpen(LPCOSMOSERVER pSvr, LHSERVERDOC lhDoc
  330.     , OLE_LPCSTR pszDoc, LPLPOLESERVERDOC ppServerDoc)
  331.     {
  332.     POLYLINE        pl;
  333.     LPCOSMODOC      pDoc;
  334.  
  335.     /*
  336.      * 1.   Create a document of the specified class.
  337.      * 2.   Read the contents of the specified file and initialize the document.
  338.      * 3.   Save the filename of the loaded file with this document,
  339.      *      if necessary.
  340.      * 4.   Allocate and initialize an OLESERVERDOC structure.
  341.      * 5.   Store lhDoc in the OLESERVERDOC structure.
  342.      * 6.   Store a pointer to the new OLESERVERDOC structure in ppServerDoc.
  343.      * 7.   Return OLE_OK if successful, OLE_ERROR_OPEN otherwise.
  344.      */
  345.  
  346.     if (!FCosFileRead(pGlob, (LPSTR)pszDoc, &pl))
  347.         return OLE_ERROR_OPEN;
  348.  
  349.     pDoc=PDocumentAllocate(&pOLE->vtblDoc);
  350.  
  351.     if ((LPCOSMODOC)NULL==pDoc)
  352.         return OLE_ERROR_OPEN;
  353.  
  354.     //Save the document in the server for later access in ServerRelease.
  355.     pSvr->pDoc=pDoc;
  356.  
  357.     pDoc->lh=lhDoc;
  358.     *ppServerDoc=(LPOLESERVERDOC)pDoc;
  359.  
  360.     WindowTitleSet(pGlob->hWnd, (LPSTR)pszDoc);
  361.     SendMessage(pGlob->hWndPolyline, PLM_POLYLINESET, TRUE, (LONG)(LPSTR)&pl);
  362.     pGlob->fOpenFile=TRUE;
  363.  
  364.     return OLE_OK;
  365.     }
  366.  
  367.  
  368.  
  369.  
  370.  
  371. /*
  372.  * ServerRelease
  373.  *
  374.  * Purpose:
  375.  *  Notifies a server that all connections to it have been closed and
  376.  *  that the server application can terminate.  This function should
  377.  *  change the state of a global flag that causes any PeekMessage
  378.  *  waiting loop to exit.
  379.  *
  380.  * Parameters:
  381.  *  pSvr            LPCOSMOSERVER identifying the server.
  382.  *
  383.  * Return Value:
  384.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  385.  */
  386.  
  387. OLESTATUS WINAPI ServerRelease(LPCOSMOSERVER pSvr)
  388.     {
  389.     /*
  390.      * 1.   Set a flag to indicate that Release has been called.
  391.      * 2.   If the application is hidden, we must use ServerRelease to
  392.      *      instruct the application to terminate, by posting a WM_CLOSE
  393.      *      or otherwise effective message, then exit the method with OLE_OK.
  394.      * 3.   Free any resources allocated for this server, like documents,
  395.      *      but DO NOT free the OLESERVER structure itself.
  396.      * 4.   Return OLE_OK if successful, OLE_ERROR_GENERIC otherwise.
  397.      */
  398.  
  399.     pSvr->fRelease=TRUE;
  400.  
  401.     /*
  402.      * If we are invisible when we get ServerRelease, that's a flag
  403.      * to us meaning exit.  Posting a WM_CLOSE takes care of all
  404.      * this.  Note that even though WM_CLOSE processing in COSMO.C
  405.      * checks for a dirty file and asks to save if necessary, the
  406.      * file cannot be dirty because there has been no chance for
  407.      * the user to make any changes.
  408.      *
  409.      * ServerRelease may be called twice when the server is opened invisible
  410.      * for an update of a client object.  In this case, we'll get
  411.      * ServerRelease once, where we should post a message to terminate the
  412.      * application.  The second time around we need to handle free resources
  413.      * associated with the server.  We detect this through the validity
  414.      * pOLE->pSvr->lh which we set to 0L in FFileExit before calling
  415.      * OleRevokeServer.  0L in lh signals us that we're in the final
  416.      * revoke and we can free documents.
  417.      *
  418.      */
  419.     if (!IsWindowVisible(pGlob->hWnd) && 0L!=pOLE->pSvr->lh)
  420.         {
  421.         PostMessage(pGlob->hWnd, WM_CLOSE, 0, 0L);
  422.         return OLE_OK;
  423.         }
  424.  
  425.     /*
  426.      * Free the document if we are closing from OleRevokeServer, not through
  427.      * a client releasing the server.
  428.      */
  429.     if (0L==pOLE->pSvr->lh)
  430.         {
  431.         //Free the document we're holding.
  432.         if (NULL!=pSvr->pDoc)
  433.             {
  434.             if (NULL!=pSvr->pDoc->hMem)
  435.                 LocalFree(pSvr->pDoc->hMem);
  436.             }
  437.  
  438.         pSvr->pDoc=NULL;
  439.         }
  440.  
  441.     return OLE_OK;
  442.     }
  443.  
  444.  
  445. #endif //MAKEOLESERVER
  446.