home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / MS_DEV / VID / SERVER / ASF / DATA.Z / atumd3.asp < prev    next >
Text File  |  1996-10-22  |  16KB  |  244 lines

  1. <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
  2.  
  3. <html>
  4.  
  5. <head>
  6. <title> Module 3: Creating Your Own ActiveX Server Components </title>
  7.  
  8. </head>
  9.  
  10. <BODY BGCOLOR="#FFFFFF"><FONT FACE="ARIAL,HELVETICA">
  11.  
  12. <h1><a name="ChapTop">Module 3: Writing Your Own ActiveX Server Components</a></h1>
  13.  
  14.  
  15. <p>Now that <a href="atumd2.asp">Module 2</a> has familiarized you  with the components Active Server Pages (ASP) provides, it’s time to think about creating your own components, components that meet your specific needs. 
  16.  
  17. <p>Suppose that you want to provide access to specific financial functions through your Web site. ASP does not explicitly provide access to such functionality, but getting it is as easy as creating your own ActiveX server component—which you will do in this module. You will call your component from a form like the one that follows. 
  18.  
  19. <p><strong><a name="note">Note</a>   </strong>To  complete this module, you must have the 32-bit version of either  Visual Basic 4.0 Professional Edition or Visual Basic 4.0 Enterprise Edition installed on your computer.
  20.  
  21.  
  22. <!--The purpose of the server component is to 
  23. provide any OLE-compliant program (including ASP) access to financial functions. This component will be created using Visual 
  24. Basic. Visual Basic has a rich set of financial functions that will be exposed as methods of your Finance component. </p> -->
  25.  
  26. <hr>
  27.  
  28. <h2><a name="CreatingtheFinanceComponent">Lesson 1: Creating the Finance Component</a></h2>
  29.  
  30. <p>A component should contain a set of related methods (functions) that provide added value beyond what is in the scripting language that will 
  31. be calling it. Because VBScript does not provide financial functions, you must give access to this functionality by using your Finance server 
  32. component. This server component could expose all of the Visual Basic finance functions including the <strong>DDB</strong> function (double-declining 
  33. balance), <strong>FV</strong> function (future value), <strong>IPmt</strong> function (interest payment), <strong>IRR</strong> function (internal rate of return), and others. Only the 
  34. implementation of the <strong>FV</strong> function will be documented in this tutorial. </p>
  35.  
  36. <h3><A NAME="StartVisualBasic">Start Visual Basic</A></h3>
  37.  
  38. <ol>
  39. <li>Click the <strong>Start</strong> button on the taskbar.</li>
  40. <li>Select<strong> Visual Basic 4.0</strong>. </li>
  41. <li>Click <strong>Visual Basic 4.0</strong> in the submenu to run the design environment. </li>
  42. </ol>
  43.  
  44. <h3><A NAME="LearnMoreAboutVisualBasicFinanceFunctions">Learn More About Visual Basic Finance Functions</A></h3>
  45.  
  46. <p>The Visual Basic Help system describes the available functions.</p>
  47.  
  48. <ol>
  49. <li>Click <strong>Help</strong>. </li>
  50. <li>Select <strong>Search For Help On</strong>.</li>
  51. <li>With the <strong>Index</strong> tab selected, type <strong>finance</strong> as the word to look for.</li>
  52. <li>Double-click the <strong>finance</strong> index entry. </li>
  53. <li>Click <strong>FV Function</strong> to learn more about it.</li>
  54. <li>Close the <strong>Visual Basic Help</strong> dialog box when you have finished reviewing the finance functions.</li>
  55. </ol>
  56.  
  57. <h3><A NAME="NametheProject">Name the Project</A></h3>
  58.  
  59. <p>Visual Basic uses the project name as the first part of the name that is referenced in order to use the server component in an ASP script. </p>
  60.  
  61. <ol>
  62. <li>Click <strong>Tools</strong>. </li>
  63. <li>Select <strong>Options</strong>.</li>
  64. <li>Click the <strong>Project</strong> tab. </li>
  65. <li>Double-click the value <strong>Project1 </strong>in the <strong>Project Name</strong> text box. </li>
  66. <li>Type <strong>MS</strong> and click <strong>OK</strong>. </li>
  67. </ol>
  68.  
  69. <p>The project is now named MS. Later, you will reference the Finance server component as <FONT FACE="COURIER"><code>MS.Finance</code></FONT> from an ASP script. </p>
  70.  
  71. <h3><A NAME="Removethedefaultform">Remove the default form</A></h3>
  72.  
  73. <p>The next section lays the groundwork for the Finance server component. Methods and properties provide the interface between ActiveX Server components and Visual Basic scripts and 
  74. other languages. Because browsers can request a script to run on the server, the executing script can ask for information from a server component and then format and return that information to the browser. The server component must not bring up a dialog box on the server’s screen because the screen will not be displayed on the client browser.</p>
  75. <p>Visual Basic version 4.0 automatically creates a form. You will remove this default form from your project because it will not be used. </p>
  76. <ol>
  77. <li>In the <strong>View</strong> menu, select <strong>Project</strong>. </li>
  78. <li>Select <strong>Form1</strong> within the <strong>Project1</strong> project window. </li>
  79. <li>In the <strong>File</strong> menu, select <strong>Remove File</strong>. </li>
  80. </ol>
  81.  
  82. <h3><A NAME="AddtheFinanceClasstotheProject">Add the Finance Class to the Project</A></h3>
  83.  
  84. <p>In Visual Basic, to create a component with a set of functionality you can call, you define a class. This class groups methods and properties. 
  85. In your project, it will be the place within which you specify your finance methods. </p>
  86. <ol>
  87. <li>From the <strong>Insert</strong> menu, choose <strong>Class Module</strong>. </li>
  88. <li>Press the F4 key to display the property sheet for Class1. </li>
  89. <li>Click the value <strong>0 - Not Creatable</strong> for <strong>Instancing</strong>. </li>
  90. <li>Click the arrow, then select <strong>2 - Creatable MultiUse</strong>. </li>
  91. <li>Triple-click <strong>Class 1</strong> to select the class name. </li>
  92. <li>Type <strong>Finance</strong> to change the class name. </li>
  93. <li>Click <strong>False </strong>for the property <strong>Public</strong>.</li>
  94. <li>Type <strong>t </strong>to select <strong>True</strong> for the property <strong>Public</strong>. </li>
  95. <li>Close the <strong>Finance</strong> property sheet </li>
  96. </ol>
  97.  
  98. <h3><A NAME="AddtheCalcFVFunctiontotheFinanceclass">Add the CalcFV Function to the Finance class</A></h3>
  99.  
  100. <p>The Finance server component does require some programming code. This code will make the Visual Basic built in future value function available to languages making use of your component. </p>
  101.  
  102. <p>In the Finance class window, type the following lines:</p>
  103.  
  104. <blockquote>
  105. <FONT FACE="COURIER"><pre>Public Function CalcFV(rate, nper, pmt, Optional pv, Optional whendue) 
  106. CalcFV = FV(rate, nper, pmt, pv, whendue)
  107. End Function</pre></FONT>
  108. </blockquote>
  109.  
  110. <h3><A NAME="AddtheComponentsEntryPoint">Add the Component’s Entry Point</A></h3>
  111.  
  112. <p>All server components require an entry (starting) point. This is the code that will be called when the object is first made available to a 
  113. language. In VBScript, when you use <strong>Server.CreateObject</strong>, an instance is created of an object. When the <strong>Server.CreateObject </strong>statement 
  114. is executed the <strong>Sub Main</strong> procedure in a server component (created with Visual Basic) is called. </p>
  115.  
  116. <p>Your finance component does not have to do anything special to initialize itself when it is called. For that reason, you only have to provide an 
  117. empty (no Visual Basic statements) <strong>Sub</strong> <strong>Main</strong> procedure. </p>
  118.  
  119. <ol>
  120. <li>In the <strong>Insert </strong>menu, select <strong>Module</strong>.<br>
  121. </li>
  122. <li>In the <strong>Module 1</strong> window, type <strong>Sub Main.</strong><br>
  123. </li>
  124. <li>Press ENTER.<br>
  125. </li>
  126. </ol>
  127. <blockquote>
  128.  
  129. <p>This automatically enters the following code: </p>
  130.  
  131. </blockquote>
  132. <blockquote>
  133. <p><FONT FACE="COURIER"><code>Sub Main()<br>
  134. End Sub</code></FONT> <br>
  135. </p>
  136. </blockquote>
  137.  
  138. <h3><A NAME="SavetheFinanceProject">Save the Finance Project</A></h3>
  139. <p>When you save your work, you will be asked to save all three parts of the Visual Basic project. These include the project file, the class 
  140. module, and the code module. </p>
  141.  
  142. <ol>
  143. <li>Open the <strong>File </strong>menu.</li>
  144. <li>Select <strong>Save Project</strong>. </li>
  145. <li>In the <strong>File name</strong> text box, type <strong>c:\winnt\system32\inetsrv\asp\cmpnts\Finance</strong>.  (If you did not accept the default installation directory, substitute the name of your installation directory  for \winnt\system32.)</li>
  146. <li>Click the <strong>Save</strong> button. <br>
  147. </li>
  148. <li>Click the <strong>Save</strong> button to save Module1. <br>
  149. </li>
  150. <li>Double-click the value <strong>Project1</strong> in the <strong>File name</strong> text box to select it. </li>
  151. <li>Type the name <strong>Finance</strong> for the Project file (.vbp). </li>
  152. <li>Click the <strong>Save</strong> button to save the project .</li>
  153. </ol>
  154.  
  155. <h3><A NAME="MaketheComponentanIn-ProcessComponent">Make the Component an In-Process Component</A></h3>
  156.  
  157. <p>Visual Basic allows you to create in-process ActiveX components (OLE Automation Servers) and out-of-process ActiveX components. An <em>in-process</em> ActiveX component is a dynamic-link library (file name extension .dll that is loaded by the calling process. An <em>out-of-process</em> ActiveX component is an executable (file name extension .exe) that runs as a separate process from the calling application. Because in-process components are in the same process space as the calling process, they provide better performance than out-of-process components.</p>
  158.  
  159. <p>To make the Finance server component an in-process ActiveX component</p>
  160.  
  161. <ol>
  162. <li>Open the <strong>File</strong> menu.</li>
  163. <li>Select <strong>Make OLE DLL File</strong>. </li>
  164. <li>Click the <strong>Options</strong> button. </li>
  165. <li>Select the <strong>Auto Increment</strong> check box. </li>
  166. <li>Click <strong>OK</strong>. </li>
  167. <li>Type <strong>c:\winnt\system32\inetsrv\asp\cmpnts\Finance.dll</strong> in the File name text box and click <Strong>OK</Strong>. (If you did not accept the default installation directory, substitute the name of your installation directory  for \winnt\system32.)</li>
  168. <li> Exit Visual Basic.</li>
  169. </ol>
  170.  
  171. <h3><A NAME="RegistertheFinanceServerComponent">Register the Finance Server Component</A></h3>
  172.  
  173. <p>All server components must be registered. Windows NT and Windows 95 make use of the system registry to keep track of what server 
  174. components are available for use. By registering the Finance server component, you make it callable by VBScript and all of the other 
  175. OLE-compatible languages on your computer. </p>
  176.  
  177. <ol>
  178. <li>Click the <strong>Start</strong> button on the taskbar. </li>
  179. <li>Select <strong>Run</strong>.</li>
  180. <li>Type <strong>command</strong>. </li>
  181. <li>Press the <font size=2><strong>ENTER</strong></font> key. </li>
  182. <li>Type <strong>cd \winnt\system32\inetsrv\asp\cmpnts</strong> at the command prompt. (If you did not accept the default installation directory, substitute the name of your installation directory  for \winnt\system32.)</li>
  183. <li>Press the <font size=2><strong>ENTER</strong></font> key. </li>
  184. <li>Type <strong>regsvr32 Finance.dll</strong>. </li>
  185. <li>Press the <font size=2><strong>ENTER</strong></font> key. </li>
  186. <li>Click the <strong>OK</strong> button when a dialog box appears that says <strong>DllRegisterServer in finance.dll succeeded</strong>. </li>
  187. <li> Close the command prompt window.</li>
  188. </ol>
  189.  
  190. <!--
  191. <H3><A Name="finsamp">Use</a> this form to determine the future value of a savings plan.</H3> 
  192. -->
  193.  
  194. <hr>
  195. <h2> <a name="CallingtheFinancecomponentfromascript">Lesson 2: Calling the Finance Component From a Script</a></h2>
  196.  
  197. <p>To test the component, you can call the component from Active Server Pages (ASP), Visual Basic, Microsoft® Office products that use Visual Basic for 
  198. Applications, or any other OLE Automation controller.</p>
  199.  
  200. <p>To call the Finance server component from Active Server Pages by using VBScript, you can use an HTML form as input to calculate the future 
  201. value of a person’s savings plan. </p>
  202.  
  203. <h3><A NAME="TheHTMLform">The HTML form</A></h3>
  204.  
  205. <p>An HTML form will be used to gather values that describe a savings plan. These values are assigned variables that are made available to an 
  206. ASP script as part of the <strong>Request</strong> object. You can reference a value from an HTML form. For example, the annual percentage 
  207. rate entered on a form can be referenced by a script using <FONT FACE="COURIER"><strong><code>Request("APR")</code></strong></FONT>. The HTML tag <FONT FACE="COURIER"><code><INPUT TYPE=TEXT NAME=APR></code></FONT> provides 
  208. the input field necessary to enter a value. </p>
  209.  
  210. <p>To send the form to a server running Microsoft Internet Information Server and ASP, the user presses a Submit button. The 
  211. Submit button calls the page indicated by the <FONT FACE="COURIER"><code>ACTION</code></FONT> property of the HTML form tag. The HTML tag for the Submit button (<FONT FACE="COURIER"><code><INPUT 
  212. TYPE=SUBMIT VALUE=" Calculate Future Value "></code></FONT>) uses the value for <FONT FACE="COURIER"><code>ACTION</code></FONT> from the HTML form tag (<FONT FACE="COURIER"><code><FORM METHOD=POST 
  213. ACTION="Finance.asp"></code></FONT>) to call the ASP page Finance.asp.</p>
  214.  
  215. <p>We have created the form for you. Use your text editor to open the file Finance.htm in the Lessons directory (\Inetpub\Aspsamp\Tutorial\Lessons by default).</p>
  216.  
  217. <h3><A NAME="Thescript">The script</A></h3>
  218. <p>VBScript is used to call your Finance server component. The script starts by validating the inputs from the HTML form and assigning default 
  219. values for any values that were not entered on the form. The VBScript <strong>IsNumeric</strong> function is used to test whether or not a numeric (valid) 
  220. value was entered for each of the text inputs on the HTML form. </p>
  221. <p><strong>Server.CreateObject</strong> is used to create an instance of (that is, make usable) your Finance component named <FONT FACE="COURIER"><code>MS.Finance</code></FONT>. Once an instance 
  222. of a server component is created, you can make use of its methods and properties. On the script line immediately following 
  223. <strong>Server.CreateObject</strong>, the method <strong>CalcFV</strong> is used to calculate a savings plan’s future value. The result of this calculation is then sent to the 
  224. browser of the user requesting the information.</p>
  225. <p>To view the script, use a text editor to open the file Finance.asp in the Lessons directory (\Inetpub\Aspsamp\Tutorial\Lessons by default).</p>
  226. <h3><A NAME="Usingyourbrowsertorunthetest">Using your browser to run the test</A></h3>
  227. <p>To run the Finance.asp ASP page, open the  Finance.htm  form, which  then calls the Finance.asp script to calculate the future value of the savings plan specified on the HTML form. </p>
  228.  
  229. <ol>
  230. <li>Open Finance.htm by pointing  your browser to <a href="http://localhost/aspsamp/tutorial/lessons/finance.htm">http://localhost/aspsamp/tutorial/lessons/finance.htm</a>.</li>
  231. <li>Press<strong> </strong><font size=2><strong>ENTER</strong></font>.
  232. </li>
  233. <li>Optionally, enter values for the form inputs in the <strong>Savings Plan</strong> form. <br><br></li>
  234. <li>Click the <strong>Calculate Future Value</strong> button. <p>The value of your savings plan should appear. </p></li>
  235. </ol>
  236.  
  237. <p>In a relatively short time you have created a useful ActiveX server component. If you need access to other financial functions, you can implement the other financial functions built into Visual Basic as additional methods of your Finance server component. </p><hr>
  238.  
  239. <p align=center><a href="/iasdocs/aspdocs/sdklegal.htm"><em>© 1996 Microsoft Corporation. All rights reserved.</em></a></p>
  240. </FONT>
  241. </body>
  242.  
  243. </html>
  244.