| Tenouk C & C++ | MFC Home | IIS & ISAPI Programming 5 | ActiveX Doc Servers & Internet 2 | Download | Site Index |


 

 

 

 

 

 

ActiveX Document Servers and the Internet Part 1

 

 

 

 

 

 

 

Program examples compiled using Visual C++ 6.0 compiler on Windows XP Pro machine with Service Pack 2 and some Figure screen snapshots have been taken on Windows 2000 server. The Internet Information Services version is IIS 4.x/5.x/6.x on Windows 2000 Server SP 4 and Windows XP Pro SP 2. The Internet Explorer is 6.x. Topics and sub topics for this tutorial are listed below. A complete information about IIS installation, configuration and testing a Web site is dumped HERE and how to setup FTP server also included HERE. Both Web and FTP servers were done on Windows 2000 Server SP4. Don’t forget to read Tenouk’s small disclaimer.

  1. Introduction

  2. ActiveX Document Theory

  3. ActiveX Document Servers vs. OLE Embedded Servers

  4. Running an ActiveX Document Server from Internet Explorer

  5. ActiveX Document Servers vs. ActiveX Controls

  6. OLE Interfaces for ActiveX Document Servers and Containers

  7. MFC Support for ActiveX Document Servers

  8. COleServerDoc

  9. CDocObjectServerItem

  10. CDocObjectServer

  11. COleDocIPFrameWnd

  12. ActiveX Document Server Example MYEX35A

  13. MYEX35A Phase 1: A Simple Server

  14. MYEX35A From Scratch

 

 

 

Introduction

 

An ActiveX document is a special file that you can download from a Web server. When the browser sees an ActiveX document file, it automatically loads the corresponding ActiveX document server program from your hard disk, and that program takes over the whole browser window to display the contents of the document. The Microsoft Internet Explorer browser is not the only ActiveX document container program. The Microsoft Office Binder program also runs ActiveX document server programs, storing the several ActiveX documents in a single disk file.

In the COM world, an ActiveX document server program is called a server because it implements a COM component. The container program (Internet Explorer or Office Binder) creates and controls that COM component. In the Internet world, the same program looks like a client because it can request information from a remote host (Microsoft Internet Information Server). In this module, you'll learn about ActiveX document servers and ActiveX documents and you'll build two ActiveX document servers that work over the Internet in conjunction with Internet Explorer.

 

ActiveX Document Theory

 

It's helpful to put ActiveX documents within the context of COM and OLE, which you already understand if you've read the other modules in this book. You can, however, get started with ActiveX document servers without fully understanding all the COM concepts.

 

ActiveX Document Servers vs. OLE Embedded Servers

 

As you saw in Module 27, an OLE embedded server program runs in a child window of an OLE container application and occupies a rectangular area in a page of the container's document (see Figure 28-1). Unless an embedded server program is classified as a mini-server, it can run stand-alone also. In embedded mode, the server program's data is held in a storage inside the container application's file. The embedded server program takes over the container program's menu and toolbar when the user activates it by double-clicking on its rectangle.

In contrast to an embedded server, an ActiveX document server takes over a whole frame window in its container application, and the document is always active. An ActiveX server application, running inside a container's frame window, runs pretty much the same way it would in stand-alone mode. You can see this for yourself if you have Microsoft Office 97 (not available in Office 2000/2003 and newer version). Office 97 for example, includes an ActiveX container program called Binder (accessible from the Office shortcut bar), and the Office applications (Microsoft Word, Microsoft Excel, and so on) have ActiveX server capability. Figure 1 shows a Word document and an Excel chart inside the same binder.

 

 

ActiveX document and Internet - Figure 1: A Word document and an Excel chart inside a Microsoft Office Binder window.

 

Figure 1: A Word document and an Excel chart inside a Microsoft Office Binder window.

 

Like an embedded server, the ActiveX document server saves its data in a storage inside the ActiveX container's file. When the Office user saves the Binder program from the File menu, Binder writes a single OBD file to disk; the file contains one storage for the Word document and another for the Excel spreadsheet. You can see this file structure yourself with the DFVIEW utility, as shown in Figure 2.

 

ActiveX document and Internet - Figure 2: A file structure displayed by the DocFile Viewer.

 

Figure 2: A file structure displayed by the DocFile Viewer.

 

Running an ActiveX Document Server from Internet Explorer

 

Running an ActiveX document server from Internet Explorer is more fun than running one from Microsoft Office Binder (Internet Explorer refers to Internet Explorer 3.0 or greater). Rather than load a storage only from an OBD file, the server program can load its storage from the other side of the world. You just type in a URL, such as http://www.DalaiLama.in/SecretsOfTheUniverse.doc, and a Microsoft Word document opens inside your Browser window, taking over the browser's menu and toolbar. That's assuming, of course, that you have installed the Microsoft Word program. If not, a Word document viewer is available, but it must be on your hard disk before you download the file.

An ActiveX document server won't let you save your changes back to the Internet host, but it will let you save them on your own hard disk. In other words, File Save is disabled but File Save As is enabled.

