Click here to Skip to main content
Click here to Skip to main content

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

By , 24 Jan 2011
 
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)

About the Author

Darki699

Unknown
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 5 Nice!mvpManfred R. Bihy25-Jan-11 10:21 
GeneralP.S. Notice above that the object 'myCOM' can also be delet...memberDarki69924-Jan-11 10:48 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 24 Jan 2011
Article Copyright 2011 by Darki699
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid