简体   繁体   中英

How to p/Invoke a c++ executable into C#

I have a C++ application which is g++ compiled executable from a .cpp file. Now I want to call the methods in this .cpp file or/executable into my C# application.

I was looking into different ways to be able to do it and I came across P/Invoke. However, it sounds like the starting point for a C++ application to be p/Invoked is that it needs to be a DLL. I do not have a DLL and rather have this executable.

What can I do so that this code is reference-able in my C# P/Invoke layer?

However, it sounds like the starting point for a C++ application to be p/Invoked is that it needs to be a DLL. I do not have a DLL and rather have this executable.

For p/invoke you need to compile the functionality that you need into a DLL. You cannot load an executable file into another process.

You could run the executable in another process and communicate with it via some form of IPC. One such option would be an out of process COM server.

However I suspect that the easiest route is to compile the code as a DLL.

DLLs and EXEs are both Windows PE files, so either one will work just fine. You just need to make sure the functions you want to call are properly exported.

You'll need to create C wrapper functions for everything you want to export though. Add extern "C" __declspec(dllexport) to their prototypes, and use them to call your desired C++ code.

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