Important alert: (current site time 7/15/2013 9:17:46 AM EDT)
 

winzip icon

Run Code from string

Email
Submitted on: 2/13/2003 3:35:43 PM
By: Jose Fuentes VB MVP  
Level: Advanced
User Rating: By 41 Users
Compatibility: VB.NET
Views: 29105
(About the author)
 
     You can now save a whole class in a field of a table and call it and run it. This code will provide the answers of how to turn a string into code that runs. kind of like Scripting your application though much more powerfull
 
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.


Other 33 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

2/13/2003 3:49:28 PMJose Fuentes

Please Vote
(If this comment was disrespectful, please report it.)

 
2/14/2003 2:51:19 AMMOB GoAn

nice code dude
(If this comment was disrespectful, please report it.)

 
2/18/2003 12:58:51 PMJose Fuentes

Lets vote, guys 402 visits and only 4 votes? This has gotta rock some of you guys? I have been brain storming of how I could use this. One idea was to make an addin for VS.Net and just save the code to a table Then just build menu system that would pull on click the string form the table and build the Form. This way someone could just make a change at a client site to the code in the table and the next time a client clicks the button boom it's changed, kind of like Web programming.
(If this comment was disrespectful, please report it.)

 
2/18/2003 6:12:26 PMKevin Pirkl

Pretty cool code. How do you make it handle other Imports Assemblies? Excellent vote from me.
(If this comment was disrespectful, please report it.)

 
2/18/2003 6:24:39 PMJoe Fuentes

Notice this section
PM.ReferencedAssemblies.Add(ASM.Location)

What's happening there is that it's going though a loop of all the assemblies loaded for that object so far to add more you would do

PM.ReferencedAssemblies.Add("PathtoDLL")

on it's own line after the loop.

You only have to provide the path and name and you are done.

Hope that helps.

Joe

(If this comment was disrespectful, please report it.)

 
3/31/2003 11:21:44 AM

Wow, this is seriously cool stuff!
(If this comment was disrespectful, please report it.)

 
4/1/2003 1:26:59 AM-LankZ-

Wow, its just like the VBScript control in VB6. I got a question for ya, is it possible to use classes in the acutal program through the compiled code?
(If this comment was disrespectful, please report it.)

 
4/1/2003 9:18:12 AMJose Fuentes

