Click here to Skip to main content
15,881,559 members
Articles / Web Development / CSS
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
24 Jan 2011CPOL 10K   1   2
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)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 Nice! Pin
Manfred Rudolf Bihy25-Jan-11 10:21
professionalManfred Rudolf Bihy25-Jan-11 10:21 
GeneralP.S. Notice above that the object 'myCOM' can also be delet... Pin
Darki69924-Jan-11 10:48
Darki69924-Jan-11 10:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.