If you have Microsoft Office, try running Word or Excel in Internet Explorer now. The MYEX34A server is quite capable of delivering documents or worksheets to your browser, assuming that they are accessible from its home directory. Note that Internet Explorer recognizes documents and worksheets not by their file extensions but by the CLSID inside the files. You can prove this for yourself by renaming a file prior to accessing it.

 

ActiveX Document Servers vs. ActiveX Controls

 

Both ActiveX document servers and ActiveX controls can run with and without the Internet. Both are compiled programs that can run inside a browser. The following table lists some of the differences between the two.

 

 

ActiveX Document Server

ActiveX Control

Module type

EXE

Most often a DLL

Can run stand-alone

Yes

No

Code automatically downloaded and registered by a WWW browser

No

Yes

Can be embedded in an HTML file

No

Yes

Occupies the entire browser window

Yes

Sometimes

Can be several pages

Yes

Not usually

Can read/write disk files

Yes

Not usually

 

Table 1.

 

OLE Interfaces for ActiveX Document Servers and Containers

 

ActiveX document servers implement the same interfaces as OLE embedded servers, including IOleObject, IOleInPlaceObject, and IOleInPlaceActiveObject. ActiveX document containers implement IOleClientSite, IOleInPlaceFrame, and IOleInPlaceSite. The menu negotiation works the same as it does for Visual Editing.

Some additional interfaces are implemented, however. ActiveX document servers implement IOleDocument, IOleDocumentView, IOleCommandTarget, and IPrint. ActiveX document containers implement IOleDocumentSite. The architecture allows for multiple views of the same document - sort of like the MFC document-view architecture - but most ActiveX document servers implement only one view per document.

The critical function in an OLE embedded server is IOleObject::DoVerb, which is called by the container when the user double-clicks on an embedded object or activates it through the menu. For an ActiveX document server, however, the critical function is IOleDocumentView::UIActivate. (Before calling this function, the container calls IOleDocument::CreateView, but generally the server just returns an interface pointer to the single document-view object.) UIActivate finds the container site and frame window, sets that window as the server's parent, sets the server's window to cover the container's frame window, and then activates the server's window.

It's important to realize that the COM interaction takes place between the container program (Internet Explorer or Binder) and the ActiveX document server (your program), which both are running on the client computer. We know of no cases in which remote procedure calls (RPCs) are made over the Internet. That means that the remote host (the server computer) does not use COM interfaces to communicate with clients, but it can deliver data in the form of storages.

 

MFC Support for ActiveX Document Servers

 

MFC allows you to create your own ActiveX document server programs. In addition, Visual C++ 6.0 allows you to write ActiveX document containers. To get a server program, create a new MFC AppWizard EXE project and then check the Active Document Server check box, as shown in Figure 3. To create a container program, just make sure the Active Document Container check box is marked.

 

ActiveX document and Internet - Figure 3:  Step 3 of the MFC AppWizard.

 

Figure 3:  Step 3 of the MFC AppWizard.

 

Here's a rundown of the classes involved in MFC's ActiveX Document Server Architecture.

 

COleServerDoc

 

As it is for any COM component, your ActiveX document server's document class is derived from COleServerDoc, which implements IPersistStorage, IOleObject, IDataObject, IOleInPlaceObject, and IOleInPlaceActiveObject. The COM interfaces and MFC classes discussed here were named before Microsoft introduced ActiveX technology. An ActiveX document server was formerly known as a document object server or a doc object server, so those are the names you'll see in the source code and in some online documentation.

 

CDocObjectServerItem

 

This class is derived from the COleServerItem class used in embedded servers. Your ActiveX document server program has a class derived from CDocObjectServerItem, but that class isn't used when the program is running in ActiveX document mode.

 

CDocObjectServer

 

This class implements the new ActiveX server interfaces. Your application creates an object of class CDocObjectServer and attaches it to the COleServerDoc object. If you look at COleServerDoc::GetDocObjectServer in your derived document class, you'll see the construction code. Thereafter, the document object and attached CDocObjectServer object work together to provide ActiveX document server functionality. This class implements both IOleDocument and IOleDocumentView, which means that you can have only one view per document in an MFC ActiveX document server. You generally don't derive classes from CDocObjectServer.

 

COleDocIPFrameWnd

 

This class is derived from COleIPFrameWnd. Your application has a frame window class derived from COleDocIPFrameWnd. The framework constructs an object of that class when the application starts in embedded server mode or in ActiveX document server mode. In ActiveX document server mode, the server's window completely covers the container's frame window and has its own menu resource attached, with the identifier IDR_SRVR_INPLACE (for an SDI application).

 

An ActiveX Document Server Example: MyEX35A

 

You could construct the MYEX35A example in two phases. The first phase is a plain ActiveX document server that loads a file from its container. The view base class is CRichEditView, which means the program loads, edits, and stores text plus embedded objects. In the second phase, the application is enhanced to download a separate text file from the Internet one line at a time, demonstrating that ActiveX document servers can make arbitrary WinInet calls.

 

