1/8/07

PHP, COM, .NET and asp

I wanna start a topic about Com, .NET and PHP.
First, What is COM?

All About COM
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).


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.


Call the project php4winbook and name the class Examples

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.


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:


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:

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.


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.
------
Now that we have examined COM, we will finish with a look at Microsoft .NET.

What Is Microsoft .NET?
It is important to remember that .NET is not COM, and vice versa. .NET is a brand-new strategy from Microsoft. As mentioned at the start of this chapter, COM is the cornerstone of the current Microsoft platform. .NET, however, is the cornerstone of future Microsoft development. .NET applications run not only on Microsoft platforms but on any device (your PC, your WebTV, your handheld, and so on).

.NET is a combination of things. Largely, it's a framework that encompasses a Common Language Runtime (CLR, discussed in a moment) and common class set (also covered in a moment). It also gives application developers new ways of working with the platform they are targeting and creating the applications they want to deploy. As we will see, .NET is a far more powerful way of working than COM and is decidedly COM's successor.

What Makes Up Microsoft .NET?
Microsoft .NET is a broad concept that Microsoft splits into four terms:

.NET framework

.NET development tools

.NET OSs and servers

.NET services

.NET Framework
This is how .NET applications are created; they use the CLR and a common class set. Microsoft provides the .NET framework SDK to allow you to do this.

The .NET framework also gives you several new languages that make use of the CLR and common class set:Visual Basic .NET, a new version of Visual Basic; C# (pronounced "C sharp"), a language with the control and power of C and C++ but with the rapid application development capability of a language like Visual Basic; and J# (pronounced "J sharp"), which is a version of Java for the .NET framework.

.NET Development Tools
Microsoft offers Visual Studio .NET for easy .NET development. You don't need Visual Studio .NET to create .NET applications, but it has been developed to aid in this process.

.NET OSs and Servers
Microsoft has rebranded most of its product line to use the .NET name. However, Microsoft OSs and servers don't yet use the .NET framework. (However, plans are underway to include the CLR in future versions of Microsoft Server 2000 and to allow the creation of stored procedures in C# for SQL Server.) Microsoft also plans to release the next version of Windows 2000 as Windows .NET. This OS won't be based on the .NET framework, but it will contain parts of the .NET framework.

.NET Services
Web services (discussed in Chapter 9, "PHP and Web Services") are a big part of Microsoft .NET. They are very easy to build and deploy using Microsoft .NET. To aid in this sort of development, Microsoft is building a set of web services called Microsoft MyServices to allow your web service applications to connect to Microsoft services such as Microsoft Passport and HotMail.

What Makes Up the .NET Framework?
The .NET framework is split into two parts: the CLR and the common class set.

CLR
The Common Language Runtime (CLR) is the runtime of the .NET framework. In a .NET language (such as Visual Basic .NET or C#), the compiler compiles the code to something called MSIL (Microsoft Intermediate Language). MSIL can then be run on any CLR. The CLR itself can be targeted at any platform (Windows, Linux, Solaris, UNIX, and so on), so instead of your having to rewrite your .NET application to suit the platform, all the platform needs is a suitable CLR. In theory, this means that your applications can target any application that has a suitable CLR.

Note

The CLR is to be ported to the FreeBSD UNIX platform by Microsoft and to Linux by several third parties.



This approach might be familiar to Java developers. In the case of Java, the common runtime is called the Java Runtime Environment (JRE).

Common Class Set
The .NET framework provides a common class set that a .NET-compatible language can use. The common class set provides you with classes for many things, from handling data such as strings and variables to working with the Win32 API. The class set handles much of the functionality that was often difficult, such as working with databases with ADO (now called ADO.NET).

.NET Applications
.NET applications can be several different things, from conventional Windows GUI applications to console applications, web services, Windows services, and .NET components (which is the .NET equivalent of COM components).

All .NET applications can make use of something called an assembly. Microsoft .NET assemblies allow a .NET application to package itself into a single compact unit. For example, an assembly can contain an application's .exe file, any related component (DLL) files, and graphics files. The assembly spells the end of "DLL hell," in which different versions of the same DLL could be installed to a computer, causing compatibility problems. Now an application needs to look no further than its assembly.

Assemblies can be of two types: private and shared. In a private assembly, the assembly's contents are available only to the application within that assembly (any DLL files in that assembly can be used by that assembly's application). A shared assembly can be accessed by files (applications, DLLs, and so on) in other assemblies. A shared assembly is stored in the GAC (Global Assembly Cache), which you can view under C:\WINNT\Assembly\ if you have the .NET framework installed. You can use the shfusion.dll (a Windows shell extension that comes with the .NET framework) to obtain more information about the GAC.

