In C# when Application A calls Application B using an application handle such as (IAppB)Activator.CreateInstance(Type.GetTypeFromProgID('AppB')).CallMe()
, how does App A send the call to App B? I'm not asking for a hardware layer explanation, I'm trying to figure out if it is possible to somehow capture method calls to an application in order to mess with them.

- 15,336
- 34
- 92
- 135
2 Answers
Given the usage and names, in all likelihood you are making a method call to an out-of-process COM server. The mechanics of it can fill a book (many have been written about) so just a brief overview.
The ball starts rolling by starting the COM server, using configuration info that's stored in the registry. The underlying native api call is CoCreateInstance(). The COM infrastructure then again uses the registry to discover the DLLs that act as the proxy and the stub. The job of the proxy is to emulate the exact COM interface that you are using. In this case the default interface of the coclass, the one that declared the CallMe() method.
The proxy serializes the passed arguments, if any, then calls RPC to emit a call request to the server. At the server end, the stub acts as the client code. It deserializes the arguments and makes the actual call. The call result, at least the HRESULT, and possibly any arguments that are passed by reference and need to be copied back then travel back to the proxy to complete the call.
Messing with this scheme at the low-level RPC boundary isn't practical. You could however technically create your own proxy and stub. There are significant obstacles, they are normally auto-generated from a description of the interface written in the IDL language by the MIDL tool. At the very least, you'll need that IDL so you know exactly what the interface looks like. That tends to be difficult since you rarely own the source code of the server so don't have the IDL either. You can possibly decompile the type library if the server has one. That's unlikely since you are making a late-bound call. Last but not least, you'll need to know RPC pretty well. Difficult too, it's very obscure.

- 922,412
- 146
- 1,693
- 2,536
you are mixing up multiple things here
- Application A calling Application B - this typically means 'Process A' calling 'Process B'
- C# method A calling method B, - any normal call in C#
- C# calling COM (which is what yr code snippet is doing)
- Invoking via reflection techniques. Which is also in your sample
The answer to your question depends on exactly which one you are talking about.
If you are looking at simply place yourself in general between function A and function b in the same app domain look into AOP - Aspect Oriented Programming in C#
Or interception http://www.codeproject.com/Articles/8436/Intercepting-method-calls-in-C-an-approach-to-AOSD
Or look at castle dynamic proxy http://docs.castleproject.org/Default.aspx?Page=DynamicProxy