MYEX35A Phase 1: A Simple Server

 

The MYEX35A example in the download page is complete with the text download feature from Phase 2. You can exercise its Phase 1 capabilities by building it, or you can create a new application with AppWizard. If you do use AppWizard, you should refer to Figure 3 to see the AppWizard EXE project dialog and select the appropriate options. All other options are the default options, except those for selecting SDI (Step 1), setting the project's filename extension to 35a using the Advanced button in Step 4, and changing the view's base class (CRichEditView - on the wizard's last page). You don't have to write any C++ code at all.

Be sure to run the program once in stand-alone mode to register it. While the program is running in stand-alone mode, type some text (and insert some OLE embedded objects) and then save the document as test.35a in your Internet server's home directory (\scripts or \wwwroot directory). Try loading test.35a from Internet Explorer and from Office Binder.

 

MYEX35A From Scratch

 

Let build MYEX35A phase 1, an ActiveX document server from scratch. Follow the shown steps.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

----------------------------------------------------------------------------------------------------------------

ActiveX document and Internet - Figure 4: MYEX35A – Visual C++ new project dialog.

 

Figure 4: MYEX35A – Visual C++ new project dialog.

 

ActiveX document and Internet - Figure 5: MYEX35A – AppWizard step 1 of 6, an SDI application.

 

Figure 5: MYEX35A – AppWizard step 1 of 6, an SDI application.

 

ActiveX document and Internet - Figure 6: MYEX35A – AppWizard step 2 of 6.

 

Figure 6: MYEX35A – AppWizard step 2 of 6.

 

ActiveX document and Internet - Figure 7: MYEX35A – AppWizard step 3 of 6, selecting the compound document, deselect the Automation and ActiveX Controls.

 

Figure 7: MYEX35A – AppWizard step 3 of 6, selecting the compound document, deselect the Automation and ActiveX Controls.

 

ActiveX document and Internet - Figure 8: MYEX35A – AppWizard step 4 of 6.

 

Figure 8: MYEX35A – AppWizard step 4 of 6.

 

Click the Advanced button. Set the File Extension as shown below.

 

ActiveX document and Internet - Figure 9: MYEX35A – Setting the file extension.

 

Figure 9: MYEX35A – Setting the file extension.

 

 

ActiveX document and Internet - Figure 10: MYEX35A – AppWizard step 5 of 6.

 

Figure 10: MYEX35A – AppWizard step 5 of 6.

 

ActiveX document and Internet - Figure 11: MYEX35A – AppWizard step 6 of 6, selecting CRichEditView as a base class.

 

Figure 11: MYEX35A – AppWizard step 6 of 6, selecting CRichEditView as a base class.

 

ActiveX document and Internet - Figure 12: MYEX35A –project summary.

 

Figure 12: MYEX35A –project summary.

 

Build and run MYEX35A.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ActiveX document and Internet - Figure 13: MYEX35A program output.

 

Figure 13: MYEX35A program output.

 

ActiveX document and Internet - Figure 14: MYEX35A output – typing some texts.

 

Figure 14: MYEX35A output – typing some texts.

 

ActiveX document and Internet - Figure 15: MYEX35A output – inserting an object.

 

Figure 15: MYEX35A output – inserting an object.

 

ActiveX document and Internet - Figure 16: Inserting a bitmap object.

 

Figure 16: Inserting a bitmap object.

 

ActiveX document and Internet - Figure 17: MYEX35A output – text and bitmap editor.

 

Figure 17: MYEX35A output – text and bitmap editor.

 

ActiveX document and Internet - Figure 18: Inserting a bitmap to MYEX35A output.

 

Figure 18: Inserting a bitmap to MYEX35A output.

 

ActiveX document and Internet - Figure 19: MYEX35A output with bitmap and text.

 

Figure 19: MYEX35A output with bitmap and text.

 

Then, save MYEX35A output. Click File Save As and save as test.35a.

 

ActiveX document and Internet - Figure 20: Saving MYEX35A output.

 

Figure 20: Saving MYEX35A output.

 

Try opening the saved file in Internet Explorer browser. You can also use http://localhost/test.35a to open the file.

 

ActiveX document and Internet - Figure 21: Opening MYEX35A output in browser.

 

Figure 21: Opening MYEX35A output in browser.

 

 

 

 

 

 

 

 

 

 

 

 

Further reading and digging:

  1. DCOM at MSDN.

  2. COM+ at MSDN.

  3. COM at MSDN.

  4. Win32 process, thread and synchronization story can be found starting from Module R.

  5. MSDN MFC 7.0 class library online documentation.

  6. MSDN MFC 9.0 class library online documentation - latest version.

  7. MSDN Library

  8. Windows data type.

  9. Win32 programming Tutorial.

  10. The best of C/C++, MFC, Windows and other related books.

  11. Unicode and Multibyte character set: Story and program examples.

 

 


 

| Tenouk C & C++ | MFC Home | IIS & ISAPI Programming 5 | ActiveX Doc Servers & Internet 2 | Download | Site Index |