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