Can VB.Net talk to C/C++ code?

T

Terry Cai

I am trying to write an application by using th VB.Net with user forms
and try to let it talk to business logic layer previously written i
C/C++ (mainly C++).

Could you tell me what I should do so that my VB.Net applcation ca
talk to C/C++ code?

Thanks so much for your time.

Terry Cai
(e-mail address removed)
 
R

Richard Grimes

Terry said:
I am trying to write an application by using th VB.Net with user
forms, and try to let it talk to business logic layer previously
written in C/C++ (mainly C++).

Could you tell me what I should do so that my VB.Net applcation can
talk to C/C++ code?

The question is can your .NET code talk to unmanaged code (it is
irellevant what .NET language you use) and the answer is yes. However,
you have to do some work.

You have not said anything about the C++ code, so what work you do
depends on how code is 'exported' from the C++ code:

1) Is it a COM server, if so, you use COM interop. If the COM server has
a type library then you simply run tlbimp and that will generate a .NET
assembly which will do the work for you. If you do not have a type
library then you either have to create the interop classes yourself
(that is too much for me to describe here) or create a Managed C++
wrapper class (see 5).

2) Is it a DLL with exported C functions? If so, then you can use
platform invoke with <DLLImport>, once you have used that attribute, you
can call the method as if it is a .NET method.

3) Is it a DLL with exported C++ functions (eg MFC). If that is the case
then you typically will not use <DLLImport> (you could, but it involves
a lot more work), and instead, you should create a Managed C++ wrapper
class (see 5).

4) Is it a template library? If so, then you cannot use it directly by
VB.NET not C#, but you can use it with managed C++ (see next)

5) Is it a static library (.lib file)? If so, then you must access it
with Managed C++. You should write a wrapper managed C++ class that
calls the unmanaged code and gives access to the functionality through
public members. Since it is managed C++ it means that its public types
(as long as they are CLS compliant) can be accessed by VB.NET.

Richard
 
T

Terry Cai

Thanks so much for Richard's valuable information!

I will take a look at the C++ code to see what I should do based o
your tips.

Thanks again!

Terry Cai
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top