Click here to Skip to main content
11,642,233 members (60,324 online)
Click here to Skip to main content

Tagged as

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

, 24 Jan 2011 CPOL 3.7K 1
Rate this:
Please Sign up or sign in to vote.
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 Smile | :)

License

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

Share

About the Author

Darki699
Unknown
No Biography provided

You may also be interested in...

Comments and Discussions

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

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150731.1 | Last Updated 24 Jan 2011
Article Copyright 2011 by Darki699
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid