跳到主要內容

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

留言

這個網誌中的熱門文章

Files sharing problem between Windows 2003 server host and virtual machine

Regarding VMServer 1.08, when I copy files between a Windows 2003 host and a vm(no matter it is Windows XP or Windows 2003 Server), the following error prompted out arbitrarily, usually for copying large files. First I thought I had solved it by disabling the Jumbo frame and any kind of flow control in network card advance setting. It corrected the problem for most of the cases until I came across a situation to copy a 600MB file, the captioned error "network name is no longer available " prompted out after 10 to 20 seconds. It was not easy to find the solution as there are many cases, with different reasons, having the same symptom, thus I jot it down for a record. 1. Disable Jumbo frame, or any kind of flow control (try disabling Jumbo frame first) 2. Disable Denial of service attack in Windows 2003. Microsoft had acknowledged it has problem by itself in some cases. VMWare Server and "...network name is no longer available..." Did you ever receive following erro...

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

全港乒乓球錦標賽 09 甲組賽事

今天換個輕鬆一點的題目,看看精彩的乒乓球 我在這裏介紹有個Channel有很清晰的乒乓球比賽片段,是全港乒乓球錦標賽甲組的賽事,其中有高澤禮在香港比賽的最新的片段 ,我相信這種場面很少有機會見到,因為,第一是業餘對專業,我在電視中都未看過,是認真比賽的那一種,不是國家隊奧運完結之後派冠軍來的表演賽,大家放軟手腳逗下逗下的那一種,可觀性高很多,第二,拍攝很有現場感,就算電視台都很少見到這樣的角度,就像親臨現場一樣。 雖然高禮澤貴為國際頂級球員,但落到這場地區賽事的時候都非常認真;雖然他技術高幾班,但認真程度從有球滴死對手時,球証看不清楚,而他正領先下,他都幫忙指証便可看出。 他是港隊中又有善又認真的專業球員,希望給年輕球員有認真對陣高水準球員的機會。 http://www.youtube.com/watch?v=nSo6Kg0vEus (這幾局可能是平均分數最接近的比賽) http://www.youtube.com/watch?v=LTJuYI906AI 還有很多其他攝錄質素很高的片段 謝嘉俊(港隊代表) vs 趙頌熙(港隊少年代表,在港有乒乓神童之稱,曾在11歲時贏得中國全國兒童賽冠軍) http://www.youtube.com/watch?v=zNEG2CzrDMI 謝嘉俊(港隊代表) vs 王德龍 09全港公開乒乓球單項錦標賽 Part 1 http://www.youtube.com/watch?v=OIeSo9V5V-0 趙頌熙(青少年港隊代表) 對 陳嘉耀 09全港公開乒乓球單項錦標賽 決勝局 Part 1 http://www.youtube.com/watch?v=jpaXjLL1bT4 陳文鋒 對 蔡鎮滔 2009全港公開乒乓球單項錦標賽 Part 2 http://www.youtube.com/watch?v=ZdcG8-I0buw&feature=channel_page 或者可以直接溜灠這個channel頻道 http://www.youtube.com/ppball