This article describes how to use ATL COM Components in ASP.Net web service and how to consume the ASP.Net Web Service from Visual Basic 6 Client. Ultimately the VB6 client calls the method of the COM component created in Visual C++ through the ASP.Net Web Service.
ATL COM Component
Let us create a simple component using ATL COM AppWizard. Here the Project, Class and method names are Sum, CAdd and AddNumbers respectively. AddNumbers method takes two integers as input parameters and returns the sum of the inputs.
Then build the Sum.dll. Now we have a standard simple COM Component.
ASP.Net Web Service
First create one ASP.Net webservice named ATL_WebService.
Now we have to use the Sum.dll inside the ASP.Net application. The basic problem lies in the fact that ASP.NET is based on the Common Language Runtime, a so-called Managed Execution Environment. The code itself then is called Managed Code and the Runtime obtains the information for management of the code out of the Metadata which was generated by the compiler for the description of the source code. The problem now is that existing components do not have Metadata information. Therefore, we have to generate the Metadata for them.
Creating the Metadata DLL
The Type Library Importer (Tlbimp.exe) converts the type definitions found within a COM type library into equivalent definitions in a common language runtime assembly. The output of Tlbimp.exe is a binary file (an assembly) that contains runtime metadata for the types defined within the original type library.
Hence first copy the Sum.dll to Bin directory of ATL_WebService. Then type the following in command line.
Tlbimp Sum.dll /out: Sum_proxy.dll
With this simple command, the Metadata information is saved to the file Sum_proxy.dll (this DLL is an Assembly). Now is our Proxy for the actual 'unmanaged' COM object. The output of the above command is as given below.
Using the Intermediate Language Disassembler (Ildasm.exe), you can explore the Sum_proxy.dll. AddClass has one method named AddNumbers.
Use of the component in ASP.NET
From the project menu select add reference menu and select the sum_proxy.dll created in bin folder. Then create the Webmethod as shown in the following figure.
Now run the program from IDE. You will get one window as given below
Now we have created one ASP.Net webservice to add two numbers, which eventually calls the ATL COM component. Cool !!!
Build WebService Client With VB6 and SOAP
You need to install the Microsoft SOAP Toolkit 2.0 or later on your development machine before you can build the VB6 client. Once you install the toolkit, building the client is surprisingly simple. Create a new project in VB6, and choose the Standard EXE template. Name the project WebServiceTest, and change the main form's name to frmWebServiceTest. Add a project reference (Project | References) to the Microsoft SOAP Type Library, giving you access to the SOAP objects.
Add two textboxes (named txtFirst and txtSecond) , a button (named cmdFindSum) and a label (named lblResult) to the form. Double-click on the cmdFindSum, and add following code:
Now run the application.
Simply click on the button. Wah!! Dot net made everything simple.