Calling exe C ++ functions from C #

I am trying to control a running application written in C ++ using another C # application.

In my C ++ code, I defined an API:

_declspec(dllexport) //is this even possible when compiling an .exe? int getSomething(); 

Is there any way to call this function from C # code?

Will the classic approach work:

 [DllImport("myexe.exe", CharSet = CharSet.Auto)] public static extern int getSomething(); 
+6
source share
2 answers

Yes, any PE executable can export functions this way. Just keep in mind that the compiler sometimes cripples export names, which leads to the following things:

 MyAPIFunction@16 

You can verify that the names are in order by loading the executable into a tool such as PEInfo .

You should be able to call it just like a function in a DLL.

Update So it looks like you want IPC, not a P / Invoke call. See this page for information on how to use named pipes in C #. And here is a great place to start looking for information on how to use named pipes in C ++.

+4
source

Yes, you can export functions from .exe just like you can from .dll , and the way you showed is the right way to do this.

No, you cannot interact with an existing process by doing this, just as loading a function from .dll will not allow you to interact with other processes using .dll .

0
source

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


All Articles