473,395 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Interfacing between VB6 and C#

Hi,

My C# application is supposed to cooperate with legacy VB6
code, but I am little bit worried if and how far C# strings,
double etc. in the method interfaces will work smoothly from
VB6 code.

In VB6 code below an array of string "params" is passed to
the C# method SetParameters.
VB6:
Dim params(4) As String
params(0)="home"
params(1)="sweet"
params(2)="home"
Set myClass = new MyClass()
myClass.SetParameters(params)

C#:
public MyClass
{
( ..)
public void SetParameters(string[] pars)
{
(..)
}
}

And how can I add this VB6 code to my (C#) .NET solution
project. It seems I can only add C# code (classes etc.) to
my solution.

Some help would be nice.

Thanks,
Peter

Nov 16 '05 #1
3 1339
Peter,

Yes, you're right that you can only add C# code to a C# project, since VB is
a different language. Over and above that, VB6 works off a completely
different platform entirely. C# targets the .NET framework, whilst VB6
targets COM.

Luckily, .NET has some funky stuff in it that allows it to work with COM
modules, and vice versa. In your case, you're trying to call a .NET assembly
from your VB6 program. What you will need to do is set the "Register for COM
Interop" option (found in the C# projects Properties|Configuration
Properties|Build) to True. Build the .NET DLL, and then go into your VB6
project, and add the .NET DLL as a reference. If you look in your object
browser, you should see the C# library there, along with all your public
classes, properties and methods. Now you can create the C# class in your VB6
code, just as you would any VB6 class.

Please note that there are some things you can do in .NET that COM will not
understand, for example parameterized constructors. You must provide a
parameterless public constructor to any C# class you want to be created by
your VB6 code. If the class will be created in .NET, and passed back to VB6,
this is not neccessary.

When you want to install the .NET DLL on another PC, it will have to have
the .NET Framework installed, and to register the DLL with COM you must use
the RegAsm utility instead of RegSvr32.

Regards

Sean Hederman
http://codingsanity.blogspot.com

"Peter" <An*****@work.nl> wrote in message
news:11***************@wgsvr01.wldelft.nl...
Hi,

My C# application is supposed to cooperate with legacy VB6
code, but I am little bit worried if and how far C# strings,
double etc. in the method interfaces will work smoothly from
VB6 code.

In VB6 code below an array of string "params" is passed to
the C# method SetParameters.
VB6:
Dim params(4) As String
params(0)="home"
params(1)="sweet"
params(2)="home"
Set myClass = new MyClass()
myClass.SetParameters(params)

C#:
public MyClass
{
( ..)
public void SetParameters(string[] pars)
{
(..)
}
}

