There's a neat little trick that you can use to never have the OS hold a lock on the .dll
's / .exe
's an .exe
needs to run.
Let's say our directory looks like this:
>opener.exe >actualProgram.exe >dependency.dll
As a basic example, we could do this:
namespace Opener
{
class Program
{
static void Main(string[] args)
{
AppDomain domain = AppDomain.CurrentDomain;
AppDomain newDomain = AppDomain.CreateDomain(
domain.FriendlyName,
domain.Evidence,
domain.BaseDirectory,
domain.RelativeSearchPath,
true, // "shadow copy" files: copy rather than lock
domain.SetupInformation.AppDomainInitializer,
domain.SetupInformation.AppDomainInitializerArguments
);
string assembly =
System.Reflection.Assembly.GetExecutingAssembly().Location
+ "\\..\\actualProgram.exe";
newDomain.ExecuteAssembly(assembly, args);
}
}
}
We could also put more logic in there, which we could use to actually replace/update actualProgram.exe
despite an instance of the process being opened. Try it: you'll be able to delete/modify/replace dependencies and the "actual" program when it's loaded via the "opener". Obviously this has its benefits, namely making updates a lot easier.
But in your "actual.exe", how can you be sure that it was loaded via another executable, and know that it was loaded without having a lock taken on the .exe
? If someone is to just load the "actual.exe", i.e. not via the "opener", I want to make sure that the process immediately exits.
Hints?
To execute a file in Microsoft Windows, double-click the file. To execute a file in other GUI operating systems, a single or double-click will execute the file. To execute a file in MS-DOS and numerous other command line operating systems, type the name of the executable file and press Enter .
-Right click on the Start button to open the menu, select Run to open the Run box and type netplwiz in the Run box. Press Enter to open the User Accounts settings box. -Click the Properties button and set the level of access you want to grant the user. Select Administrator and click Apply / Accept and exit.
You may Share a folder containing the .exe on your computer within the domain and use telnet to get a command line on the other computer to run the program.
In order to be executed by the system (such as an operating system, firmware, or boot loader), an executable file must conform to the system's application binary interface (ABI). In simple interfaces, a file is executed by loading it into memory and jumping to the start of the address space and executing from there.
actualProgram.exe
that lets it know it was launched by the launcher. (Also suggested in a comment on the OP by oleksii)AssemblyResolve
event, loading the file from disk into a byte array and using the Assembly.Load(byte[], byte[])
overload.Note: As long as all of your assemblies are loaded manually or by the runtime before you modify/move/delete them, you're probably ok. Also, changing the DLL after it's been loaded will probably have no effect on the running process. I say probably in both cases, because it's generally a good idea to leave the libraries alone at runtime, especially if you aren't loading them manually.
See Also:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With