![]() |
|
[.NET] [Tutorial]How to use the CodeDom Compiler in VB.NET[Tutorial] - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [.NET] [Tutorial]How to use the CodeDom Compiler in VB.NET[Tutorial] (/showthread.php?tid=2026758) Pages: Pages
1
2
|
[Tutorial]How to use the CodeDom Compiler in VB.NET[Tutorial] - Skillful - Dec 19, 2011 Hello. I am glad to present my tutorial! :) This tutorial will teach you to use the CodeDom Compiler in VB.NET Before you start reading this tutorial I would recommend you to check out this program so that you know what can you do by reading this tutorial. Its nice to be able to make programs with your own programs. You can customize it how you want and it dont need to have that big file size like VB.NET Express or Visual Studio. So im going to teach you how to:
So lets start! :D What is CodeDom? CodeDom is Code Document Object Model. This provides a graphical (in the sense of graph nodes, not of a picture) representation of code in the form of a tree. This represents the hierarchy of nested code elements (namespaces contain classes, classes contain methods, methods contain variable declarations, and so on). You can use CodeDom for generating an in memory code tree, or you can use it to compile ordinary code written in a file (like we will do later). View MSDN to read all about CodeDom elements. With this code tree, you can represent any piece of code independently of a specific language. Importing the necessary Namespace and declaring the required Variables To begin with... Import this namespace at the top of your code: Code Imports System.CodeDom.CompilerInside your class, you need to declare these variables: Code Private provider As CodeDomProviderIn this tutorial we will be using Options as CompilerParameters and Provider as a CodeDomProvider. Creating a Compiler To create a compiler, you must choose what language that shall be used, for example VB, CS, CPP, etc. To do this, use this code: Code If CodeDomProvider.IsDefinedLanguage("vb") ThenOf course you can change "vb" to whatever coding language you want, but we will come back to that later in Show installed Programming Languages. Compiler Options We will use the class CompilerParameters to set the options for our compiler. The available options are:
Adding References To add references to your project, use the ReferencedAssemblies property. This example demonstrates how to use this property: Code Dim OpenReferences As New OpenFileDialogIts smart to always add the System.dll reference because i cant think of any program that don't uses that. Mscorlib.dll is also a smart reference to add. To do so use this: Code Options.ReferencedAssemblies.Add("System.dll")Adding Resources Resources is a group of files compiled into a .NET Resource file. The can be images, sounds, text, or files. They are used by the program in different ways. One way is to display an image in a picture box, another is to change the language of the application. The EmbeddedResources property is mostly files or objects used in the application, while the LinkedResources can be culture options, these will be included in a separate assembly. To check if the assembly supports resources, use this piece of code: Code If Provider.Supports(GeneratorSupport.Resources) ThenTo add a resource file, or more use this: Code Dim OpenResources As New OpenFileDialogYou can change 'EmbeddedResources' to 'LinkedResources' if you want the resources to be linked in another assembly. Show installed Programming Languages Its handy to know which coding languages that is supported on your computer, here is some code to show it: Code Dim Languages As String = ""This code will show you a message box with each language on its own line. You can replace "vb" in Creating a Compiler with one of these languages. Compiling to an Assembly To compile files to an assembly we will use the CodeDomProvider.CompileAssemblyFromFile method. This requires 2 parameters CompilerOptions As CompilerParameters and FileNames() As String. We will use Options that we have set earlier in Compiler Options and some filenames that we will get with this code: Code Dim SourceFiles As New OpenFileDialogThis code opens a dialog for selecting files, then it tries to compile the filenames with the specified CompilerOptions and returns the result to the Results variable. Later on we can check if there were any errors with the Results.Errors property. Example I will now include a full example for compiling code. This will demonstrate the different properties we have been through. Also remember this project to see what you can make with the CodeDom compiler. Conclusion I have spent a lot of valuable time writing this tutorial so that other members or newbies can benefit from it. A thanks would be appreciated. Regards, Skillful * - daniellook - Dec 19, 2011 Great tutorial. I dont know if im ever gonna use it but I bookmarked it. Good job! * - зяяоя - Dec 19, 2011 Nice tutorial, but you might want to explain to other's exactly what CodeDom is and what it does, how to add icons, parameters etc etc. Nonetheless, good tutorial man. Good job. * - RiperFish - Dec 19, 2011 (Dec 19, 2011 08:00 AM)зяяоя Wrote: Nice tutorial, but you might want to explain to other's exactly what CodeDom is and what it does, how to add icons, parameters etc etc. Nonetheless, good tutorial man. Good job. They can simply search on Msdn.Great tut Dude :). * - зяяоя - Dec 19, 2011 (Dec 19, 2011 08:03 AM)₤l ΩaGnifiCo Wrote: They can simply search on Msdn.Great tut Dude :). But wouldn't that be defeating the purpose of a tutorial in the first place? I mean it doesn't bother me, i know codedom. Just trying to help the quality of the tutorial that's all. * - Skillful - Dec 19, 2011 Thanks for your suggestion зяяоя! :D I've updated the thread with the 'missing information' :D * - Weakky. - Dec 19, 2011 Nice tutorial man :) ! Keep it up 🤝 * - зяяоя - Dec 19, 2011 (Dec 19, 2011 08:08 AM)Skillful Wrote: Thanks for your suggestion зяяоя! :D =) i meant no disrespect, just trying to help. Great tutorial man. * - Tedd - Dec 19, 2011 Great tutorial. Bookmarked for reference! :) * - Skillful - Dec 19, 2011 Glad that you'll liked this 🤝 * - blackperuvian - Dec 19, 2011 Thanks Very usefull tutorial , HQ tut * - intrigued1980 - Dec 28, 2011 Nice tutorial man! Many thanks for this share. * - ProfitCoder - Mar 16, 2013 Hey ;) This is a very nice tutorial Thanks * - hansbald - May 21, 2013 Wow thanks thats what i needed! Nice work! * - gungunfebrianza - May 22, 2013 Sir... can you help me to hide this cmd form : [img]http://i564.photobucket.com/albums/ss86/febrianza/1-13_zps823e8f6c.png[/img] PHP Code and i need your help how to add some resource(VBS file) and embed it into an compiled executable ? so when i execute it the vbs executed :) |