views:

256

answers:

4

I am currently learning VC++. I have created an application that has functionality to block/allow IP addresses and I would like to expose this functionality to a C# application.

I have defined the following members in my header file, which reference methods in my .cpp file (bare with me if that's obvious, as this is my second day of C++) which need to be accessible outside my application.

public:

// Constructor.
ZizFilter();

// Destructor.
~ZizFilter();

BOOL StartFirewall();
BOOL StopFirewall();

BOOL AddIPAddressToBlockedList(char* IP)
BOOL RemoveIPAddressFromBlockedList(char* IP)

BOOL BlockAll(char* tunnelAddress);
BOOL UnblockAll();

I understand the C# Interop side and how to consume exposed assembly methods, but I don't know how to get my C++ application to publically expose the methods.

A: 

You need to put your methods in a dll (Dynamic Link Library). If you are using Visual Studio you should be able to just follow the win32 dll new project wizard.

Dolphin
+1  A: 

Ahem... Let's clarify some things:

  1. C# Interop allows you to use functions exposed by classic Win32 DLLs as if they were .NET methods. Those DLLs are usually written in C.

  2. You can write Win32 DLLs in C++ as well. It's just the same as writing a Win32 DLL in C, with the only difference that...

  3. C++ compilers mangle function names, unlike C. Name mangling incorporates information about the function parameter and return types into a function name, that's what makes function overloading possible. Unfortunately, that means your beautiful function names such as BlockAll (and perhaps the not so beautiful ones like AddIPAddressToBlockedList) get converted to ugly (or uglier) things. You don't want to use a function whose name is ?UnblockAll@@YAXXZ, do you?

  4. Name mangling is compiler-dependent. Thus, writing Win32 DLLs in C++ is not a good idea unless you are going to use it only from an executable (or another DLL) compiled with the same version of the same compiler (in this case, Microsoft's C++ compiler).

If you want to make your life simple, use C and not C++ to export functions to your .NET application. C++ has an awesome feature, extern "C" that tells the compiler to treat your code as C code. That allows you to write C wrappers around your C++ functions.

EDIT:

Another possibility that I forgot completely is to use C++ Interop (yeah, you can use C++ to write a .NET wrapper around your Win32 DLL) and only then use C# to call your .NET wrapper. Beware you should, though, since C++/CLI (C++'s extension to allow for .NET-specific semantics) is very contrived.

Eduardo León
+2  A: 

One way to handle this is to write a wrapper in C++ CLI - this is the version of c++ that is extended to do managed .net things.

You can create a managed or "ref" class in C++ cli that will appear as a normal .net class to c#.

Within the c++ cli class you can call your pure c++ class as normal.

morechilli
This can be an extremely effective technique that carries with it the advantage of never having to let COM into the picture. I used C++/CLI *quite* effectively as a layer between a legacy MFC UI control and a new C# WinForms front end. I wrapped the MFC control in C++/CLI as a managed Control so that it looked like any other managed object to the C# code.
Greg D
Worked like a charm. Thanks!
George
A: 

Another option is to use Swig to generate c# code to call your c++ functions. This assumes your c++ functions are compiled into a DLL, and that you don't mind having a lot of extra wrapper code. The advantage with this tool is you can call the same c++ code from multiple languages with out having to write code specific to each language. The bad is that it's another external tool in your build process with it's own input and output that you have to keep track of.

Jared