home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / D12 / OLE.ZIP / OLEAPP.PAS next >
Encoding:
Pascal/Delphi Source File  |  1992-10-01  |  6.8 KB  |  238 lines

  1. {***************************************************}
  2. {                                                   }
  3. {   Windows 3.1 OLE Server Demonstration Program    }
  4. {   Application Unit                                }
  5. {   Copyright (c) 1992 by Borland International     }
  6. {                                                   }
  7. {***************************************************}
  8.  
  9. unit OLEApp;
  10.  
  11. { This unit contains the definition of the OLE Server
  12.   Application Object.
  13.  
  14.   Note that this application object is defined in its own
  15.   unit because other objects in the program need to reference
  16.   their owning application.
  17.  
  18.   Note: To compile the OLE Server demo, set Compile|Primary File to OLESERVR.PAS
  19. }
  20.  
  21. interface
  22.  
  23. uses Ole, OWindows, Server;
  24.  
  25. type
  26.  
  27. { Application Object }
  28.  
  29.   POLEApp  = ^TOLEApp;
  30.   TOLEApp  = object(TApplication)
  31.     Server       : POleServerObj;
  32.     cfNative     : TOleClipFormat;
  33.     cfOwnerLink  : TOleClipFormat;
  34.     cfObjectLink : TOleClipFormat;
  35.  
  36.     procedure InitInstance; virtual;
  37.     procedure CreateServer; virtual;
  38.     procedure Wait(var WaitFlag: Boolean); virtual;
  39.     function  RegisterClipboardFormats: Boolean; virtual;
  40.     Procedure Error(ErrorCode: Integer); virtual;
  41.   end;
  42.  
  43. implementation
  44.  
  45. uses WinTypes, WinProcs, OleTypes, Strings,
  46.      ServrWin, OleObj, Objects;
  47.  
  48.  
  49. { TOleApp Methods }
  50.  
  51. { Processes the command line and check for option /Embedding or -Embedding,
  52.   then create the OLE server. There are four scenarios we are concerned with:
  53.  
  54.     1. Case One: oleservr.exe
  55.       - Embedding = False; create an untitled document
  56.  
  57.     2. Case two: oleservr.exe filename
  58.       - Embedding = False; create a new document from the file
  59.  
  60.     3. Case three: oleservr.exe -Embedding
  61.       - Embedding = True; do NOT create or register a document.
  62.                           do NOT show a window until client requests it
  63.  
  64.     4. Case four: oleservr.exe -Embedding filename
  65.       - Embedding = True; load file, register it (this is the linking case)
  66.                           do NOT show a window until client requests it
  67. }
  68. procedure TOleApp.CreateServer;
  69. var
  70.   Strng    : PChar;
  71.   Embedded : Boolean;
  72.   Path     : PChar;
  73.   ServerObj: POleServerObj;
  74. begin
  75.   Strng    := CmdLine;
  76.   Embedded := False;
  77.   Path     := nil;
  78.  
  79.   { Skip any whitespace
  80.   }
  81.   if Strng <> nil then
  82.   begin
  83.     while (Strng^ = ' ') and (Strng^ <> #0) do
  84.       inc(Strng);
  85.  
  86.   { Check for a '-' or '/'.  If found, check for the "Embedding"
  87.     option.  Then, skip past the option to the file name.
  88.   }
  89.     if (Strng^ = '-') or (Strng^ = '/') then
  90.     begin
  91.       Embedded := (StrIComp(@Strng[1], Embedding) <> 0);
  92.       while (Strng^ <> ' ') and (Strng^ <> #0) do
  93.         inc(Strng);
  94.     end;
  95.  
  96.   { Skip any whitespace before looking for the file name
  97.   }
  98.     while (Strng^ = ' ') and (Strng^ <> #0) do
  99.       inc(Strng);
  100.  
  101.     if Strng^ <> #0 then
  102.       Path := Strng;
  103.   end
  104.   else
  105.   begin
  106.     Embedded := False;
  107.     Path     := nil;
  108.   end;
  109.  
  110.   { If we are embedded, then we won't display the window until requested
  111.     to by the library.
  112.   }
  113.   if Embedded then
  114.     CmdShow := sw_Hide;
  115.  
  116.   { Create the server object.  Recall that the object will attach itself
  117.     to this application, much as a child window attaches to a parent, so
  118.     we don't need to hold the results of these New's.
  119.   }
  120.   if Path <> nil then
  121.     New(ServerObj, InitFromFile(@Self, Path))
  122.   else
  123.     New(ServerObj, Init(@Self, Embedded));
  124. end;
  125.  
  126. { Registers the clipboard formats.  If you are a mini-server (embedding 
  127.   only) you will need to register clipboard formats for "Native" and 
  128.   "OwnerLink".  If you are a full server (linking and embedding) you will
  129.   also need to register clipboard format "ObjectLink"
  130. }
  131. function TOleApp.RegisterClipboardFormats: Boolean;
  132. begin
  133.   cfNative    := RegisterClipboardFormat('Native');
  134.   cfOwnerLink := RegisterClipboardFormat('OwnerLink');
  135.   cfObjectLink:= RegisterClipboardFormat('ObjectLink');
  136.  
  137.   RegisterClipboardFormats :=    (cfNative     <> 0)
  138.                              and (cfOwnerLink  <> 0)
  139.                              and (cfObjectLink <> 0);
  140. end;
  141.  
  142. { Initializes this instance of the OLE application, by doing the following:
  143.     - Create the main window
  144.     - Create OLE VTbl thunks
  145.     - Create clipboard formats
  146.     - Parse the command line
  147.     - Create/register OLE server
  148.  
  149.   NOTE: We let Windows free all thunks when the application terminates,
  150.         and don't do it ourselves
  151. }
  152. procedure TOleApp.InitInstance;
  153. begin
  154.   MainWindow := New(PServerWindow, Init(nil, DemoTitle));
  155.   MainWindow := MakeWindow(MainWindow);
  156.  
  157.   RegisterType(ROleObjectObj);
  158.  
  159.   if (not TOleServerObj_InitVTbl(HInstance) or
  160.       not TOleDocument_InitVTbl(HInstance) or
  161.       not TOleObjectObj_InitVTbl(HInstance)
  162.      )
  163.   then
  164.     Status := olInitVTblError
  165.   else 
  166.     if not RegisterClipboardFormats then
  167.       Status := olRegClipError
  168.     else
  169.       CreateServer;
  170.  
  171.   { We do this *after* calling CreateServer, because if we are embedded
  172.     then we don't want to display the main window until requested to by
  173.     the server library, and it is CreateServer who determines that and sets
  174.     'CmdShow' accordingly
  175.   }
  176.   if MainWindow <> nil then
  177.     MainWindow^.Show(CmdShow)
  178.   else
  179.     Status := em_InvalidMainWindow;
  180. end;
  181.  
  182. { Redefines the Error method to trap error messages generated by OLE app,
  183.   display an error message box and terminate the application.
  184. }
  185. procedure TOleApp.Error(ErrorCode: Integer);
  186. var
  187.   Strng : PChar;
  188. begin
  189.   Strng := nil;
  190.   if (ErrorCode = olRegClipError) then
  191.     Strng := 'Fatal Error: Cannot register ''Native'', ''OwnerLink'', and ' +
  192.       '''ObjectLink'' clipboard formats'
  193.   else 
  194.     if (ErrorCode = olInitVTBLError) then
  195.       Strng := 'Fatal Error: Cannot create thunks for ''OleServer'', ' +
  196.         '''OleServerDoc'', and ''OleObject'' VTbls';
  197.  
  198.   if Strng <> nil then
  199.   begin
  200.     MessageBox(0, Strng, DemoTitle, mb_OK or mb_IconStop);
  201.     PostAppMessage(GetCurrentTask, wm_Quit, 0, 0);
  202.   end
  203.   else
  204.     TApplication.Error(ErrorCode);
  205. end;
  206.  
  207. { Dispatches messages until the given flag is set to True.  One use of this
  208.   function is to wait until a Release method is called after a function has
  209.   returned Ole_Wait_for_Release.
  210.  
  211.   PARAMETER: "WaitFlag" is a reference to a flag that will be set to True
  212.              when we can return.
  213. }
  214. procedure TOleApp.Wait(var WaitFlag: Boolean);
  215. var
  216.   Msg         :  TMsg;
  217.   MoreMessages:  Bool;
  218. begin
  219.   MoreMessages := False;
  220.   while not WaitFlag do
  221.   begin
  222.     OleUnblockServer(Server^.ServerHdl, MoreMessages);
  223.  
  224.     if not MoreMessages then 
  225.     begin
  226.       { If there are no more messages in the OLE queue, go to system queue
  227.       }
  228.       if (GetMessage(Msg, 0, 0, 0)) then
  229.       begin
  230.         TranslateMessage(Msg);
  231.         DispatchMessage (Msg);
  232.       end;
  233.     end;
  234.   end;
  235. end;
  236.  
  237. end.
  238.