Joakim Carlsson
Joakim Carlsson

Reputation: 1580

C# Reflection invoke method in a new process

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?

Upvotes: 2

Views: 1421

Answers (3)

usr
usr

Reputation: 171188

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.

Upvotes: 0

David Pine
David Pine

Reputation: 24525

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.

Upvotes: 1

Marcin Iwanowski
Marcin Iwanowski

Reputation: 791

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

Upvotes: 0

Related Questions