And how can I add this VB6 code to my (C#) .NET solution
project. It seems I can only add C# code (classes etc.) to
my solution.

Some help would be nice.

Thanks,
Peter

Nov 16 '05 #2
You cannot add VB6 code to a .NET solution.

The safest bet would be to write COM objects in VB6 and interface to them
from .NET. The next safest after that would be to try migrate the code to
VB.NET.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Peter" <An*****@work.nl> wrote in message
news:11***************@wgsvr01.wldelft.nl...
Hi,

My C# application is supposed to cooperate with legacy VB6
code, but I am little bit worried if and how far C# strings,
double etc. in the method interfaces will work smoothly from
VB6 code.

In VB6 code below an array of string "params" is passed to
the C# method SetParameters.
VB6:
Dim params(4) As String
params(0)="home"
params(1)="sweet"
params(2)="home"
Set myClass = new MyClass()
myClass.SetParameters(params)

C#:
public MyClass
{
( ..)
public void SetParameters(string[] pars)
{
(..)
}
}

And how can I add this VB6 code to my (C#) .NET solution
project. It seems I can only add C# code (classes etc.) to
my solution.

Some help would be nice.

Thanks,
Peter

Nov 16 '05 #3
Thanks a lot for your valuable suggestions.

And what about using array of strings in C# .

BTW, will my array of strings "params" defined in VB6 passed
poperly to my C# object MyClass if I use your suggestions
???

Somewhere else I found piece of code upcasting the strings
to an object type, and cast it back to a string[]
afterwards. Is that better .... ???

Something like:
public MyClass
{
( ..)
public void SetParameters(object pars)
{
string[] params = (string[]) pars;
(..)
}
}

Some help would be nice !

Thxs,
Peter
"Sean Hederman" <us*******@doesntexist.com> wrote in message
news:cr**********@ctb-nnrp2.saix.net...
Peter,

Yes, you're right that you can only add C# code to a C# project, since VB is a different language. Over and above that, VB6 works off a completely different platform entirely. C# targets the .NET framework, whilst VB6 targets COM.

Luckily, .NET has some funky stuff in it that allows it to work with COM modules, and vice versa. In your case, you're trying to call a .NET assembly from your VB6 program. What you will need to do is set the "Register for COM Interop" option (found in the C# projects Properties|Configuration Properties|Build) to True. Build the .NET DLL, and then go into your VB6 project, and add the .NET DLL as a reference. If you look in your object browser, you should see the C# library there, along with all your public classes, properties and methods. Now you can create the C# class in your VB6 code, just as you would any VB6 class.

Please note that there are some things you can do in .NET that COM will not understand, for example parameterized constructors. You must provide a parameterless public constructor to any C# class you want to be created by your VB6 code. If the class will be created in .NET, and passed back to VB6, this is not neccessary.

When you want to install the .NET DLL on another PC, it will have to have the .NET Framework installed, and to register the DLL with COM you must use the RegAsm utility instead of RegSvr32.

Regards

Sean Hederman
http://codingsanity.blogspot.com

"Peter" <An*****@work.nl> wrote in message
news:11***************@wgsvr01.wldelft.nl...
Hi,

My C# application is supposed to cooperate with legacy VB6 code, but I am little bit worried if and how far C# strings, double etc. in the method interfaces will work smoothly from VB6 code.

In VB6 code below an array of string "params" is passed to the C# method SetParameters.
VB6:
Dim params(4) As String
params(0)="home"
params(1)="sweet"
params(2)="home"
Set myClass = new MyClass()
myClass.SetParameters(params)

C#:
public MyClass
{
( ..)
public void SetParameters(string[] pars)
{
(..)
}
}

And how can I add this VB6 code to my (C#) .NET solution
project. It seems I can only add C# code (classes etc.) to my solution.

Some help would be nice.

Thanks,
Peter



Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: 2mc | last post by:
All, I'm getting a better handle on Python and NumPy. I've found solutions to many of my questions. Thanks to all. I would like to interface Python (in WinXP) with another program through...
4
by: Chuck Amadi | last post by:
Hi all Anyone know a good Pygresql Tutorial for Interfacing between Python & Postgresql . Cheers Chuck
12
by: David Walker | last post by:
Hi I have a program which I need to interface with a webpage - the webpage will accept an input (probably a 'post' string from the program) and then will process it and needs to return a value. ...
4
by: Shaurya Anand | last post by:
I would like to know more about Interfacing hardware devices through .NET. We're students and what we're trying to do is control our robots with printer or serial port interfacing... very similar...
3
by: Kevin | last post by:
Hi All I got some good responses from some of you guys regarding me serial port interfacing problem that I posted a little while back. However your help leaded me to this link belo ...
2
by: headware | last post by:
I'm relatively new to ASP.NET and ADO.NET, but I have a basic design question regarding the use of web services and APS.NET applications. Right now we have an application that uses web services to...
1
by: Neil | last post by:
Our hosting company's server went down a few days ago. After they fixed it, our PHP Page and Database are not interfacing. They are not believing me that I have done nothing on my end in several...
1
by: keliie | last post by:
Hello, I've recently completed a database that tracks my food purchases and inventory and I want to add another piece to the database that tracks sales. Our company's waitstaff enters order...
2
by: abhay | last post by:
hi,i m interfacing gsm modem to my microcontroller.i need to send sms through it. i am using AT commands for that.the command to send sms (AT+ CMGS) terminates with ctrl-Z.now in my program i hav...
6
by: dev | last post by:
hi i want to know about interfacing or accessing a serial port using c-program please help me with an example thanks for whom who reply me soon
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.