CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Mar 2005
    Posts
    23

    Linking an executable to a loaded DLL

    Hello,

    I have an application that is being deployed on an intranet. The main executable is a windows form that references several DLLs, and these DLLs need to be updated from time to time. Users are using the application by referencing the executable on a shared drive. The problem is that once a user runs the application, the executable and DLLs are locked by the user and therefore cannot be updated.

    My solution to the problem is to use a loader program to load the assemblies into memory and have the application reference those instances rather than the files themselves. This works well when I have to maintain just the executable, but I do not understand how to get the executable to reference the loaded DLLs rather than the files.


    Thank you for your help!

    Code:
     Assembly exeAssembly;
        MethodInfo method;
    
        public void RunSystem(string ExePath, string[] DLLPaths)
            {
                System.Threading.Thread thd;
                System.Threading.ThreadStart ts;
                FileStream fs;
                BinaryReader br;
                List<Assembly> dllAssemblies;
    
                //Load the executable file into memory.
                fs = new FileStream(ExePath, FileMode.Open);
                br = new BinaryReader(fs);
                byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
                fs.Close();
                br.Close();
                exeAssembly = Assembly.Load(bin);
    
                //Load each DLL file into memory
                foreach (string s in DLLPaths)
                {
                    fs = new FileStream(ExePath, FileMode.Open);
                    br = new BinaryReader(fs);
                    bin = br.ReadBytes(Convert.ToInt32(fs.Length));
                    dllAssemblies.Add(Assembly.Load(bin));
                    fs.Close();
                    br.Close();
                }
    
                //Now that the assemblies are loaded into memory, the DLL references in the executable
                // need to reference the loaded DLL assemblies instead of the DLL files.
                //This is what I do not understand how to do.
    
                //............
    
    
                //Now, create a thread and run the loaded executable on it.
                method = exeAssembly.EntryPoint;
                ts = new System.Threading.ThreadStart(InvokeExe);
                thd = new System.Threading.Thread(ts);
                thd.TrySetApartmentState(System.Threading.ApartmentState.STA);
                thd.Start();
            }
    
            private void InvokeExe()
            {
    		 if (method != null)
                 {
                   	// create an istance of the Startup form Main method
                      object o = m_Assembly.CreateInstance(m_Method.Name);
                      // invoke the application starting point
                      method.Invoke(o, null);
                    }
                }
            }

  2. #2
    Join Date
    Apr 2010
    Posts
    4

    Re: Linking an executable to a loaded DLL

    Unfortunately, I don't know of any way to programmatically hook-up references via reflected assemblies. You could probably go about it two different ways:

    1. The hurdle you are trying to overcome is being able to push updates to shared assemblies. You could install the assemblies on each client, then implement an updater into your application that would check for updates upon startup. If updates exist, you kick off the separate updater application and stop the currently running application. The updater would run, copy the updated assemblies onto the local machine, then restart the application that initiated it.

    This would remove the need to have a centralized location for the assemblies.

    2. The other option would be to use reflection on the types that you are using from the reflected assemblies. You could build wrappers around them or use interfaces.


    The only problem with #2 is you're basically having to code around a deployment issue and isn't necessarily the greatest route to take. It may be a better investment to create an updater which can be generic enough to be used by other applications. I've done this at my current position and have multiple applications using one updater application.

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Linking an executable to a loaded DLL

    Honestly, the reason you are running into issues is because you are doing it wrong. Why does everyone access this executable from a share? That seems awfully hackish and error prone. Why can't it be installed on the client? Why can't it have a web interface?
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  4. #4
    Join Date
    Mar 2005
    Posts
    23

    Re: Linking an executable to a loaded DLL

    Thank you for your responses.

    The major reason why I would like a centralized location as opposed to installing assemblies on each machine is that I have no control over the machines, and anything I install locally is not reliable after a user logs out.

    I really do like the first option you presented bscar. I think I will do that, except that the updater will launch from the share and verify that all of the updated assemblies are on the local machine. That way I do not have to rely on the updater being present on any of the machines when the user needs it.

    This really is a great help. Thank you.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Linking an executable to a loaded DLL

    Check out 'One Click' deployment.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured