By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

Interfacing between VB6 and C#

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.