Yes you can use the PM.ReferencedAssemblies.Add("PathtoD
LL")
as given in an example above to add it as a reference for the compiled code or in class you use you can public or private declare a class that way when it loops though all the assemblies the program class that is running the compile is using it will auto add that declared class as a reference. Then at that point you just do a Dim whatever as whateverclass

Now of course if you want to be able to be able to pass a class as a object you can setup a public passobj as new object in the string code and then after you have it compiled you can pass the object to the new compiled object and run whatever methods.

I hope I didn't get this any more confused then you are now.

Joe
(If this comment was disrespectful, please report it.)

 
4/1/2003 9:18:25 AMJose Fuentes

Personally I have a form designer and I use it to add custom actions to form objects placed on the form. Just like you would if you did it in the IDE. So if they place in the click event of a button they put on the form something like

msgbox("hello")

then i setup an event and compile the class pointing the event to that sub and boom they hit the button and it pops up hello.

Of course I do just one compiled string class and just have many subs auto created based on what events they selected and the code they inserted for those events. So only one compile is done.

I have been in contact with MS about why they don't have it not make a DLL but they said it will always create a DLL and this is the only process that slows things down.

Hope I helped
Joe

(If this comment was disrespectful, please report it.)

 
5/20/2003 9:17:49 AMG.T. Dudley

I like the code. I have played with this
some in a current project.
I noticed that when I run the sample once
Then change the code and run again
it does not change the result from the first run.
Is this only happing to me?

(If this comment was disrespectful, please report it.)

 
5/20/2003 9:26:18 AMJose Fuentes

Commit out the line inmemory or memory something.

I have found that it will load it into memory and no mater how many times you rerun the compile it will use the one in memory untill the application closes.
Sorry I can't give you the exact name, I am not at my system. it's something like load in memory = true or something like that
It does have a (=true) in it.

Hope that helps.

Joe
(If this comment was disrespectful, please report it.)

 
5/21/2003 2:16:21 AM

Cool stuff, only drawback: if you modify the code and run it again the previous code is executed and not the new one.

Thomas
(If this comment was disrespectful, please report it.)

 
5/21/2003 8:25:44 AMJose Fuentes

My answer to someone eles as it's posted above your response Thomas is to commit out
PM.GenerateInMemory = True
and it will execute the code shown every time.
(If this comment was disrespectful, please report it.)

 
5/21/2003 12:35:10 PM

Quite cool job buddy. Appriciation. This is a new idea to compile and execute code at run time. With this idea, for an end user, desktop application also can behave like web application. Excelent...... from my side buddy.
(If this comment was disrespectful, please report it.)

 
5/21/2003 5:56:29 PMTheBigRocket

Man, totally awesome! This is just what I was looking for. ***** from me.
(If this comment was disrespectful, please report it.)

 
5/30/2003 9:05:19 AMG.T. Dudley

I commented out the PM.GenerateInMemory = True and it still runs the code from the first run and I also get a compile error stateing Unable to write to outputfile.

Is there a way to run the code once then change the code and run it again with the new code?
(If this comment was disrespectful, please report it.)

 
5/30/2003 9:16:31 AMJose Fuentes

Ok the output name, change that dynamicly like maybe use a Guid .DLL for the name and even set it to a temp folder.

application.startup & "\Temp\" & guid.newguid.tostring & ".DLL"

when the app loads have it remove all contents of the file (the app running the code). Also make sure you have a folder in the root called Temp

THis was one way for me to fix the problem. I'm in contact with a MS employee about this issue. Now another way, if you don't mind using the last compile. Is to do a try catch on compiling the file and if it fails you load up the dll in the catch portan. Though this would mean that it will not be able to recompile changes untill you log out of the program and log back into it.

Also another way would be to add a dispose to your running code example so that you can dispose of the object. That way it can be free to compile again since the DLL won't be locked.

Not sure if that would work, though it sounds like it will.

Let me know
(If this comment was disrespectful, please report it.)

 
6/30/2003 2:10:27 AM

this is just what I was looking for, it will keep me from having to write a scripting language for my new app. thanks a lot. Great code, and well commented.
(If this comment was disrespectful, please report it.)

 
6/30/2003 3:07:37 AM

How can I make this load Forms with buttons and stuff?
(If this comment was disrespectful, please report it.)

 
6/30/2003 7:52:28 AMJose Fuentes

Well you will have to define the whole form as a string. Then use the example to compile it and then with the instance of the object you do.
obj.show()
You could also just read a text file. Like a vb.net form source file form1.vb and then add a namespace and compile it and show the form.

Have fune.
Joe
(If this comment was disrespectful, please report it.)

 
6/30/2003 2:43:47 PM

how do i add a namespace ??
(If this comment was disrespectful, please report it.)

 
7/7/2003 11:02:38 PMJose Fuentes

Look at the code, it's creating one when it constructs the string. There is more to it then the code you see in the textbox, you have to put that code in a class and in a namespace.

Hope that helps.

Joe
(If this comment was disrespectful, please report it.)

 
10/28/2003 9:15:55 AM

This code is excellant. I'm working on a project right now that I need this for. Can you reccomend a book or reference manual on the codedom compiler (VISUAL BASIC ONLY PLS) ?
(If this comment was disrespectful, please report it.)

 
10/28/2003 9:35:04 AMJose Fuentes

Well I'm in the proccess of getting a book proposal though. If it get's though I'm writing a book on application frameworks and this is going to be covered quite a bit. I don't know of any other book that covers this and in VB.net.

I'll let everyone know when I get it done.

Joe
(If this comment was disrespectful, please report it.)

 
10/28/2003 1:43:14 PMNormand Carbonneau

Great, It's exactly what I need. Thanks for sharing...
(If this comment was disrespectful, please report it.)

 
12/4/2003 10:47:41 PMGeoffreyLee

This is certainly excellent coding. You are a real genius. Good job.
(If this comment was disrespectful, please report it.)

 
2/25/2004 3:07:55 AM

very good

(If this comment was disrespectful, please report it.)

 
5/3/2006 11:56:44 AMravi

I am working on storing the form as an object to a table. What I mean when I sat form as an object is, I want to store all the controls such as textboxes,labels etc. and their properties into datatable.Later when loading the form I would just obtain the form object from table and generate the form design and layout. Can this be done using the above code
(If this comment was disrespectful, please report it.)

 
6/15/2008 11:31:26 AMsebastian nielsen

GREAT!
Now I can encrypt portions of a application with system.cryptography.Rijndaelmanaged and protect the application with a key derived with Sha512 from a password. So it will simply run the decrypted program code when correct password is specifyed.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.