2

I have a WPF application that acts like some kind of 'loader' and with that loader I get a byte[] from my SQL server and I invoke that method like this:

Assembly assembly = Assembly.Load(bin);
MethodInfo method = assembly.EntryPoint;
method.Invoke(null, null);

How ever, that will start the new process inside the loader process but when the application been loaded I would like to close the loader. Can I somehow invoke my method as a new procees / inside another process?

Joakim Carlsson
  • 1,540
  • 5
  • 19
  • 42
  • Could you spool the data to disk and start a new process based on that? – Rowland Shaw Feb 12 '16 at 12:56
  • Possible duplicate of [How do I use reflection to invoke a private method?](http://stackoverflow.com/questions/135443/how-do-i-use-reflection-to-invoke-a-private-method) – David Pine Feb 12 '16 at 12:58
  • @DavidPine How is this a duplicate, I don't want to invite a private method, I want to invoke a method so it runs in a new process so I can close my loader process. – Joakim Carlsson Feb 12 '16 at 13:18
  • @DavidPine I guess I could, but the reason I want to do it like this is so that the user can't get the hands on the .exe / .dll to easy. – Joakim Carlsson Feb 12 '16 at 13:19
  • I have an answer for that then... – David Pine Feb 12 '16 at 13:20
  • Why do you need to exit the loader process? – usr Feb 12 '16 at 13:24
  • Because the loader is a WPF application and my other application is a Console application. The WPF application takes a lot of RAM, an as the loader is not needed on the Console Application been launcher I see no reason to keep the Loader Active. – Joakim Carlsson Feb 12 '16 at 13:26

3 Answers3

1

You are looking for handling AppDomain.AssemblyResolve event. Then you can embedded the primary executable's references as resources, i.e.; the *.dll's that your app relies on can be embedded resources. Then when you handle this event, you can get the resource and load the assembly returning it from said event handler.

This is extremely helpful in preventing users from getting there hands on external .exe / .dll.

David Pine
  • 23,787
  • 10
  • 79
  • 107
0

If you are able to posses the name/path of the application to run use Process.Start method. Details here: https://msdn.microsoft.com/en-us/library/system.diagnostics.process.start(v=vs.110).aspx

0

You cannot reach into another process that way without that process cooperating. Probably, you need to author a managed exe that loads the DLL and runs it.

You could even do that in your existing application. Create a command line argument that causes your application to load a DLL and run it instead of showing a WPF UI. Then, restart "yourself".

Or, just close the WPF window and exit the UI thread. That should clean up most of the resources.

usr
  • 168,620
  • 35
  • 240
  • 369