开发者

How do I expose a C++ method to my C# application?

开发者 https://www.devze.com 2022-12-18 14:13 出处:网络
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 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 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 publicly expose the methods.


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.


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 use C linkage (no mangling). 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消