Private oContext As ObjectContext Private oResponse As Response Implements ObjectControl Private Sub ObjectControl_Activate() Set oContext = GetObjectContext() Set oResponse = oContext("Response") End Sub Private Sub ObjectControl_Deactivate() Set oContext = Nothing Set oResponse = Nothing End Sub Private Function ObjectControl_CanBePooled() As Boolean ObjectControl_CanBePooled = False End Function
Public Sub DisplayGreeting() oResponse.Write "Hello, out there!<BR>" & VBCrLf End Sub
The VBCrLf constant simply adds the carriage-return and line-feed characters to the end of the line, and is purely an aesthetic addition to the code. Step 4: That should comprise the whole of the code for the component. For the next step, we’ll compile it into a DLL that can subsequently be called from a server-side script. Before doing this, though, you’ll probably want to give some meaningful names to your project and its sole class. To alter the default name for the project from "Project1" to something more agreeable, click on the project name in the Project Explorer window, which lies to the right of the workspace. In the Properties window below it, there should be only one property listed, which is (Name). Alter this to suit your tastes, and then click on the listing for Class, again in the Project Explorer window. The (Name) property for the class will be the first of several properties listed in the Properties window. For our example, we’ve chosen the rather predictable names of "Example" and "Greeting" for our project and class. After entering the code as directed, and renaming everything, your Visual Basic editor window should look something like the following (but note that we have removed the toolbox from the left of the window so that more of the code is visible.) Now save your project and then, on the File menu, select Make Example.dll, where "Example" is the name that you’ve given to the project. The default location for the DLL is the folder in which you saved your project, and this will do as well as any. Once you’ve chosen a location for the DLL, your code will be compiled and a listing for the resulting DLL will be added to the system registry. Step 5: Having created the DLL we must now register it with the Microsoft Transaction Server. Components that are registered with MTS are organized into packages which typically contain all the components for a particular application. If you are using Windows NT 4.0, to register the DLL with MTS, we must first create a new package. To do this, open the Transaction Server Explorer by clicking the Windows NT Start button, selecting Programs, then Windows NT 4.0 Option Pack, followed by Microsoft Transaction Server and finally Transaction Server Explorer. When the MTS explorer window appears, expand the Microsoft Transaction Server folder in the left pane, then expand Computers, followed by My Computer. Now select (by left-clicking on) the Packages Installed folder. Next, right-click on Packages Installed and select New followed by Package. If you are using Windows 2000, to register the DLL with MTS, you must click the Start button, when the window pops up select Settings, followed by Control Panel, then Administrative Tools, and finally Component Services. Expand the My Computer directory in the left pane and select Com+ Applications then right click and select New followed by Application. Click the Create an Empty Package button from the pop-up window that appears, enter the name of your package (we’ve called the package Example), click Next, if you are Using Windows 2000 click Next once more, and then Finish. A new icon representing your example package will then appear in the right pane of the explorer. Having created the requisite MTS package, we must now add our newly created DLL to this package. To achieve this, expand your package in the MTS explorer, and then select (by left-clicking) the Components folder. Next, right-click on Components, select New, and then Component. Click the Import component(s) that are already registered button. After a short while a pop-up window will appear containing a list of all the components registered on your computer. Select Example.Greeting (or whatever you chose to call your component) and click the Finish button. An icon representing your component will appear in the previously blank right pane of the MTS explorer. This completes the registration of your component, and you can now begin testing. Step 6: Having built and registered our new component, we can now begin testing. To test your new ActiveX Server Component, create an ASP page now (using Visual Interdev or the editor of your choice) and paste in the following code:
<% Set oExample = Server.CreateObject("Example.Greeting") oExample.DisplayGreeting %>
The CreateObject method of the Server object creates an instance of the component specified by its single String argument. In the above example, we create an instance of our Example.Greeting component. Now save your ASP page in a folder that is, or can be made, accessible to IIS. If you are not sure where to save your ASP page, we suggest making a folder C:\inetpub\wwwroot\test and storing it there. Before viewing your test ASP page in a browser, it is important that you configure the page to run in a separate process from the main Internet Information Server (IIS) service. If you do not do this, you may have to shut down the Web server each time you wish to rebuild your DLL. This is because the web server process will attach itself to your DLL and prevent the file from being overwritten when you attempt to recompile your Visual Basic project. By configuring IIS to run your page in a separate process however, we will only have to shut down that one process in order to free up the DLL and enable the project to be rebuilt. To make sure that your page is set to run out-of-process, start up the Microsoft Management Console (MMC) for IIS. This should be accessible, if you are using Window NT 4.0, by clicking the Start button, selecting Programs, then Windows NT 4.0 Option Pack, followed by Microsoft Personal Web Server (for NT workstation) or Microsoft Internet Information Server (for NT Server) and finally selecting Internet Service Manager. If you are Using Windows 2000, this is accessible by clicking the Start button, selecting Settings, followed by Control Panel, then Administrative Tools and finally Internet Service Manager. You will notice that you can configure both the Internet Information Server and the Transaction Server from this interface. Having opened MMC, locate your ASP page’s directory in the Internet Information Server tree view in the left pane, and then right-click on the directory name. On the menu that appears, select Properties. In order for your test ASP page to run out-of-process from IIS, the directory in which it’s located must be an application starting point. To determine whether or not this is the case, look at the tree view in MMC. Icons for the directories which are application starting points are depicted as open boxes, whereas those directories which are not application starting points are represented by the standard folder icon. If the page’s directory is not an application starting point, click the Create button on the property page. You can give the new application a name after creating it, although this is not actually necessary. Once you’ve ensured that the directory is capable of running the page out-of-process, make sure that the check-box to the left side of Run in separate memory space is checked (in the above screenshot this checkbox is disabled as we have not yet created the Application starting point.) Finally, click Apply, followed by OK, to close the property page. Now, you may finally view your test ASP page containing the script to call your object’s DisplayGreeting subroutine. When you do this, you should see the string "Hello, out there!" displayed in the browser. Step 7: Having our simple "Hello World!" example working, you can now modify it and extend it to perform the particular tasks that you are interested in. Although our example component contains only a single subroutine, you can extend it to implement as many functions and subroutines as you desire. Each function or subroutine that is added to your Visual Basic project and declared as Public will be accessible from an ASP page, just as the DisplayGreeting subroutine was. And of course, the procedures that you add can do far more than simply send a string to the browser. Having made changes to the code in your Visual Basic project, you must detach the existing DLL from any active processes before you attempt to rebuild it. If you do not do this, you will receive a "Permission Denied" error when attempting to remake the DLL. To detach your DLL from IIS, open the Internet Service Manager and view the Properties of the directory containing the ASP page that uses your component. Click the Unload button to detach your DLL from the IIS process. Then click Apply followed by OK to close the properties window. You will also need to detach your DLL from MTS. To do this, right-click on your package in the Transaction Server Explorer, and select Shut Down. If you have your ASP page open in an editor such as Microsoft Visual Interdev, this application may also be attached to your DLL. To detach the DLL, exit the application. Having released all active references to your DLL you may now rebuild your component by selecting Make Example.dll (or whatever you DLL may be called) from the File menu in the Visual Basic editor. If, after recompiling your DLL, your test page fails to load correctly, you may also need to refresh MTS so that it knows that the component has been updated. This can be achieved from the MTS explorer by right-clicking on My Computer and selecting the Refresh All Components option. Alternatively, you can run mtxrereg from any command prompt, or by selecting Run from the Windows NT Start menu, entering mtxrereg and then clicking the OK button. With this in mind, you should now have all the tools necessary to begin building your very own ASP/MTS Server components. Step 8: Have fun. Go play! For help with debugging your ActiveX component, read our companion tutorial, Debugging ActiveX Components in Visual Basic.