473,434 Members | 1,500 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

Compile on the Fly

I am writing an application that will require new commands to be added while
the program is still running. I thought I had found the answer in .nets
compiler facilities. I would seperate my commands out into a dll and compile
this at startup and when the source file is changed. This works fine on
startup of the application. But when I try to recompile the dll during
runtime it doesnt seem to work. If I leave the current dll in place then it
just carries on using the old dll and if i delete it then the compile
function returns that it cannot find the new dll.

Help would be appreciated.

Public Function CompileCommands() As String

Dim VBP As New VBCodeProvider()

Dim CVB As System.CodeDom.Compiler.ICodeCompiler

CVB = VBP.CreateCompiler

Dim PM As New System.CodeDom.Compiler.CompilerParameters()

PM.GenerateInMemory = True

PM.GenerateExecutable = False

PM.OutputAssembly = "commands.dll"

PM.MainClass = "command"

PM.IncludeDebugInformation = False

Dim ASM As System.Reflection.Assembly

'I believe this is to get all the assemblies of the current

'application and add them as references to the new assembly

PM.ReferencedAssemblies.Add("OpenTelnet.exe")

For Each ASM In AppDomain.CurrentDomain.GetAssemblies()

PM.ReferencedAssemblies.Add(ASM.Location)

Next

'here we do the deed

Dim Results As System.CodeDom.Compiler.CompilerResults

Results = CVB.CompileAssemblyFromFile(PM, Application.StartupPath &
"\defaultcommands\commands.vb")

'CVB.CompileAssemblyFromSource(PM, )

'this is just to list out all the errors if any

Dim Err As System.CodeDom.Compiler.CompilerError

Dim strerr As String

For Each err In Results.Errors

strerr = strerr & "Number " & err.ErrorNumber & " Text " & err.ErrorText & "
Line " & err.Line & vbCrLf

Next

If Results.Errors.Count > 0 Then

Return strerr

Exit Function

End If

'now we create an instance of the assembly newly created

'and set it to an object notice this is also a good example of late binding

Dim vargs As Object

commandobj = Results.CompiledAssembly.CreateInstance("commands. command",
False, Reflection.BindingFlags.CreateInstance, Nothing, vargs, Nothing,
Nothing)

End Function

Cheers

Daren
Nov 20 '05 #1
3 6227
May I ask why this needs to be done? Perhaps I can offer an alternate
solution, using the System.Reflection.Emit namespace. It contains classes
that allow you to dynamically build an assembly in memory.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
"Daren Fox" <da***@foxonline.co.uk> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...
: I am writing an application that will require new commands to be added
while
: the program is still running. I thought I had found the answer in .nets
: compiler facilities. I would seperate my commands out into a dll and
compile
: this at startup and when the source file is changed. This works fine on
: startup of the application. But when I try to recompile the dll during
: runtime it doesnt seem to work. If I leave the current dll in place then
it
: just carries on using the old dll and if i delete it then the compile
: function returns that it cannot find the new dll.
:
: Help would be appreciated.
:
: Public Function CompileCommands() As String
:
: Dim VBP As New VBCodeProvider()
:
: Dim CVB As System.CodeDom.Compiler.ICodeCompiler
:
: CVB = VBP.CreateCompiler
:
: Dim PM As New System.CodeDom.Compiler.CompilerParameters()
:
: PM.GenerateInMemory = True
:
: PM.GenerateExecutable = False
:
:
:
: PM.OutputAssembly = "commands.dll"
:
: PM.MainClass = "command"
:
: PM.IncludeDebugInformation = False
:
: Dim ASM As System.Reflection.Assembly
:
: 'I believe this is to get all the assemblies of the current
:
: 'application and add them as references to the new assembly
:
: PM.ReferencedAssemblies.Add("OpenTelnet.exe")
:
: For Each ASM In AppDomain.CurrentDomain.GetAssemblies()
:
: PM.ReferencedAssemblies.Add(ASM.Location)
:
: Next
:
: 'here we do the deed
:
: Dim Results As System.CodeDom.Compiler.CompilerResults
:
: Results = CVB.CompileAssemblyFromFile(PM, Application.StartupPath &
: "\defaultcommands\commands.vb")
:
: 'CVB.CompileAssemblyFromSource(PM, )
:
: 'this is just to list out all the errors if any
:
: Dim Err As System.CodeDom.Compiler.CompilerError
:
: Dim strerr As String
:
: For Each err In Results.Errors
:
: strerr = strerr & "Number " & err.ErrorNumber & " Text " & err.ErrorText &
"
: Line " & err.Line & vbCrLf
:
: Next
:
: If Results.Errors.Count > 0 Then
:
: Return strerr
:
: Exit Function
:
: End If
:
: 'now we create an instance of the assembly newly created
:
: 'and set it to an object notice this is also a good example of late
binding
:
: Dim vargs As Object
:
: commandobj = Results.CompiledAssembly.CreateInstance("commands. command",
: False, Reflection.BindingFlags.CreateInstance, Nothing, vargs, Nothing,
: Nothing)
:
:
:
: End Function
:
:
:
: Cheers
:
: Daren
:
:
Nov 20 '05 #2
Sure.

I am writing a MUD, (telnet style game) and obviously it will evolve over
time. So I needed a way to be able to "script" the commands that people will
use to interact with the game. So that I can change/add commands over time
without taking down the whole system.

If you do have another way then that would be cool.

