Click here to Skip to main content
15,867,594 members
Articles / Web Development / CSS
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.
4.67/5 (10 votes)
21 Jan 2011CPOL2 min read 37.6K   14   16
This is an example of how you may take a COM *.dll file, and binary-embed it inside your C# project as a *.cs file. You can create a fully functional COM interface in managed enviroment using the steps described in the project.

Introduction


I wanted to prove that I can take any external COM Interface DLL and make it into a C# class object that can be used internally instead of Dynamically linking it.


When you add a resource COM interface, you actually dynamically link your project with a DLL file that will later show up in your project compilation output directory ..\bin\debug or ..\bin\release. In my opinion, this is not clean coding especially when you are using external DLLs that you have a license to use but don't want others to use them without a license. There are a few applications out there that can 'embed' your DLL files inside your executable, but those are just wrappers. What I'd like to demonstrate here is how we go back from Managed C# coding to the beginning of it all: Binary..


Background


What I have done is to create a simple [took me less than 2 hours] COM/DLL parser that can take any external COM *.dll resource file and convert it into Binary and then back into Managed C# code using the 'System' and 'System.Reflection' Namespaces and even add the COM classes and functions as C# methods.


The main objective is to convert the entire DLL into managed code!


The parser is not complete, of course, since it's just an example, but it is living proof that even with managed code you can access and embed the low-level binaries. So it's nice and you may (or not) even learn something. I hope someone will see this as a challenge and continue this parser, since it's a lot of 'dirty work', it's not hard enough to be interesting, and I'm too lazy to complete all the options (interfaces, variables, enums, etc... and then recursion of it all (classes inside classes and so on...)). But the main engine is already built, so...


Using the Code


I added the entire solution, and a demo executable. The entire parser is just a small class that uses the System.Reflection.Assembly to rebuild the DLL file back from binary after I embed the binary itself inside the C# file. After that, you may parse and use the DLL classes and methods as if you are using any other C# method, internally.


Points of Interest


This is a project output example, the highlighted file is the Shell32.dll:


The DLL file, in this example: Shell32.dll

This is after I parsed and saved the DLL as a CS file, I created Shell32.cs that should maintain all the functionality of the DLL file:


The CS file that can be included in your project, in this example: Shell32.cs

This is a screenshot of the compiler, loading the Shell32.dll file:


Loading the Shell32.dll file

This is a screenshot of the compiler, after it parsed the Shell32.dll file:


Parsing the Shell32.dll file

Then you save the 'DLL' file as a C# Shell32.cs file:


Saving the Shell32.cs file

And it's that simple.

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

 
QuestionTLB ? Pin
Wrangly31-Mar-19 21:51
Wrangly31-Mar-19 21:51 
GeneralReason for my vote of 5 Nice tip! Pin
Manfred Rudolf Bihy25-Jan-11 10:22
professionalManfred Rudolf Bihy25-Jan-11 10:22 
GeneralReason for my vote of 5 Great idea! This way one can zip COM... Pin
carga25-Jan-11 1:35
carga25-Jan-11 1:35 
GeneralRe: Or download the compatible dll (after you figure out the sys... Pin
Darki69925-Jan-11 7:48
Darki69925-Jan-11 7:48 
GeneralReason for my vote of 4 nice article Pin
Kong-Chang Jie24-Jan-11 11:22
Kong-Chang Jie24-Jan-11 11:22 
GeneralReason for my vote of 4 ??? Pin
Kong-Chang Jie24-Jan-11 11:16
Kong-Chang Jie24-Jan-11 11:16 
GeneralI gave him a 1 because this is a terrible idea. 1) it makes... Pin
SledgeHammer0121-Jan-11 8:43
SledgeHammer0121-Jan-11 8:43 
GeneralRe: It is expensive, and it's does make the executable a tad lar... Pin
Darki69924-Jan-11 10:09
Darki69924-Jan-11 10:09 
GeneralReason for my vote of 5 Innovative! Pin
gparaskevas21-Jan-11 2:33
gparaskevas21-Jan-11 2:33 
GeneralYou talk about an interpreter, that sounds pretty "hard and ... Pin
Espen Harlinn20-Jan-11 21:16
professionalEspen Harlinn20-Jan-11 21:16 
GeneralWhile not uninteresting (codegen is always is interesting), ... Pin
leppie20-Jan-11 17:49
leppie20-Jan-11 17:49 
GeneralMy vote of 4: As stated above, I don't think that downvotin... Pin
CDP180220-Jan-11 12:59
CDP180220-Jan-11 12:59 
GeneralYou are a bit hard on him. He wants to embed the DLL into th... Pin
CDP180220-Jan-11 12:25
CDP180220-Jan-11 12:25 
GeneralRe: Check out my solution for that in 'Alternate 1' I added. Thi... Pin
Darki69925-Jan-11 13:12
Darki69925-Jan-11 13:12 
GeneralReason for my vote of 1 I don't get it. Why would you do thi... Pin
SledgeHammer0120-Jan-11 10:09
SledgeHammer0120-Jan-11 10:09 
GeneralRe: The main reason for this example is to explain how to embed ... Pin
Darki69924-Jan-11 9:59
Darki69924-Jan-11 9:59 
The main reason for this example is to explain how to embed binaries and interact with them under managed C# coding. It's not supposed to be effective, it's supposed to be a demonstration of how to run a dll (or any byte data for that instance, could be an executable, an image etc...) from inside a class object. In my opinion grading or downgrading an example like this is meaningless Poke tongue | ;-P

Why not use COM ref. you ask?

1. Maybe it's not a COM at all.. or maybe it's a purchased COM that you have the license to use but don't want others to be able to access it.

2. Maybe it's a unique version of the DLL, that will not be found on the end users computer

3. Maybe you want to download the binary dynamically from a website and then run them from the memory, instead of hard coding refs [Read the alternate I just added to understand how to do that]?

In a short version: COM refs are good, but knowing how to use the binaries dynamically is great Smile | :)

NJoy

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.