You must use the cfobject
tag or the CreateObject
function to create an instance of the COM object (component) in ColdFusion before your application pages can invoke any methods or assign any properties in the component.
For example, the following code uses the cfobject
tag to create the Windows CDO (Collaborative Data Objects) for NTS NewMail object to send mail:
<cfobject
type="COM"
action="Create" name="Mailer" class="CDONTS.NewMail">
The following line shows how to use the corresponding CreateObject
function in CFScript:
Mailer = CreateObject
("COM", "CDONTS.NewMail");
The examples in later sections in this chapter use this object.
Note: CDO is installed by default on all Windows NT and 2000 operating systems that have installed the Microsoft SMTP server. In Windows NT Server environments, the SMTP server is part of the Option Pack 4 setup. In Windows 2000 Server and Workstation environments, it is bundled with the operating system. For more information on CDO for NTS, see http://msdn.microsoft.com/library/default.asp?URL=/library/psdk/cdo/_olemsg_overview_of_cdo.htm.
The CDO for NTS NewMail component includes a number of methods and properties to perform a wide range of mail-handling tasks. (In the OLE/COM Object Viewer, methods and properties might be grouped together, so you could find it difficult to distinguish between them at first.)
The CDO for NTS NewMail object includes the following properties:
Body [ String ]
Cc [ String ] From [ String ] Importance [ Long ] Subject [ String ] To [ String ]
You use these properties to define elements of your mail message. The CDO for NTS NewMail object also includes a send
method which has a number of optional arguments to send messages.
The action
attribute of the cfobject
tag provides the following two ways to connect to COM objects:
cfobject action="Create"
) Takes a COM object, typically a DLL, and instantiates it prior to invoking methods and assigning properties.
cfobject action="Connect"
) Links to an object, typically an executable, that is already running on the server.
You can use the optional cfobject
context
attribute to specify the object context. If you do not specify a context, ColdFusion uses the setting in the Registry. The following table describes the context
attribute values:
The following example, which uses the sample Mailer COM object, shows how to assign properties to your mail message and how to execute component methods to handle mail messages.
In the example, form variables contain the method parameters and properties, such as the name of the recipient, the desired e-mail address, and so on:
<!--- First, create the object --->
<cfobject type="COM" action="Create" name="Mailer" class="CDONTS.NewMail"> <!--- Second, use the form variables from the user entry form to populate a number of properties necessary to create and send the message. ---> <cfset Mailer.From = "#Form.fromName#"> <cfset Mailer.To = "#Form.to#"> <cfset Mailer.Subject = "#Form.subject#"> <cfset Mailer.Importance = 2> <cfset Mailer.Body = "#Form.body#"> <cfset Mailer.Cc = "#Form.cc#"> <!--- Last, use the Send() method to send the message. Invoking the Send() method destroys the object.---> <cfset Mailer.Send()>
Note: Use the cftry
and cfcatch
tags to handle exceptions thrown by COM objects. For more information on exception handling, see "Handling runtime exceptions with ColdFusion tags," in Chapter 14.
When you use COM objects, consider the following to prevent and resolve errors:
The following sections describe these issues.
Improper threading can cause serious problems when using a COM object in ColdFusion. Make sure that the object is thread-safe. An object is thread-safe if it can be called from many programming threads simultaneously, without causing errors.
Visual Basic ActiveX DLLs are typically not thread-safe. If you use such a DLL in ColdFusion, you can make it thread-safe by using the OLE/COM Object Viewer to change the object's threading model to the Apartment model.
If you are planning to store a reference to the COM object in the Application, Session, or Server scope, do not use the Apartment threading model. This threading model is intended to service only a single request. If your application requires you to store the object in any of these scopes, keep the object in the Both threading model, and lock all code that accesses the object, as described in "Locking code with cflock," in Chapter 15.
COM object method in arguments are passed by value. The COM object gets a copy of the variable value, so you can specify a ColdFusion variable without surrounding it with quotation marks.
COM object out method arguments are passed by reference. The COM object modifies the contents of the variable on the calling page, so the calling page can use the resulting value. To pass a variable by reference, surround the name of an existing ColdFusion variable with quotation marks. If the argument is a numeric type, assign the variable a valid number before you make the call. For example:
<cfset inStringArg="Hello Object">
<cfset outNumericArg=0> <cfset result=myCOMObject.calculate(inStringArg, "outNumericArg")>
The string "Hello Object" is passed to the object's calculate method as an input argument. The value of outNumericArg is set by the method to a numeric value.
The following table described some error messages you might encounter when using COM objects: