home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 February / CHIPCD_02_2002.iso / Internet / Macromedia ColdFusion Server 5 / coldfusion-50-win-us.exe / data1.cab / Examples / CFDOCS / snippets / cfobjectcom.cfm < prev    next >
Encoding:
Text File  |  2001-06-13  |  3.8 KB  |  159 lines

  1. <HTML>
  2. <HEAD>
  3. <TITLE>CFOBJECT (COM) Example</TITLE>
  4. </HEAD>
  5.  
  6. <BASEFONT FACE="Arial, Helvetica" SIZE=2>
  7. <BODY  bgcolor="#FFFFD5">
  8.  
  9. <H3>CFOBJECT (COM) Example</H3>
  10. <!---
  11. Create a COM object as an inproc server (DLL).
  12. (CLASS= prog-id). Uses DocEx1.DLL, which may need
  13. to be registered on the CF Server machine.
  14. --->
  15. <!--- Check for platform. COM doesn't run under UNIX. --->
  16. <CFIF Server.OS.Name EQ "Windows NT">
  17. <CFOBJECT TYPE="COM"
  18.     ACTION="Create"
  19.     CLASS="Allaire.DocEx1.1"
  20.     NAME="obj"> 
  21.  
  22. <!---
  23. Call a method.
  24. Note that methods that expect no arguments should 
  25. be called using empty parenthesis.
  26. --->
  27. <CFSET obj.Init()>
  28.  
  29. <!---
  30. This object is a collection object, and should 
  31. support at a minimum:
  32. Property : Count
  33. Method : Item(inarg, outarg)
  34. and a special property called _NewEnum 
  35. --->
  36. <CFOUTPUT>
  37.   This object has #obj.Count# items.
  38.   <BR>
  39.   <HR>
  40. </CFOUTPUT>
  41.  
  42.  
  43. <!---
  44. Get the 3rd object in the collection.
  45. --->
  46. <CFSET emp = obj.Item(3)>
  47. <CFOUTPUT>
  48.   The last name in the third item is #emp.lastname#.
  49.   <BR>
  50.   <HR>
  51. </CFOUTPUT>
  52.  
  53. <!---
  54. Loop over all the objects in the collection.
  55. --->
  56. <P>Looping through all items in the collection:
  57. <BR>
  58. <CFLOOP COLLECTION=#obj# ITEM=file2>
  59.   <CFOUTPUT>
  60.     Last name: #file2.lastname# <BR>
  61.   </CFOUTPUT>
  62. </CFLOOP>
  63.  
  64. <CFELSE>
  65. <H4>COM supported is limited to the Windows/NT platform.</H4>
  66. </cfif>
  67.  
  68. <!-----
  69. IDL for the object
  70. **** Important Note ***********************************
  71. The Item() method takes a VARIANT as an [in] argument. ColdFusion
  72. cannot determine the type and simply sends a BSTR. IF THE OBJECT
  73. does not corece it to the right type (which it does in this case)
  74. using the following :
  75.  
  76.       HRESULT hr;
  77.  
  78.    // Check to see if a number has been passed
  79.    if (Index.vt != VT_I4)
  80.    {
  81.        hr = VariantChangeType(&Index, &Index, 0, VT_I4);
  82.    }
  83.  
  84. then an error will be reported. To avoid these type of problems,
  85. object developers should specify the type directly in the IDL.
  86.  
  87. For example, if the IDL had been coded :
  88.  
  89. HRESULT Item([in] long index, [out,retval] LPVARIANT pItem);
  90.  
  91. **** Important Note ***********************************
  92. import "oaidl.idl";
  93. import "ocidl.idl";
  94.  
  95.     [
  96.         object,
  97.         uuid(3607722F-3B5A-11D2-BEBB-00C04FA35D22),
  98.         dual,
  99.         helpstring("IEmployeeCollection Interface"),
  100.         pointer_default(unique)
  101.     ]
  102.     interface IEmployeeCollection : IDispatch
  103.     {
  104.         [propget, id(1), helpstring("property Count")] 
  105.         HRESULT Count([out, retval] long *pVal);
  106.         [id(2), helpstring("method Item")] 
  107.         HRESULT Item([in] VARIANT index, [out,retval] LPVARIANT pItem);
  108.         [id(3), helpstring("method Init")] HRESULT Init();
  109.         [propget, id(-4), helpstring("property _NewEnum"), restricted] 
  110.         HRESULT _NewEnum([out, retval] LPUNKNOWN *pVal);
  111.     };
  112.     [
  113.         object,
  114.         uuid(36077231-3B5A-11D2-BEBB-00C04FA35D22),
  115.         dual,
  116.         helpstring("IEmployee Interface"),
  117.         pointer_default(unique)
  118.     ]
  119.     interface IEmployee : IDispatch
  120.     {
  121.         [propget, id(1), helpstring("property EmployeeID")] 
  122.         HRESULT EmployeeID([out, retval] long *pVal);
  123.         [propput, id(1), helpstring("property EmployeeID")] 
  124.         HRESULT EmployeeID([in] long newVal);
  125.         [propget, id(2), helpstring("property LastName")] HRESULT LastName([out, retval] BSTR *pVal);
  126.         [propput, id(2), helpstring("property LastName")] HRESULT LastName([in] BSTR newVal);
  127.     };
  128. [
  129.     uuid(36077222-3B5A-11D2-BEBB-00C04FA35D22),
  130.     version(1.0),
  131.     helpstring("DocEx1 1.0 Type Library")
  132. ]
  133. library DOCEX1Lib
  134. {
  135.     importlib("stdole32.tlb");
  136.     importlib("stdole2.tlb");
  137.  
  138.     [
  139.         uuid(36077230-3B5A-11D2-BEBB-00C04FA35D22),
  140.         helpstring("EmployeeCollection Class")
  141.     ]
  142.     coclass EmployeeCollection
  143.     {
  144.         [default] interface IEmployeeCollection;
  145.     };
  146.     [
  147.         uuid(36077232-3B5A-11D2-BEBB-00C04FA35D22),
  148.         helpstring("Employee Class")
  149.     ]
  150.     coclass Employee
  151.     {
  152.         [default] interface IEmployee;
  153.     };
  154. };
  155. --->
  156.  
  157. </BODY>
  158. </HTML>
  159.