12

Here is my task:

  • Inject custom managed code into running managed WPF application (i.e. my code should run in other AppDomain)
  • Injected code must be executed on UI thread

When I say 'best' I mean that:

  • Injection process must be as stable as possible (no thread deadlocks, etc.)
  • Code should run on x86 and x64 platforms without problem (especially on Vista x64)

Minimal use-case

  • select running WPF program
  • inject custom code
  • inject code changes title of the main window of target process to "Code Injected"

Solutions I'm evaluating:

1) Injection via windows hooks (like in Snoop)

pros:

  • injected code runs on UI thread

cons:

  • requires mixed (c++/cli) external dll
  • hard to debug

2) Injection via EasyHook library

pros:

  • library looks solid and well-tested

cons:

  • didn't find a way to run code on UI thread
  • injection library must be signed and installed in GAC

3) Injection via WriteProcessMemory/CreateRemoteThreadEx/LoadLibrary

pros:

  • simple

cons:

  • very unstable (code must be executed in DllMain, CLR hosting required, etc)

I'm going to use method #1. Can you recommend a better approach?

Are there any decent techniques based on CLR hosting in unmanaged DLLs ?

Note, that I'm aware of these questions:

1
  • 2
    How did this go? Any downsides of Option?
    – Cilvic
    May 25, 2011 at 9:06

2 Answers 2

2

on my old project, I was using CECIL (done for mono) which allow me to inject performance tracing code in any code. Sample is too big to be pasted here, but have a look to the project ReflectionStudio and especially this injector class - . It is allways available on mono/cecil web site - only thing is to take care of your request about the main UI thread.

2
+50

Since a user put an open bounty on a question delivered nearly 9 years ago, I'll throw in an updated option I've used for a similar project:

The Prism framework for WPF is very well documented and solid. Now I'm not sure it's compatibility with Vista (original request 9 years ago) and I'm not sure that is important anymore. It contains a very MEF like function in its unity bootstrapper class such that you dynamically load what they call modules class libraries (which is really just XAML containers).

So you build out your module class libraries and at WPF shell runtime it scans your module directory in your container application for any of the module assemblies and dynamically loads via a module catalog (method of the Unity Bootstrapper) into the host container through predetermined regions. (sounds really MEF like huh?)

Now Brian Lagunas (http://brianlagunas.com) has an example posted that takes this a step further. His module catalog implementation continuously scans a directory so as dll's are added (during shell runtime) it picks the files up and processes them which in effect gives you the ability to add managed code on the fly. Add an upload button to the WPF shell that points to the module directory and utilizes Prism's Event Aggregator and you can flip the main title of the shell whenever a new module is loaded in.

Here is a link to Brian's post discussing loading modules at runtime: http://brianlagunas.com/prism-dynamically-discover-and-load-modules-at-runtime/

Link to GitHub example: https://github.com/brianlagunas/DynamicallyDiscover-LoadModules/

If you have never used Prism then highly suggest you watch Brian's crash course video. It is around 120 minutes and will get you going quickly with Prism even though the version is slightly outdated. http://brianlagunas.com/infragistics-webinar-mvvm-made-simple-with-prism-sample-code/

5
  • So is the downvote on the quality of my answer? prism as a solution? or is there a better one? I'm here to learn as well so some sort of explanation would be just lovely. Apr 3, 2018 at 19:33
  • 1
    I guess the reason for downvote was that you did not answer the question. The question is about injecting custom code into any running WPF application, not just PRISM WPF application. Apr 4, 2018 at 7:14
  • @VojtěchDohnal prism is a framework for wpf that allows you to inject managed code into a running wpf application, specifically gaining access to the UI thread as the question stated. I’m guessing my answer isn’t directly to the point and unclear so I will find some time to clean it up for better understanding. Thank you for taking the time for critique. Apr 6, 2018 at 23:56
  • @Travis Acton, Example you provided does exactly what Vojtěch Dohnal said - injects code into WPF app built with injection supporting code.It can't be used for injecting code into any third party WPF app. Jul 27, 2018 at 14:43
  • @nicolay.anykienko no where in the requirements does it state that he is trying to inject code into a third party application. He simply states that the running host application can be on a different domain which simply infers that the interaction between host and injected code cannot be be domain dependent. Jul 27, 2018 at 18:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.