How to open a C ++ method for my C # application?

I am currently studying VC ++. I created an application that has functionality for blocking / resolving IP addresses, and I would like to introduce this functionality to a C # application.

In my header file, I defined the following elements that reference the methods in my .cpp file that should be available outside of 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 # side of interaction and how to use public assembly methods, but I don’t know how to get my C ++ application to publicly present methods.

+2
source share
4 answers

One way to handle this is to write a shell in the C ++ CLI - this is a version of C ++ that expands to run managed .net events.

You can create a managed or ref class in C ++ cli that will look like a regular .net class for C #.

In the C ++ cli class, you can call your pure C ++ class normal.

+5
source

Ahem ... Let me clarify some things:

  • C # Interop allows you to use functions discovered by the classic Win32 libraries as if they were .NET methods. These DLLs are usually written in C.

  • You can also write Win32 libraries in C ++. This is exactly the same as writing a Win32 DLL in C, with the only difference being ...

  • C ++ compilers mangle function names, as opposed to C. The mangling name includes information about the function parameter and returns types in the function name, which makes function overloading possible. Unfortunately, this means that your pretty function names, such as BlockAll (and maybe not as pretty as AddIPAddressToBlockedList ), are converted to ugly (or ugly) things. You do not want to use a function called ?UnblockAll@ @YAXXZ , you?

  • The name mangling is compiler dependent. Thus, writing Win32 DLL files in C ++ is not a good idea if you are not going to use it only from an executable file (or another DLL) compiled with the same version of the same compiler (in this case, the compiler Microsoft C ++).

If you want to make your life simple, use C rather than C ++ to export functions to your .NET application. C ++ has a wonderful function, extern "C" , which tells the compiler that it treats your code as C code using a C link (no binding). This allows you to write C wrappers around your C ++ functions.

EDIT:

Another possibility that I completely forgot is to use C ++ Interop (yes, you can use C ++ to write a .NET shell around your Win32 DLL), and only then use C # to call your .NET shell. Beware, you should, however, because C ++ / CLI (a C ++ extension that allows the use of .NET-specific semantics) is very far-fetched.

+2
source

You need to put your methods in the dll (Dynamic Link Library). If you are using Visual Studio, you can simply follow the new win32 dll project project.

0
source

Another option is to use Swig to generate C # code to call your C ++ functions. This assumes that your C ++ functions are compiled into a DLL and that you do not mind having a lot of extra shell code. The advantage of this tool is that you can call the same C ++ code from multiple languages ​​without having to write code specific to each language. The bad news is that this is another external tool in your build process with its own inputs and outputs that you should keep track of.

0
source

Source: https://habr.com/ru/post/1299677/


All Articles