简体   繁体   中英

Calling methods in a C++ application from a C# dll

Clarification. There is a C++ exe and a C# exe. The C# exe is a wrapper for a C# dll. I need the dll to call a logger function in the C++ code (so that only one log file is produced). Currently there is a c++/CLI bridge which allows the C++ exe to call methods in the C# dll.

Apologies if this is a poor question. Its possibly a case of I just don't know what to search for / results for what I am searching for isn't of much use.

I have an application written in C++. It calls a tool written in C#. It appears the executable for the tool is just a wrapper for ac# dll. The tools purpose is to analyse and display data. The main application calls it for example to have it open a new file. The tool has never had to call anything in the C++ code before so this has always been one way. It appears to be implemented via a C++/CLI bridge. The bridge calls the functions in the tool api.

It is now required for the tool to call some methods in the C++ application. I have no idea how to go about implementing this. My c# / C++/CLI experience is somewhere between poor and non-existent. I started by attempting to clone the C++/CLI bridge and "reverse" it, but since the C# code is in effect a library calling it from the bridge is fairly simple. However, i'm not really sure (if its even possible) how to call the C++ application from a bridge.

So far the only workable solution I can think of is to have the c# code output to a file (or hopefully shared memory) then the c++ code check it periodically. This isn't close to ideal.

Any advice would be appreciated.

Thanks

Ok, i think i'll throw in a suggestion here.

Depending on what your goal is:

1) Goal: execute some of your c++ logic from c# code.

Solution: this one is fairly simple. You extract logic of interest into separate C++ project, build it as a library and then use it in both applications. As you said, there are plenty examples on how to call c++ dll from c# code.

2) Goal: froce your c++ application to execute some of it's logic from C# application.

Solution: it all comes to setting up an interprocess communication. There are quite a few approaches, that are listed here . I suggest using NamedPipes but you are free to pick w/e you are comfortable with.

Edit: Judging by your edit you probably want the second solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM