Running WPF Application - Snooping Programmatically

Jun 19, 2012 at 1:25 AM
Edited Jun 19, 2012 at 3:11 AM

I have a WPF application (following MVVM). Now I need to create a tool (preferably Commnad app) which will run in the background.

Once my WPF app is launched; I will snoop this running app through my tool and will examine the UI (View) and find out the ViewModel instance of that.

I was thinking to use the Snoop dlls to do that.

But I am not sure how can I do that progrmmatically?

If anyone has already done this then let me know or any idea is welcome.

One more thing to add - the tool I am trying to build is for testing this WPF application. So even if someone has any idea using Coded UI or other tool; then also please let me know.

Actually my tool will load the WPF App; then by inspecting/snooping it's View I will reach out to ViewModel. Using ViewModel; I will write my tests.

Jun 19, 2012 at 5:46 PM
Edited Jun 19, 2012 at 5:52 PM

Hi sinhaar,

I'm not sure if I understand your question correctly, but I will try my best to answer it anyway. I think what will help you achieve what you want is if you understand the Snoop injection process. When Snoop "attaches" to your process, it is actually injecting itself into the WPF process. It does this by launching the ManagedInjectorLauncher. This C# code uses the managed C++ library (ManagedInjector) to do the injection. You can start by recompiling Snoop, and putting this statement:
At the beginning of the ManagedInjectorLauncher program. This statement will basically prompt you to attach a debugger to the program, and from there on you can step through the injection process. After knowing how snoop works and knowing where to step through the code, it should be very easy to extract whatever snippet you may need from Snoop, and use it into your own programs or libraries.

I hope this helps.

 - Maciek 

Jun 19, 2012 at 6:33 PM

Thanks for your input Maciek.

I have downloaded the code base of Snoop. Let me try this out in ManagedInjectorLauncher32-4.0 project.

Hopefully, if I directly use the codebase in my project, there should not be any issue as this is open source.



Jun 20, 2012 at 1:15 AM

Hi Maciek,

I am able to attach/inject my application and Snoop as you mentioned.

It's almost like the same code ~\Snoop\Injector.cs (method Launch).

Now once my utility starts the process of ManagedInjectorLauncher (with proper parameters), is there a way my utility gets the control back with instance of Snoop classes. Using those instance I want to get the Window object.

Or what's the way to get the Window object back?

Jun 20, 2012 at 3:59 AM

Hi Arindam,

After the injection process, Snoop actually runs inside the application which had the code injected. The rest of your code will be running inside the application that had the assemblies injected. That being said, you will need to do the rest of your processing inside the application. 

For further ideas of what to do, examine what snoop does. Put a breakpoint in the method, GoBabyGo inside SnoopUI.xaml.cs. This code will run inside the application being snooped, so you will need to open this file in the IDE that's debugging the application being snooped. If you are unable to hit this breakpoint, put in the Debugger.Break() command in this method as you did in the managed injector launcher program. Stepping through the snoop code, and seeing how everything is done will teach you a lot :-).


 - Maciek

Jun 20, 2012 at 7:46 AM

Thanks Maciek. I am on it and could see the light at end of tunnel.

Perfect. This is really a good learning experience.