Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / CSS

C# An Example of a COM Parser, Embed COM Interface DLLs in a *.cs file

5.00/5 (2 votes)
24 Jan 2011CPOL 10.8K  
Another way to use the code above is to create dynamic dynamic linking, to download dlls and embed them during runtime!How to do this:1. Add a project reference to a COM object, but DO NOT include the DLL in the executable folder2. Do not call any of the functions from the COM ref...
Another way to use the code above is to create dynamic "dynamic linking", to download dlls and embed them during runtime!

How to do this:

1. Add a project reference to a COM object, but DO NOT include the DLL in the executable folder
2. Do not call any of the functions from the COM ref above, until after you complete the following steps:
3. Download a byte array from a website during runtime, this is your recent most updated DLL COM file, save the dll into: byte[] bytes (for the example purpose).
4. Add the following lines to your project:
// Here you load the COM object to memory from the downloaded byte array 'bytes':
System.Reflection.Assembly myCOM = System.Reflection.Assembly.Load(bytes);

// Create a resolve event handler, when the dll can't be found it will use this:
System.ResolveEventHandler CurrentDomain_AssemblyResolve_ResolveEventHandler;
CurrentDomain_AssemblyResolve_ResolveEventHandler = new System.ResolveEventHandler(CurrentDomain_AssemblyResolve);

// Now add an event listener to the assembly resolve event of the current application domain:
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve_ResolveEventHandler;


Add the function to catch the 'DLL not found' event:
//Add this function 'CurrentDomain_AssemblyResolve', it will return the downloaded COM when the system can not find the DLL requested

     public System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs arg)
        {
            if (arg.Name.Contains("NAME_OF_THE_COM_YOU_ADDED"))
            {
                return myCOM;
            }
        }

5. Now call the function you need from the COM, and the dynamically downloaded DLL will be attached during runtime ^^

And this is another example of how to use the code above :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)