6

I'm trying to monitor a running application written in C++ using a different C# application.

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

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

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

Will the classic approach work:

[DllImport("myexe.exe", CharSet = CharSet.Auto)]
public static extern int  getSomething();
3
  • 1
    To answer the first sub-question: Yes, executables can have exported functions. Commented Oct 6, 2011 at 10:05
  • 3
    for clarification though you will be loading the module into your own process not hooking into the "running application" you are trying to monitor. To do that you will need some kind of IPC. Commented Oct 6, 2011 at 10:26
  • @Yaur yes, IPC is what I was looking for. After some lurking, I realized what I wanted is impossible without IPC. Commented Oct 6, 2011 at 12:23

2 Answers 2

5

Yes, any PE executable can export functions this way. Just keep in mind that the compiler will sometimes mangle the export names, resulting in stuff like this:

MyAPIFunction@16

You can check that the names are OK by loading the executable file into a tool such as PEInfo.

You should be able to call it in exactly the same way you would a function in a DLL.

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

Sign up to request clarification or add additional context in comments.

3 Comments

Will calling, for example, a set function change the respective variable in the C++ process?
No. You're calling a function in the context of the caller, not in the context of another process. It maps the binary image into the .NET process's virtual memory and performs a P/Invoke call to the address of the specified function. Everything occurs within the .NET process, just as if it were a DLL you were loading. I think you're looking for a solution to performing operations in another running process. If that's the case, you need an Inter-Process Communication mechanism. There's a few ways to do it, but named pipes and sockets are usually the best options.
yes, that's exactly what I'm trying to do. I wanted to avoid inter-process communication, but apparently there's no other way (which makes sense, that's why I asked the question). Thanks for the answer nonetheless.
0

Yes, you can export functions from a .exe exactly like you can from a .dll and the way you've shown is the correct way to do that.

No, you can't interact with an existing process by doing that, just as loading a function from a .dll wouldn't allow you to interact with other processes using that .dll.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.