"Tom Spink" <th**********@ntlworld.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
May I ask why this needs to be done? Perhaps I can offer an alternate
solution, using the System.Reflection.Emit namespace. It contains classes
that allow you to dynamically build an assembly in memory.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
"Daren Fox" <da***@foxonline.co.uk> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...
: I am writing an application that will require new commands to be added
while
: the program is still running. I thought I had found the answer in .nets
: compiler facilities. I would seperate my commands out into a dll and
compile
: this at startup and when the source file is changed. This works fine on
: startup of the application. But when I try to recompile the dll during
: runtime it doesnt seem to work. If I leave the current dll in place then
it
: just carries on using the old dll and if i delete it then the compile
: function returns that it cannot find the new dll.
:
: Help would be appreciated.
:
: Public Function CompileCommands() As String
:
: Dim VBP As New VBCodeProvider()
:
: Dim CVB As System.CodeDom.Compiler.ICodeCompiler
:
: CVB = VBP.CreateCompiler
:
: Dim PM As New System.CodeDom.Compiler.CompilerParameters()
:
: PM.GenerateInMemory = True
:
: PM.GenerateExecutable = False
:
:
:
: PM.OutputAssembly = "commands.dll"
:
: PM.MainClass = "command"
:
: PM.IncludeDebugInformation = False
:
: Dim ASM As System.Reflection.Assembly
:
: 'I believe this is to get all the assemblies of the current
:
: 'application and add them as references to the new assembly
:
: PM.ReferencedAssemblies.Add("OpenTelnet.exe")
:
: For Each ASM In AppDomain.CurrentDomain.GetAssemblies()
:
: PM.ReferencedAssemblies.Add(ASM.Location)
:
: Next
:
: 'here we do the deed
:
: Dim Results As System.CodeDom.Compiler.CompilerResults
:
: Results = CVB.CompileAssemblyFromFile(PM, Application.StartupPath &
: "\defaultcommands\commands.vb")
:
: 'CVB.CompileAssemblyFromSource(PM, )
:
: 'this is just to list out all the errors if any
:
: Dim Err As System.CodeDom.Compiler.CompilerError
:
: Dim strerr As String
:
: For Each err In Results.Errors
:
: strerr = strerr & "Number " & err.ErrorNumber & " Text " & err.ErrorText & "
: Line " & err.Line & vbCrLf
:
: Next
:
: If Results.Errors.Count > 0 Then
:
: Return strerr
:
: Exit Function
:
: End If
:
: 'now we create an instance of the assembly newly created
:
: 'and set it to an object notice this is also a good example of late
binding
:
: Dim vargs As Object
:
: commandobj = Results.CompiledAssembly.CreateInstance("commands. command",
: False, Reflection.BindingFlags.CreateInstance, Nothing, vargs, Nothing,
: Nothing)
:
:
:
: End Function
:
:
:
: Cheers
:
: Daren
:
:

Nov 20 '05 #3
Hello,

"Daren Fox" <da***@foxonline.co.uk> schrieb:
runtime it doesnt seem to work. If I leave the current dll in place then it just carries on using the old dll and if i delete it then the compile
function returns that it cannot find the new dll.


If you use the DLL, you must unload the whole 'AppDomain' the DLL is loaded
into. You can create a separate 'AppDomain', load the DLL into it and then
unload the 'AppDomain' in order to "free" the DLL:

http://msdn.microsoft.com/library/en...rp05162002.asp

Plug-in/out sample written in C# by Thomas Scheidegger:

http://www.codeproject.com/csharp/livecodedotnet.asp

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: janeaustine50 | last post by:
Python's InteractiveInterpreter uses the built-in compile function. According to the ref. manual, it doesn't seem to concern about the encoding of the source string. When I hand in an unicode...
5
by: Carmine Cairo | last post by:
Hi, I'm working on a project and today I've note a little problem during the compile fase. Here a little piece of code: // 1st version welldone = 0; size = p->getSize(); backbone = new...
5
by: Brice Prunier | last post by:
Here under 4 schemas i'm working with ( it may be long: sorry...) The context is the following : Resident.xsd imports Person.xsd and includes Common.xsd ( anonimous schema: no TargetNamespace )...
10
by: Chris LaJoie | last post by:
Our company has been developing a program in C# for some time now, and we haven't had any problems with it, but just last night something cropped up that has me, and everyone else, stumped. I...
6
by: Thomas Connolly | last post by:
I have 2 pages referencing the same codebehind file in my project. Originally the pages referenced separate code behind files. Once I changed the reference to the same file, everything worked...
15
by: steve yee | last post by:
i want to detect if the compile is 32 bits or 64 bits in the source code itself. so different code are compiled respectively. how to do this?
16
by: desktop | last post by:
I have read that using templates makes types know at compile time and using inheritance the types are first decided at runtime. The use of pointers and casts also indicates that the types will...
1
by: brianrpsgt1 | last post by:
Newbie here.... I have been able to successful pull info from a MySQL DB, get the results and output them in an HTML format using Cheetah to the screen using IDLE. I am doing this on a Windows...
3
by: NvrBst | last post by:
Right now I have C99 code in .c extensions. I compile it in VSC++ and it complains about a lot of errors. I change the extensions to .cpp and compile in VSC++ and it succeeds. Is there a way...
6
by: Ed Leafe | last post by:
I've noticed an odd behavior with compile() and code that does not contain a trailing newline: if the last line is a comment inside of any block, a syntax error is thrown, but if the last line is a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.