All About COM

I l @ ve RuBoard

COM stands for component object model. We will start our look at COM with an overview of the two basic components of COM: a component and an object.

What Is a Component?

A component can be described as a piece of code that can be shared by other programs. For example, you might write a spell-checker component that could be used by a word processing package or email software.

What Is an Object?

An object can be described as code that is reusable. This sounds like a component, and indeed, a component can be made of objects. Perhaps your spell-checker has a dictionary object for checking words, a custom dictionary object for storing custom words, and an object for running through words and checking them against the dictionaries.

What Is COM?

COM is a model for creating components from objects. COM is a binary specification. In other words, it is not programming-language-bound. But all languages that support the creation of COM components must create the components to the same specification. In this way, COM components created in one programming language (such as ActiveState and Perl) can be reused in another COM-supported programming language (such as VB).

COM is still the cornerstone of Windows development. Almost everything that Microsoft does is based on COM. Windows is COM-based, Office is COM-based, Microsoft VB can create and interface with COM components ” the list is endless.

Server-Side or Client-Side COM

COM components are often divided into client-side or server-side components. Although they serve different purposes, they fundamentally serve the same purpose ”forming a reusable block of code in an application. A spell-checker component is an example of a client-side component, and a tax component for an e-commerce web site is an example of a server-side component. Server-side components can take advantage of newer elements for COM: Microsoft Transaction Server (MTS) and Microsoft Message Queue (MMQ). Client-side components can also be GUI components that can be used to make up a GUI for an application (such as a button).

A Brief History of COM

This section looks at how COM developed, from its early days as a way of sharing data to its current incarnation as a fully featured component system.

DDE

Dynamic Data Exchange (DDE) was developed in the late 1980s to allow Windows applications to share data (text and graphics) from the Clipboard. However, because DDE was available only to Microsoft applications, it was not a widely used or popular solution for transferring data between applications.

OLE 1

To resolve this problem, Microsoft developed OLE (Object Linking and Embedding), now called OLE 1. OLE 1 allowed you to insert a document from one application into the document of another. An example would be embedding a spreadsheet into a word processing package. Because the spreadsheet was linked, if you updated it, the update would appear in the embedded spreadsheet in the word processing document as well. OLE 1 was slow and suffered from bugs . However, Microsoft realized this and began developing OLE 2.

OLE 2

It was during the development of OLE 2 that Microsoft saw how OLE caused problems for applications. Different applications used different methods to allow the embedding of applications within themselves . OLE required many function calls and parameters that most applications achieved in different ways. Put another way, one problem lay in getting two applications to find a common way of communicating. COM was developed as a part of OLE 2 to overcome this problem.

COM/DCOM

After COM was born, it gave applications a common way of communicating. If the applications used COM, either built using COM objects or by using COM for an external interface, all COM applications would have a common way of communicating. COM applications could be COM objects themselves or could be made of COM objects.

Distributed COM (DCOM) was developed later to allow COM objects to communicate over a network (rather than the same computer). It's often described as "COM with a long wire."

COM+

Microsoft began a concentrated effort to allow developers to use COM as a way to build applications from components. With the arrival of COM support in ASP and DCOM, Microsoft saw how developers were building applications from COM components that were housed on several different computers.

To build on this, Microsoft added Microsoft Transaction Server (MTS) and Microsoft Message Queue (MSMQ) to the Windows NT 4 service pack.

MTS added transaction services that COM objects could call on, and MSMQ allowed messages between COM objects to be stored in a queue. If a message could not get through, it could be stored and forwarded later.

With the arrival of Windows 2000, however, Microsoft developed a version of COM to integrate MTS and MSMQ within COM so that natively it had DCOM, transaction, and messaging services available. Microsoft called this version COM+.

What Makes Up a COM Object?

In its simplest form, a COM object consists of one or more classes, as shown in Figure 7.1. Each class consists of a collection of methods, and within these methods is the code that makes up your object. Methods can be private, meaning that they can be used only by other methods within the same class. Methods can also be public, meaning that they can be used by all methods and are exposed to the outside world (and thus are the only kinds of functions you can use when calling a COM object).

Figure 7.1. A COM object in its simplest form.

graphics/07fig01.gif

COM has a lot more to it than what I have described. However, because an in-depth discussion of COM's internals is beyond the scope of this book, I recommend the books listed in the next section for further information.

How Are COM Objects Created?

COM objects can be created in any programming language that supports the COM binary specification. All Microsoft programming tools (such as Visual Basic and Visual C++) support COM, as do ActiveState programming tools (Perl, Python,TCL) and Borland programming tools (such as Delphi), to name just a few.

If you are interested in learning more about COM's internals and COM development, I suggest the following books:

  • Appleman, Dan. Dan Appleman's Developing COM/ActiveX Components with Visual Basic 6. Sams Publishing, 1998.

  • Brill, Gregory. Applying COM+. New Riders Publishing, 2000.

Creating a Server-Side COM Component in VB 6.0

Creating a COM object in VB is very straightforward. First, open VB and select ActiveX DLL, as shown in Figure 7.2.

Figure 7.2. Selecting an ActiveX DLL project type inVisual Basic.

graphics/07fig02.gif

Call the project php4winbook and name the class Examples, as shown in Figure 7.3.

Figure 7.3. The object and class name of your COM object.

graphics/07fig03.gif

Next, add the following code to the examples:

 Option Explicit  Public Function HelloFunc(ByRef uname As Variant) As Variant        HelloFunc = "hello " & uname  End Function 

