In Microsoft Windows 2000, you can configure a unique object constructor string for each dynamic-link library (DLL) component that is installed in the COM+ run time. This constructor string is commonly used to specify an initialization string that must be accessible to all object instances of the component. This article includes a code sample that demonstrates how you can configure the COM+ object constructor string for a Visual Basic component that the component's object instances can use.
The constructor string is commonly used to specify a Database connection string that the object instances of a component use to establish database connections. An advantage of using the constructor string for this purpose is that it prevents the connection string from being hardcoded in the component's code. To change the name of the database server, point to a different database, or implement other modifications, you only have to modify the component's COM+ object constructor; you no longer have to change the component's code and recompile the actual DLL. Besides storing database connection information, you can also use the COM+ object constructor string to specify any initialization string that needs to be accessible to all object instances of the component for which it is configured.
How to Configure the COM+ Object Constructor String
The Visual Basic component should implement the IObjectConstruct
COM+ interface to enable its object instances to access the COM+ object constructor string that has been configured for it. IObjectConstruct
exposes a single method named Construct (IObjectConstruct_Construct
) that fires when an object instance of the component is created. You can use the Constructor object that is passed as a parameter to this method to obtain the configured COM+ object constructor string. To implement a sample that demonstrates how you can this functionality, perform the following steps:
- Open a new ActiveX DLL project in Visual Basic.
- Rename the project "prjConstruct".
- Rename the class module "clsConstruct".
- Set project references to COM+ Services Library and ActiveX Data Objects 2.5 Library.
- Copy and paste the following code in the class module:
This code sample demonstrates how to implement IObjectConstruct_Construct to access the COM+ object constructor string. The Construct object parameter must be cast to an IObjectConstructString interface. IObjectConstructString exposes a single property called ConstructString that can be used to access the COM+ object constructor string. Notice how the constructor string is assigned to a global variable and reused in the GetAuthors function.
'General Declarations Section
Dim connstr As String
Private Sub IObjectConstruct_Construct(ByVal pCtorObj As Object)
Dim mConstructString As IObjectConstructString
Set mConstructString = pCtorObj
connstr = mConstructString.ConstructString
Public Function GetAuthors() As ADODB.Recordset
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open "Select * from Authors", cn, adOpenStatic, adLockBatchOptimistic
Set rs.ActiveConnection = Nothing
Set GetAuthors = rs
- Compile the DLL. To create the COM+ Server application to host the prjConstruct.clsConstruct component, follow these steps:
- In Control Panel, point to Administrative Tools, and then click Component Services to open Component Services Microsoft Management Console (MMC) snap-in.
- Click to expand the Component Services, Computers, My Computer, and COM+ Applications nodes. Click COM+ Applications.
- Right-click COM+ Applications, point to New, and then click Application.
- Create an empty server-side application named VBComPlusTest, and set its identity property to use the credentials of the Interactive user.
- Complete the wizard, and click Finish. This creates the sample COM+ application that is used to host the prjConstruct.clsConstruct Visual Basic component.
- To install the prjConstruct.clsConstruct component in the "VBComPlusTest" COM+ application , follow these steps:
- In the Component Services MMC snap-in, click to expand the VBComPlusTest node. Click the Components subfolder.
- Right-click Components, point to New, and then click Component.
- In the COM Component Install Wizard, click Next, click Install new component(s), and select prjConstruct.dll, and click Next.
- Complete the wizard, and click Finish.
- Open the Properties window for the Visual Basic component prjConstruct.clsConstruct.
- On the Activation tab, select the Enable Object construction check box.
- In the Constructor String box, specify the following ADO connection string, and then click OK:
NOTE: This sample uses the Microsoft SQL Server PUBS sample database.
"Provider=SQLOLEDB;Data Source=<Your SQL Server>;Initial Catalog=pubs;" & _
"User Id=<User Id>;Password=<Password>"
How to Use the COM+ Object Constructor String
- Open a new Standard EXE project in Visual Basic to test the Visual Basic COM+ component. Form1 is created by default.
- Add a list box (List1) and a command button (Command1) to Form1.
- Copy and paste the following code in the Click event of Command1:
Dim obj As Object
Dim rs As Object
Set obj = CreateObject("prjConstruct.clsConstruct")
Set rs = obj.GetAuthors()
Do While Not rs.EOF
Set obj = Nothing
- Save the Standard EXE project, and run it.
- Click Command1 to populate the list box with the first names of all the authors in the Authors table.
To demonstrate the usefulness of the COM+ object constructor string, change the DataSource
property in the constructor string to point to a different SQL Server that also has the PUBS sample database. When you run the client code, the records from the new data source are retrieved and displayed. To confirm this, add a new record to the Authors table in the second SQL Server PUBS database.
Article ID: 271284 - Last Review: June 29, 2004 - Revision: 1.2
- Microsoft Visual Basic 5.0 Professional Edition
- Microsoft Visual Basic 6.0 Professional Edition
- Microsoft Visual Basic 5.0 Enterprise Edition
- Microsoft Visual Basic Enterprise Edition for Windows 6.0
|kbhowto kbmdacnosweep KB271284|