Microsoft KB Archive/271284

= How To Access the COM+ Object Constructor String in a Visual Basic Component =

Article ID: 271284

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q271284



SUMMARY
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.



MORE INFORMATION
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 &quot;prjConstruct&quot;. Rename the class module &quot;clsConstruct&quot;. Set project references to COM+ Services Library and ActiveX Data Objects 2.5 Library.  Copy and paste the following code in the class module: 'General Declarations Section Implements IObjectConstruct Dim connstr As String

Private Sub IObjectConstruct_Construct(ByVal pCtorObj As Object)

Dim mConstructString As IObjectConstructString Set mConstructString = pCtorObj connstr = mConstructString.ConstructString

End Sub

Public Function GetAuthors As ADODB.Recordset

Dim cn As ADODB.Connection Dim rs As ADODB.Recordset

Set cn = New ADODB.Connection cn.Open connstr

Set rs = New ADODB.Recordset rs.CursorLocation = adUseClient rs.Open &quot;Select * from Authors&quot;, cn, adOpenStatic, adLockBatchOptimistic Set rs.ActiveConnection = Nothing

Set GetAuthors = rs GetObjectContext.SetComplete

End Function 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.  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.</li> Click to expand the Component Services, Computers, My Computer, and COM+ Applications nodes. Click COM+ Applications.</li> Right-click COM+ Applications, point to New, and then click Application.</li> Create an empty server-side application named VBComPlusTest, and set its identity property to use the credentials of the Interactive user.</li> Complete the wizard, and click Finish. This creates the sample COM+ application that is used to host the prjConstruct.clsConstruct Visual Basic component.</li></ol> </li> To install the prjConstruct.clsConstruct component in the &quot;VBComPlusTest&quot; COM+ application, follow these steps: <ol style="list-style-type: lower-alpha;"> In the Component Services MMC snap-in, click to expand the VBComPlusTest node. Click the Components subfolder.</li> Right-click Components, point to New, and then click Component.</li> In the COM Component Install Wizard, click Next, click Install new component(s), and select prjConstruct.dll, and click Next.</li> Complete the wizard, and click Finish.</li></ol> </li> Open the Properties window for the Visual Basic component prjConstruct.clsConstruct.</li> On the Activation tab, select the Enable Object construction check box.</li>  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. &quot;Provider=SQLOLEDB;Data Source=<Your SQL Server>;Initial Catalog=pubs;&quot; & _ &quot;User Id=<User Id>;Password=<Password>&quot; </li></ol>

How to Use the COM+ Object Constructor String
<ol> Open a new Standard EXE project in Visual Basic to test the Visual Basic COM+ component. Form1 is created by default.</li> Add a list box (List1) and a command button (Command1) to Form1.</li>  Copy and paste the following code in the Click event of Command1: Dim obj As Object Dim rs As Object

Set obj = CreateObject(&quot;prjConstruct.clsConstruct&quot;) Set rs = obj.GetAuthors Do While Not rs.EOF List1.AddItem rs.fields(&quot;au_fname&quot;) rs.movenext Loop

Set obj = Nothing </li> <li>Save the Standard EXE project, and run it.</li> <li>Click Command1 to populate the list box with the first names of all the authors in the Authors table.</li></ol>

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.

Keywords: kbhowto kbmdacnosweep KB271284

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.