Obtaining the .NET Framework
The .NET framework currently is in version 1.0. It is available for Windows XP, Windows 2000, and Windows NT 4.0 platforms only. You can download the .NET framework from http://www.gotdotnet.com. You can also order it on a CD for free (there is a small shipping charge).

.NET and PHP
PHP currently does not support .NET natively. You can access .NET libraries, but PHP has no support for the CLR. Daniel Beulshaushen of php4win.com has developed a version of PHP and a related PHP extension that can access .NET libraries. However, this is currently in beta and cannot be used with standard PHP extensions. However, you can access .NET libraries and other features by wrapping them into .NET components and exposing such components to PHP using a feature of .NET called COM Interop.

COM Interop lets you create a COM wrapper around your .NET components. The .NET component remains a .NET component, but it uses the CLR and is built using the .NET framework. However, it is built in such a way that Windows thinks it's a COM component and makes it available to all calling COM clients.

In this way, you can make a .NET component available to PHP via the same COM features of PHP discussed earlier. To use a .NET component with PHP successfully, you must make sure that you place your .NET component in the GAC. Unlike COM, .NET does not use the Registry to find components; it uses the GAC. In order for your PHP script to find your component, you must place it in the GAC.

Creating the .NET Component
The first step is to create the .NET component. You could use Visual Studio .NET for this, but all you need is the .NET framework installed. As such, you will use a simple text editor (Notepad is fine) and the command line.

Creating the examples.vb File
Create the file examples.vb using the following code:

Namespace php4winbook

Public Class examples

Public Function showname(ByVal name As String)
Return "hello " & name

End Function

End Class

End Namespace
This is Visual Basic .NET code, but you can use any .NET language you choose (such as C#). Save the file as examples.vb.

Creating a Key File
Every component in the GAC must be versioned and signed. To do this, you will use a cryptographic key. You create the key from the command line by typing the following:

sn -k key.snk
This creates a cryptographic public key (a private key to encrypt and a public key to decrypt) called key.snk. To create this key, the CLR uses the contents of the assembly using a private key it creates itself. Anyone can use the public key to decrypt the assembly. It helps ensure that the assembly has not been tampered with. (If the assembly's contents have changed, the public key would be different.)

Creating a Reference File
You must now add a reference to the component and use that reference to sign the assembly. Create the file AssemblyInfo.vb and add the following:

Imports System.Runtime.CompilerServices



Note that you need to use the Imports statement and that


must point to the directory containing the key.snk file (in case it's in the same directory as the VB.NET files).

Compiling a Reference File
You must now compile the reference file with the following:

vbc /t:module /out:AssemblyInfo.dll AssemblyInfo.vb
Compiling examples.vb with a Reference File
Now that you have the compiled reference file, you can use it when you compile the examples.vb file. This indicates to the CLR that you have a reference file and that it can be used to sign the assembly.

The following line creates the assembly file examples.dll:

vbc /t:library /addmodule:AssemblyInfo.dll examples.vb
A Shortcut for Creating a Signed Assembly File
You can add the reference file directly to the VB.NET code:

Imports System.Reflection
Imports System.Runtime.CompilerServices




Namespace php4winbook

Public Class examples

Public Function showname(ByVal name As String)

Return "hello " & name

End Function

End Class

End Namespace
You can compile with this:

vbc /t:library examples.vb
However, if you have many components in your assembly, you might want to keep the reference file and the code for various assembly contents separate (thus making changes to the reference file code easier to make), as you did in the first example.

Adding examples.dll to the GAC
You must now add the signed assembly file to the GAC using the following:

drive letterProgram Files\Microsoft.NET\FrameworkSDK\Bin\gcutil /i
examples.dll
If you open the GAC in Windows Explorer (drive letter\WINNT\Assembly\), you should see the entry for your component


Adding the Assembly to the Registry
In order to complete the process of fooling Windows, you must create a type library (TLB) file for your assembly. COM requires TLB files so that it can gather the information it needs about the component to access its public functions correctly (that is, what data type the function is and what data type it needs to send and receive from those functions).

Regasm examples.dll /tlb:examples.tlb
Remember that Windows looks for COM components in the Windows Registry, but .NET components reside in the GAC. Regasm fools Windows by adding information to the Registry about your component, but instead of loading the COM component from the file system, it loads from the GAC.

Creating the PHP File
Create the file testcode.php with the following code:

showname("Andrew");
print($hellmes);

?>
Notice that you have used the same syntax as the COM example you built earlier in this chapter. As far as PHP is concerned, you are using a COM component.

Running the PHP File
If you now run the testcode.php file, you should see the result printed to the screen

1 comment:

Anonymous said...
This comment has been removed by a blog administrator.