Few people can look at ColdFusion’s built-in functions and features and say, “Is that a11?”However, power users will be happy to note that ColdFusion is extensible with a wide variety of tools and can even use pre-built components intended for other development environments, such as Microsoft’s Active Server Pages or Visual Basic. Many capabilities that are not built into ColdFusion (such as Network News Transfer Protocol image processing faxing and more) can be obtained at a relatively low cost from thirdparty components that are already available
In this chapter we will discuss the types of external objects that ColdFusion can harness and provide details on using Component Object Model (COM) objects with Cold-.Fusion. We will also discuss how to convert sample code that may come with a COM object into CFML.
In addition we will look at a new feature of ColdFusion 4.5: the ability to execute external system programs using the CFEXECUTE tag.
Understanding the Types of External Objects
The four main types of external objects usable from Cold Fusion are CFX tags COM objects, Java objects and Common Object Request Broker Architecture (CORBA) objects.
CFX tags are written in C++ Java or Delphi using the ColdFusion Application Programming Interface (CFAPI). They enable you to move complicated logic into a compiled dynamic-link library (DLL) for better performance, and to access functionality not available within the CFML environment. CFX tags after they are registered in the Cold-Fusion Administrator can be used like any other ColdFusion tag. Chapter 31 “Building ColdFusion Custom Tags” has a more in-depth discussion of CFX tags.
CORBA objects are supported only in the Enterprise edition of ColdFusion Server. Similar to COM, the Common Object Request Broker Architecture is a specification for software objects to communicate with one another. CORBA is most prevalent on Unix
systems, and hence will become a larger part of mainstream ColdFusion development as Allaire releases versions of ColdFusion for more Unix-based operating systems. CORBA objects can be used in ColdFusion via the CFOB] ECT tag. A detailed discussion of
CORBA usage in ColdFusion is beyond the scope of this book, but you can find mo~e information on CORBA at the Object Management
Using an embedded Java Virtual Machine, any Java class on the class path specified by the Cold Fusion Administrator can be loaded in a ColdFusion template as an external object. Java classes have the benefit of being cross-platform at the expense of the speed of compiled native code such as COM objects and C++-based CFX tags. However a wide variety of Java classes is available on the Internet to satisfy most needs. At this time, onlJ’ ColdFusion 4.5 and above for Windows NT and 2000 supports the use of external Java objects
COM objects like CFX tags are pre-compiled for better performance and provide access to functionality unavailable inside ColdFusion. Unlike CFX tags, a great variety of COM objects created by third-party vendors is already available. The Component Object Model is Microsoft’s standard for an interface that programs can use to provide services to one another without having to understand each other’s internal structure or even be written in the same language. COM objects can be written in any number of languages including Visual Basic C++ Delphi and Java. COM objects are also frequently used in Microsoft’s Active Server P?-ges-a server-side scripting, environment for Web pages similar to ColdFusion. One of the best things about Active Server Pages, as far as .Coldf’usion developers are concerned is that ASP’s relative lack of built-in functionality has engendered a burgeoning Web-related component industry to provide that missing functionality. Of course with Coldfusion’s COM support you can take advantage of many of these objects in your ColdFusion applications
Alternatives to CFXTags
If you need functionality not provided in ColdFusion,before writing a CFX tag it maybe worth while to check ASP- and Visual Basic-related Web sites for a’ pre-built component that will do the job. Somegood plates to start lookinginclude
Installing and Using COM Objects
Most third-party COM objects come with insal’lation instructions but the same basic steps apply to nearly all of them:
1. Place the files for the object into a directnry with the appropriate NT permissions.
2. Register the component by using regsw’32. exe.
3. Check the threading model
Start by placing the file or files associated with the object into a directory you have chosen. These files are usually DLL or OCX fil~ but will occasionally be of another type such as EXE. The directory the files are placed in is unimportant (unless the installation instructions for the object specify otherwise) except that anonymous users to your Web site need permission to execute the objects. For Microsoft Internet Information Server this means that the NT account IUSR_Machine Name must have read access to the
file. Some developers like to have a central location for all components; others simply place them in the \WINNT\SYSTEM32 directory
Next, it’s time to register the component so that the operating system and Cold Fusion Know where to find it. Unless the instructions that came with the component specify differently this is accomplished with regsvr32 . exe. Simply open a command prompt change to the directory where you have placed the files for this component and type regsvr 32 . exe file name. You should receive a message that the component was successfully registered. If an error occurs it’s probably time to contact the component maker for support.
Most of the time you will not nave to worry about the component’s threading model; however, if ColdFusion seems to lock up on calling the component he threading model is the first thing to check. To do this, you need Microsoft’s OLE/C(, f Object Viewer. If you do not already have OLEView, as it is commonly known, the first step is to download it from resource/o 1evi ew. asp.
You can use OLEView to view information about all the COM objects installed on your system. That’s a lot of components in most cases, so it may take a little browsing before you find the one you’re after. Once you have, select it and cheose the Implementation tab in the main window. Look for the Threading Model list box. Possible values for the list box are Apartment, Both, Free, and None. If the current value is None, set it to Apartment; otherwise, try Apartment or Free. It may take some experimentation before you find the threading model for this component that works with Coldfusion.Note that you may need to restart Coldfusion or reboot the server after each threading-model change.
Using COM Objects with ColdFusion
Before you can start using your installed component in Coldf’usion, you must create a reference to the object. This is done with the (FOB)ECTtag. All you need to know about the object at this point is its program 10, or PROGID.This is generally included in the component’s documentation but can also be found on the Registry tab in OLEView or by searching for the filename in RegEdit. To pick a simple example, we’ll use the ASPCrypt component. Since version 4.0 Cold Fusion Server has included the built-in Encrypt and Decrypt functions that provide similar functionality to ASPCrypt, but this example makes it easy to understand the propcess involved in using COM Objects in Cold Fusion.
ASPCrypt is a free component written for ASP. It duplicates exactly the functionality of the Unix Cryptf ) function. It performs one-way encryption and is useful for passwords stored in a database (you encrypt the password that the user types in and compare it to
One difficulty that ColdFusion developers may encounter when using COM objects is that seldom will any of the sample code be written in CFML. The most common languages for an)”sample code included with a commercial component are ASP/VBScript and Visual Ba~1\ …1-1.:> such, a certain amount of transl~tion must be done on the part of the ColdFusion developer. Fortunately this is relatively simple and once learned will be almost identical for any component you choose to utilize
Any language that utilizes COM objects must first create reference to the object.In ASP sample code the object creation might look like this:
The important thing to remember about (FOB]ECTis that unlike most other ColdFusion tags, you need. not include this tag in every place you wish to use the object it refers to. (FOB]ECTcreates a reference to an object that can be used any number of times
throughout the remainder of the same template. In the case of .ASPCrypt, if you wanted’ to loop through an array of strings and encrypt each string in the array,’lt’would be counterproductive to include the (FOB]ECTtag inside your loop. Doing so ~ould re-create
the object each time through the loop and potentially place a significant strain on the sen,1r. Instead you would use (FOB]ECTto create a reference to ASrCrypt outsite of -.” the loop and then use the object each time through the loop.
Starting with ColdFusion 4.5, you can optionally use the (reateObj ect function to create a COM object in a ColdFusion template. For instance, instead of the (FOB]ECTtag we just used to create the obj Crypt object, you could use the following tag
To use a COM object, you need to know about its properties and methods. A property is a variable internal to the COM object, whereas a method is a function of the COM object (in fact, this is exactly how ColdFusion treats them). Most objects come with documentation on properties and methods, but if you need to use an undocumented component the View Type Information feature of OLEView can give you basic information on an object’s properties and methods
Often the simplest way to learn how to use a new object is to examine and modify ” the sample code that comes with it. Unfortunately as we said earlier chances are the sample code will not be in CFML. The task then becomes translation of the sample code to something that Coldfusion will accept. Sample code for ASPCrypt might look something like this: