473,734 Members | 2,211 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)="swee t"
params(2)="home "
Set myClass = new MyClass()
myClass.SetPara meters(params)

C#:
public MyClass
{
( ..)
public void SetParameters(s tring[] 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 1357
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|Conf iguration
Properties|Buil d) 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.n l> 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)="swee t"
params(2)="home "
Set myClass = new MyClass()
myClass.SetPara meters(params)

C#:
public MyClass
{
( ..)
public void SetParameters(s tring[] 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.n l> 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)="swee t"
params(2)="home "
Set myClass = new MyClass()
myClass.SetPara meters(params)

C#:
public MyClass
{
( ..)
public void SetParameters(s tring[] 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(o bject pars)
{
string[] params = (string[]) pars;
(..)
}
}

Some help would be nice !

Thxs,
Peter
"Sean Hederman" <us*******@does ntexist.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|Conf iguration Properties|Buil d) 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.n l> 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)="swee t"
params(2)="home "
Set myClass = new MyClass()
myClass.SetPara meters(params)

C#:
public MyClass
{
( ..)
public void SetParameters(s tring[] 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
1327
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 the COM object. This other program downloads data for me on a daily basis. I have a choice of many methods to export the data from this program - the fastest of which is through the COM object. One of the methods of this other program is a...
4
5811
by: Chuck Amadi | last post by:
Hi all Anyone know a good Pygresql Tutorial for Interfacing between Python & Postgresql . Cheers Chuck
12
2707
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. Is there an easy way to interface the program to do this? I have two questions: How can I call a webpage as simply as possible in c / c++ (preferably not using MFC etc) and pass a 'post' string to the page? How can I retrieve an array of values...
4
1869
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 to C++ classic style. Please help us as it could do great wonders for our project.
3
1937
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 http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=9dde5cf3-4842-4d4b-baa7-73e09c4d589 Now my questions is - forgive me because I am still new at c# - that how do I use these clases? I think I need to build my own classes that inherit from...
2
2212
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 access the database layer. However, the code works in a pretty cumbersome and ungeneric way. Basically every query, update, and insert has its own function. So you see a lot of functions like webService.InsertCustomer(name, age, phone); or...
1
1264
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 months with uploading new files or doing anything with the database, insisting I have to fix this myself. I am somewhat adept at html, but had a friend get me started on the database -- then I took over, entering new data, revising fields, but know...
1
2506
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 information into Positouch terminals; I have a Positouch "server" which generates management reports and the like. I've been exporting the Positouch data to Excel then importing into Access (as a short term solution). My question is - does anyone...
2
15933
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 tried all ways to send ctrl-Z at end of my sms.but it takes this as a character or string only.also i tried with ascii value of ctrl-Z but of no use. i am able to see the transmitted commands on hyper-terminal through
6
2396
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
8774
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.