Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you ensure an executable is opened via another executable?

Tags:

c#

windows

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?

like image 659
Joseph Nields Avatar asked Aug 19 '15 13:08

Joseph Nields


People also ask

How do you run an executable file in a particular operating system?

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 .

How do I allow access to an executable file?

-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.

How do I run exe files on another computer?

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.

How is an executable file executed?

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.


1 Answers

  1. Pass a command line parameter to actualProgram.exe that lets it know it was launched by the launcher. (Also suggested in a comment on the OP by oleksii)
  2. Set an environment variable in the launcher that can be inherited by the child process.
  3. Use one of the documented methods for getting the parent process id, and use that to get information about the launching process. (Of course if the parent process terminates, Windows is free to re-assign the PID to a new process.)
  4. Create a memory-mapped file/named pipe/etc. in the launcher that can be used to pass information to the child.
  5. Manually load and unload the library references in the child. If they are .Net assemblies, you can use the 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:

  • Run-Time Dynamic Linking @ MSDN
  • How can a Win32 process get the pid of its parent?
  • Assembly.Load Method (Byte[], Byte[])
like image 136
theB Avatar answered Sep 22 '22 05:09

theB