- Table of Content
- VB6 and Dot Net Framework Interoperability
- Introduction.
- Prepare a web service provider
- Generate the proxy class using wsdl
- Prepare a Wrapper Class to use the proxy.
- Sample Wrapper Class OpenDocumentConsumer.cs.
- Important point about the Wrapper Class.
- Generate a mykey.snk file.
- Modification on AssemblyInfo.cs.
- Register the dll
- Create a Sample VB6 project
VB6 and Dot Net Framework Interoperability
Introduction
It illustrates with examples on how a VB6 program can call a class written by C#,
Tools used
- SharpDeveloper 1.1 (An open source free IDE)
- Dot Net framework SDK 1.1 (I experienced problem in using SDK 1.0 wsdl.exe to generate correct proxy cs files to retrieve the result from my example)
- VB6 (SP5)
Command used/ major action involved
- wsdl – to generate proxy files for consumer service
- Sn- to generate a unique key files for dll registration
- Regasm- to export the dll to type library and register the dll into the registry
- Drag and drop the respective dll into c:\windows\assembly folder
Prepare a web service provider
This step is optional if you already have one. I used Netbeans 6.5 to provide a webservice for my own demonstration
The major web serviace class is illustrated as below
package CSharp; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; /** * * @author edmund */ @WebService() public class OpenDocument{ /** * Web service operation */ @WebMethod(operationName = "add") public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) { //TODO write your implementation code here: return i+j; } @WebMethod(operationName="openDocument") public String openDocument( @WebParam(name = "docNumber") String docNumber, @WebParam(name = "docVersion") String docVersion) { String msg="Successful open document "+docNumber+" at "+new java.util.Date(); return msg; } } |
Generate the proxy class using wsdl
-
Execute wsdl http://localhost:8080/Netbeans/OpenDocumentService?wsdl to generate a proxy class
- A class file name OpenDocumentService.cs is generated.
Prepare a Wrapper Class to use the proxy
1. Create a project in SharpDeveloper, the project type should be a library
2. You may change the project properties later using [Project]à[Project Options]->[Configuration]->[Debug]->[Output]
3. Copy the proxy file into the project folder and add the proxy class OpenDocumentService.cs to the project, add namespace CSharp to it
4. Create a class name OpenDocumentConsumer, with namespace CSharp, to make use of the proxy class
Sample Wrapper Class OpenDocumentConsumer.cs
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace CSharp {
[GuidAttribute("0CCC8508-ADC1-4179-B6D6-125810A8D72")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class OpenDocumentConsumer
{
public string OpenDocument(string docNumber,string docVersion)
{
OpenDocumentService bdbs=new OpenDocumentService();
String result=bdbs.openDocumen(docNumber,docVersion);
return result;
}
}//end class
} //end namespace
Important point about the Wrapper Class
1. Insert a GUIATTRIBUTE into every public class
[GuidAttribute("0CCC8508-ADC1-4179-B6D6-125810A8D72")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class OpenDocumentConsumer
{
2. Add [ClassInterface(ClassInterfaceType.AutoDual)] to the class
[GuidAttribute("0CCC8508-ADC1-4179-B6D6-125810A8D72")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class OpenDocumentConsumer
This step is optional but in order for VB6 editor to discover the method name, you need this.
3. Make sure there is no constructor parameter in the class
4. Make sure the function you are using is a public member function(non-static function is not ok)
Generate a mykey.snk file
Use command sn –k mykey.snk to generate a a file call mykey.snk
Modification on AssemblyInfo.cs
1. If you do not have the AssemblyInfo.cs in the project directory, try to copy this and make one yourself and add it in the project.
using System.Reflection;
using System.Runtime.CompilerServices;
// Information about this assembly is defined by the following
// attributes.
//
// change them to the information which is associated with the assembly
// you compile.
[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// The assembly version has following format :
//
// Major.Minor.Build.Revision
//
// You can specify all values by your own or you can build default build and revision
// numbers with the '*' character (the default):
[assembly: AssemblyVersion("1.1.*")]
// The following attributes specify the key for the sign of your assembly. See the
// .NET Framework documentation for more information about signing.
// This is not required, if you don't want signing let these attributes like they're.
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile(@"C:\develop\CSharp\mykey.snk")]
2. note about the line in the assembly
[assembly: AssemblyKeyFile(@"C:\develop\CSharp\mykey.snk")]
You must modify the file to make use of the mykey.snk
3. Build or Rebuild the project
4. You get the following files in the bin\debug directory
Register the dll into the registry (make sure you regasm is the version you want to use, e.g. I used version 1.1. If not, type a longer path or modify your path variable to use the correct one)
- regasm CSharp.dll /tlb CSharp.tlb
- Drag and drop the CSharp.dll into the windows assembly folder
Create a Sample VB6 project
1. Choose Project, Reference, add CSharp to the reference
I personally add System.dll too just to test how to use built-in class from .net, you may ignore that.
2. Create a form and make a button
3. inside the button type the following code
Dim opendoc As CSharp.OpenDocumentConsumer Set opendoc = New CSharp.OpenDocumentConsumer Dim docNumber As String Dim docVersion As String docNumber = "doc123" docVersion = "1" result = opendoc.openDocument( docNumber, docVersion) MsgBox result |
4. Result screen.
The background graph is just another example of making use of built-in class of dot net framework from vb.
Reference materials:
留言
張貼留言