Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to inject your code into running WPF application

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:

like image 927
aku Avatar asked Jan 25 '09 09:01

aku


People also ask

What is dependency injection in WPF?

As you know, dependency injection is a form of “inversion of the control” (IoC) programming principle. This means that classes don't create the objects they rely on. DI frameworks have containers responsible for revealing and resolving dependencies.

Can WPF application run in browser?

WPF Browser applications (or XAML Browser applications, or XBAP) are a specific kind of application that are compiled into . xbap extensions and can be run in Internet Explorer.

Can WPF be targeted to Web browser True or false?

WPF only runs on windows. You can make a type of wpf application called xbap which runs in a browser.


2 Answers

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.

like image 188
GCamel Avatar answered Oct 08 '22 10:10

GCamel


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/

like image 2
Travis Acton Avatar answered Oct 08 '22 11:10

Travis Acton