VB.NET Access COM
Summary
1. About COM
2. VS.NET Access COM
3. Demo
1. About COM
In the case of the use of VB6 development management procedures, consider the software expansion, maintenance, and different clients, we generally use the following so-called three-layer architecture, where COM is generally used as the intermediate layer, and a main feature of COM is it extension. The intermediate layer component is allowed to support hundreds of customers and even tens of thousands of customer capabilities:
The intermediate layer of VB is directly used by ADO access database. If the query record, generally return adoDb.recordset, when the business logic layer is passed with the UI, we can use the disconnected recordset, or Recordset GetRows ( ) Method, return array, and XML delivery data.
And because of the use of COM , we can easily change the UI layer to the ASP file in the form of the browser so that the Internet calls COM .
2. VS.NET Access COM
Some applications need to interact with your own COM components or COM components provided by Microsoft and third parties, or in the upgrade, we have to consider VS.NET access COM . To simplify the development of these applications, a set of functions are provided when the public language is run, which can hide the differences between controlled and non-controlled programming models, such as calling conventions and error processing. Visual Studio .NET writes a controlled code through these features to access the functionality implemented by the COM DLL, as if the COM interface is the same as the controlled code. This is because when you add a reference to the COM type library in Visual Studio .Net, a controlled packaging is generated for the COM interface described in the Type library. Of course, this also means that there is a loss of loss when accessing the COM function from a controlled code.
Think about it, we are now .NET's application to access the VB written COM , the data type, call mechanism, management of object survival periods, etc., who can coordinate the difference between the two? That is the RUNTIME CALLABLE Wrapper. The relationship is as follows:
The RCW acts as a "actuation of the converter", which makes COM like a true .NET object through its function mechanism, so that it can be used by the .NET application. The rear processing is very simple, and the COM we reference is acting as a NameSpace, which can be convenient to use its various operations.
How to use RCW?
There are two ways if you use VS.NET integration development environment, it is simple. As long as you add a COM object to be called in the reference to the Project, the system is automatically generated and added to the program.
Or use the TLBIMP.EXE utility to generate an assembly. The assembly can be generated from most COM type libraries. If the assembly is to be shared, you must use the issuer key to sign the assembly. Tlbimp.exe can generate a signature assembly using the / keyfile option. In fact, both use System.Runtime.InteropServices.TypelibConverter to read COM , generate RCW assemblies.
3. Demo:
System Configuration
OS: Win2k Advanced Server
Database: SQL Server
Develop Tools: VB6 & VS.NET We demonstrate an example of .NET calls COM , a getProducts function of COM access database Northwind, returns a Recordset. Then call it in the .NET, displayed in the DataGrid.
First create a simple COM :
Open VB6, add an ActiveX DLL, change the Class1's Name to getter, and the engineering name is changed to ComplusDemo. Open the getter's code window and add the code.
'Get all product names and unit prices in the products table in the Northwind library
Public Function GetProducts () As ADODB.Recordset Dim cnn As ADODB.Connection Dim cmd As ADODB.COMmand On Error GoTo ErrorHandling Set cnn = New ADODB.Connection cnn.ConnectionString = "Provider = SQLOLEDB.1; Integrated Security = SSPI;" & _ "Persist Security Info = False; Initial Catalog = Northwind; Data Source = (local)" cnn.CursorLocation = adUseClient cnn.Open Set cmd = New ADODB.COMmand cmd.ActiveConnection = cnn cmd.COMmandText = "Select ProductName, UnitPrice from Products "Set getProducts = cmd.execute exit functionerrorhandling: set getProducts = Nothingend Function
Select compilation in the file to generate complusDemo.dll.
Then add it to the CoMPONENT Services for Win2k Server.
Select Component Services in Administrative Tools, as shown:
Right-click Select New-> Application. The wizard of the COM Application Install appears. Click Next, select Create An Empty Application, click on a window that enters a new program name, enter myApp, next. Keep the default selection to end. We found that there is a MYAPP in COM Applications:
Then select myApp Right-click and select Components. NEW-> Component. The wizard of the installation component appears, select the installation of new components, positioning our compiled DLL, know the configuration end. At this point, our Win2k's COM is configured, is it very annoyed :) The back call is simple.
CoM in .NET
Create a new application, add a DataGrid and Button. Select the item to add a reference, select COM, add the DLL you just compiled to ProK, OK, and then call it.
Click on Button, join the code
DIM CMD As New OledbDataAdapter ()
DIM G As New ComplusDemo.get () DIM DST AS New Dataset ()
Cmd.Fill (DST, G.GetProducts (), "Products")
DGDProducts.DataSource = DST
Run, click Button to see DataGrid, populated the name and unit price we retrieved: Is it a call to COM in .NET? Next time I will look at how to call, use .NET from COM. (to be continued)