Note that you have created a single function called HelloFunc ; this will become important to remember later in this chapter.

Finally, compile the component. This gives you php4winbook.dll. Because you have built your COM component from within VB, it is registered for use on the system you built the COM component on. If you want to use the COM component on another system, you must register it using the following command:

 RegSvr32 php4winbook.dll 

Also note that if you create COM components in VB that use the ByRef statement, they might not work in PHP 4.0.4. This appears to be because of an issue with the ByRef statement and PHP. This has been resolved in PHP 4.0.6.

How Are COM Objects Referenced?

Programming languages have different ways of referencing COM objects, but they all call the COM component's name. The call to the COM component's name is passed to Windows, which looks up the name in its Registry. After the name is found, Windows looks up the COM component's location (stored in its Registry entry) and loads it into memory. The memory location of the COM object is then passed back to the calling application. When the calling application has finished with the COM object, it should pass this information back to Windows so that Windows can unload the COM object from memory (to preserve system resources). Most languages support an automatic feature that does this for them. Nevertheless, it is a good practice to include this in your code when using COM components.

When working with the Microsoft IIS web server, it is possible to create your own memory space for your web site (Microsoft calls them web applications) so that all COM components you use are created by Windows within that memory space. It's important to note, however, that IIS holds that COM component in memory space until the web application or web server is stopped . This increases the speed with which IIS can pass COM objects back to calling applications, but it does mean that you must unload them from the web application if you want to change or recompile your COM object (or Windows will hold them as locked).

Interfacing with a COM Component in Classic ASP

As an example, we can look at how classic ASP (called ASP in the rest of this chapter) handles COM components. ASP supports the interfacing of COM, not the creation of COM objects. However, its main scripting languages, VBScript and JScript, can both be exposed as COM objects.

Let's start with some ASP code:

 <%  set test = Server.CreateObject("php4winbook.Examples")  hellmes = test.HelloFunc ("Andrew")  set test = nothing  Response.write hellmes  %> 

In this code, ASP loads into memory the php4winbook COM component you created a moment ago.

Let's look at each line of the code:

 set test = Server.CreateObject("php4winbook.Examples") 

Note that you have interfaced with the Examples class. COM can interface with only one class of a COM component in any one recall. Different applications can call on different classes of the same component, but in one call it's one class only.

 hellmes = test.HelloFunc ("Andrew") 

This line says to pass some information to the HelloFunc function of the Examples class and store its return result in hellmes.

That result is then printed to the screen, as shown in Figure 7.4.

Figure 7.4. The output of your COM object from ASP.

graphics/07fig04.gif

Note that you also unload the COM component from memory:

 set test = nothing 

How Does PHP Handle COM?

Like ASP, PHP supports the interfacing of COM, but not the creation of COM objects. In other words, it can interface with any COM object, but you cannot yet create COM objects with PHP code.

PHP's COM Functions

PHP has featured COM support since PHP 3.03. These same functions are still available in PHP 4. You can interface with your php4winbook COM component in the following way:

 <?php  //load COM component into memory  $comobj = com_load("php4winbook.Examples");  //set the property of the function to a value  com_set($comobj, "HelloFunc", "Andrew");  //obtain the result of the function  $hellmes = com_get($comobj, "HelloFunc");  print($hellmes);  ?> 

Let's look at some of these code lines:

 $comobj = com_load("php4winbook.Examples"); 

This line loads the COM component into memory. Note that, as in the ASP example, you can do this on only a single class level.

You then set the value of the HelloFunc function to the value Andrew :

 com_set($comobj, "HelloFunc", "Andrew"); 

You then obtain the return result of the HelloFunc function:

 $hellmes = com_get($comobj, "HelloFunc"); 

Although you are guaranteed backward compatibility between versions of PHP, the syntax can become messy. PHP 4 has a shorthand version for using COM components:

 <?php  $comobj = new COM("php4winbook.Examples");  $hellmes = $comobj->HelloFunc("Andrew");  print($hellmes);  ?> 

Let's look at a couple of these code lines:

 $comobj = new COM("php4winbook.Examples"); 

This loads the COM component into memory. What happens here is that when you call the COM component using PHP's COM class, you create a link between the COM component and PHP. Note that when you call the PHP component, you can do so at only a single class level and access public functions in that class. So when you call a function of the COM component, PHP translates that call into data that the COM component can understand (such as changing data types). Note the different syntax for loading the COM component into memory between ASP and PHP.

 $hellmes = $test->HelloFunc("Andrew"); 

Next you pass some information to the HelloFunc function and store its return result in $hellmes . This is a better method of setting and getting the value of functions, because you can do so in one line of code and don't need to use PHP functions. PHP makes the function call within the COM component for you. Note the PHP forward notation.

Finally, the result is printed to the screen, as shown in Figure 7.5.

Figure 7.5. The output of your COM object from PHP.

graphics/07fig05.gif

If you think there is very little difference between the way the ASP and PHP code works, you are right. Both languages must load the COM component into memory, where they then call its functions. This also demonstrates one of the beauties of using COM. You wrote the COM component in VB and accessed it in both ASP and PHP. And you could rewrite the COM component in C++ ”it makes no difference to your ASP or PHP code.

I l @ ve RuBoard


PHP Programming for Windows
PHP Programming for Windows (Landmark (New Riders))
ISBN: 0735711690
EAN: 2147483647
Year: 2002
Pages: 99

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net