跳到主要內容

VB6 Interoperability with .NET 1.1 without Visual Studio (Using Free IDE SharpDeveloper)

  • 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

  1. Execute wsdl http://localhost:8080/Netbeans/OpenDocumentService?wsdl to generate a proxy class
  2. 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]

image

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

{

image

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

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)

  1. regasm CSharp.dll /tlb CSharp.tlb
  2. Drag and drop the CSharp.dll into the windows assembly folder

image

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.

image

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.

image

Reference materials:

  1. http://www.vbrad.com/article.aspx?id=46
  2. http://msdn.microsoft.com/en-us/library/ms364069(vs.80).aspx
  3. http://msdn.microsoft.com/en-us/library/aa719105.aspx
  4. http://msdn.microsoft.com/en-gb/library/hfzzah2c(VS.71).aspx

留言

這個網誌中的熱門文章

Setup Tomcat HTTPS (with JDK 8 to Java 15) in 2 mins

  Setup Tomcat 9 HTTPS/SSL To have a quick view, you may see the video(s): https://www.youtube.com/watch?v=WDGoF13vhZU 1. Generate Keystore I am using JDK 15 to generate the keystore. But the steps are similar with Tomcat 6 + openjdk 8(as I have tried it before writing this doc) Use “keytool” command to create a self-signed certificate. During the keystore creation process, you need to assign a password and fill in the certificate’s details. D:\apache-tomcat-9.0.38\conf>keytool -genkey -alias tomcatks -keyalg RSA -keystore D:\apache-tomcat-9.0.38\conf\tomcatks When enter the passwords during generation, please make sure the two passwords you entered are the SAME. This is the requirement of Tomcat. Here is the abstract from Tomcat installation  Finally, you will be prompted for the key password , which is the password specifically for this Certificate (as opposed to any other Certificates stored in the same keystore file). You MUST use the same password here as was used f...

IIS connects to Tomcat. Bug in IIS URL redirect, http2

IIS connects to Web/Application Server Tomcat 9 IIS (Internet Information Server 10) on Windows 2016 server Plesk dropped support for connecting Tomcat from IIS. It is disgraceful. I was so disappointed to learn that. However, another exciting opportunity opened up as I learnt URL rewrite is powerful enough to take over the job and even with more elegant simplicity. So I gave it a try. Even without knowing many of its features, I was able to set it up that one of the sites mysub.yourdomain.com (not real domain, just as an example and I tried a few sites in my server) is pointing to a tomcat localhost at 8080. It sounds good enough. Right?   However, after setting the original site mysub.yourdomain.com for using https. All browsers in desktop platform and Android platform seemed to work. But browser in iOS version failed to open the https. After a few digging, some said in forum that it was a known bug https://stackoverflow.com/questions/49141004/ios-10-3-3-not-working-w...

Find directories with specific size with Java. Recursive Function Demonstration.

The first function draft was generated by AI ChatGPT 3.5. But its comparative function was wrong and AI needed extra guidance to pinpoint that comparison of object in Java needs extra cares. Then its revised its recursive function. However the recursive function provided was still incorrect as it failed to count all the size of files and files inside the subfolders of a specific folder. Then I determined to finish it myself and here is my version. The DirectorySizeChecker is a Java program that allows users to check the sizes of directories in their file system. The program takes in a directory path and an optional minimum size in megabytes as command line arguments, and outputs the paths and sizes of all directories that are equal to or larger than the specified size. The program starts by setting a default minimum size of 100MB if no size argument is provided. It then checks if the correct number of arguments have been provided, and if the input directory exists and is indeed a direc...